From 236cb5a59bfb018eb3ca4361226b2875fbf71a96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=8C=BFDD?= Date: Sat, 28 May 2016 19:24:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8Spring=20Security=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E5=AE=89=E5=85=A8=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter4-3-1/pom.xml | 66 +++++++++++++++++++ .../main/java/com/didispace/Application.java | 22 +++++++ .../java/com/didispace/WebSecurityConfig.java | 36 ++++++++++ .../com/didispace/web/HelloController.java | 33 ++++++++++ .../src/main/resources/application.properties | 0 .../src/main/resources/templates/hello.html | 13 ++++ .../src/main/resources/templates/index.html | 12 ++++ .../src/main/resources/templates/login.html | 21 ++++++ .../java/com/didispace/ApplicationTests.java | 48 ++++++++++++++ 9 files changed, 251 insertions(+) create mode 100644 Chapter4-3-1/pom.xml create mode 100644 Chapter4-3-1/src/main/java/com/didispace/Application.java create mode 100644 Chapter4-3-1/src/main/java/com/didispace/WebSecurityConfig.java create mode 100644 Chapter4-3-1/src/main/java/com/didispace/web/HelloController.java create mode 100644 Chapter4-3-1/src/main/resources/application.properties create mode 100644 Chapter4-3-1/src/main/resources/templates/hello.html create mode 100644 Chapter4-3-1/src/main/resources/templates/index.html create mode 100644 Chapter4-3-1/src/main/resources/templates/login.html create mode 100644 Chapter4-3-1/src/test/java/com/didispace/ApplicationTests.java diff --git a/Chapter4-3-1/pom.xml b/Chapter4-3-1/pom.xml new file mode 100644 index 0000000..fc27463 --- /dev/null +++ b/Chapter4-3-1/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + com.didispace + Chapter4-3-1 + 1.0.0 + jar + + Chapter4-3-1 + Spring Boot with Thymeleaf + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-security + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + \ No newline at end of file diff --git a/Chapter4-3-1/src/main/java/com/didispace/Application.java b/Chapter4-3-1/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..f7a9e16 --- /dev/null +++ b/Chapter4-3-1/src/main/java/com/didispace/Application.java @@ -0,0 +1,22 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + + SpringApplication.run(Application.class, args); + + } + +} diff --git a/Chapter4-3-1/src/main/java/com/didispace/WebSecurityConfig.java b/Chapter4-3-1/src/main/java/com/didispace/WebSecurityConfig.java new file mode 100644 index 0000000..02f1618 --- /dev/null +++ b/Chapter4-3-1/src/main/java/com/didispace/WebSecurityConfig.java @@ -0,0 +1,36 @@ +package com.didispace; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers("/", "/home").permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage("/login") + .permitAll() + .and() + .logout() + .permitAll(); + } + + @Autowired + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user").password("password").roles("USER"); + } + +} \ No newline at end of file diff --git a/Chapter4-3-1/src/main/java/com/didispace/web/HelloController.java b/Chapter4-3-1/src/main/java/com/didispace/web/HelloController.java new file mode 100644 index 0000000..0247f86 --- /dev/null +++ b/Chapter4-3-1/src/main/java/com/didispace/web/HelloController.java @@ -0,0 +1,33 @@ +package com.didispace.web; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@Controller +public class HelloController { + + @RequestMapping("/") + public String index() { + return "index"; + } + + @RequestMapping("/hello") + public String hello() { + return "hello"; + } + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public String login() { + return "login"; + } + +} \ No newline at end of file diff --git a/Chapter4-3-1/src/main/resources/application.properties b/Chapter4-3-1/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/Chapter4-3-1/src/main/resources/templates/hello.html b/Chapter4-3-1/src/main/resources/templates/hello.html new file mode 100644 index 0000000..5147713 --- /dev/null +++ b/Chapter4-3-1/src/main/resources/templates/hello.html @@ -0,0 +1,13 @@ + + + + Hello World! + + +

Hello [[${#httpServletRequest.remoteUser}]]!

+
+ +
+ + \ No newline at end of file diff --git a/Chapter4-3-1/src/main/resources/templates/index.html b/Chapter4-3-1/src/main/resources/templates/index.html new file mode 100644 index 0000000..ffe2834 --- /dev/null +++ b/Chapter4-3-1/src/main/resources/templates/index.html @@ -0,0 +1,12 @@ + + + + Spring Security入门 + + +

欢迎使用Spring Security!

+ +

点击 这里 打个招呼吧

+ + \ No newline at end of file diff --git a/Chapter4-3-1/src/main/resources/templates/login.html b/Chapter4-3-1/src/main/resources/templates/login.html new file mode 100644 index 0000000..f5cbe8e --- /dev/null +++ b/Chapter4-3-1/src/main/resources/templates/login.html @@ -0,0 +1,21 @@ + + + + Spring Security Example + + +
+ 用户名或密码错 +
+
+ 您已注销成功 +
+
+
+
+
+
+ + \ No newline at end of file diff --git a/Chapter4-3-1/src/test/java/com/didispace/ApplicationTests.java b/Chapter4-3-1/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..c5b2e60 --- /dev/null +++ b/Chapter4-3-1/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,48 @@ +package com.didispace; + +import com.didispace.web.HelloController; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class ApplicationTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup( + new HelloController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Hello World"))); + } + +}