From 8283310b770788922e93081d6526d1c26216ab8f Mon Sep 17 00:00:00 2001 From: mrbird <852252810@qq.com> Date: Tue, 21 Aug 2018 16:07:34 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Boot=E4=B8=AD=E5=BC=80=E5=90=AFSpring?= =?UTF-8?q?=20Security?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 34.Start-Spring-Security/pom.xml | 50 +++++++++++++++++++ .../java/cc/mrbird/SecurityApplication.java | 12 +++++ .../browser/BrowserSecurityConfig.java | 19 +++++++ .../mrbird/web/controller/TestController.java | 12 +++++ .../src/main/resources/application.yml | 3 ++ 5 files changed, 96 insertions(+) create mode 100644 34.Start-Spring-Security/pom.xml create mode 100644 34.Start-Spring-Security/src/main/java/cc/mrbird/SecurityApplication.java create mode 100644 34.Start-Spring-Security/src/main/java/cc/mrbird/security/browser/BrowserSecurityConfig.java create mode 100644 34.Start-Spring-Security/src/main/java/cc/mrbird/web/controller/TestController.java create mode 100644 34.Start-Spring-Security/src/main/resources/application.yml diff --git a/34.Start-Spring-Security/pom.xml b/34.Start-Spring-Security/pom.xml new file mode 100644 index 0000000..0c4200b --- /dev/null +++ b/34.Start-Spring-Security/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + cc.mrbird + Security + 1.0-SNAPSHOT + jar + + Security + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.14.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-security + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/34.Start-Spring-Security/src/main/java/cc/mrbird/SecurityApplication.java b/34.Start-Spring-Security/src/main/java/cc/mrbird/SecurityApplication.java new file mode 100644 index 0000000..4740934 --- /dev/null +++ b/34.Start-Spring-Security/src/main/java/cc/mrbird/SecurityApplication.java @@ -0,0 +1,12 @@ +package cc.mrbird; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SecurityApplication { + + public static void main(String[] args) { + SpringApplication.run(SecurityApplication.class, args); + } +} diff --git a/34.Start-Spring-Security/src/main/java/cc/mrbird/security/browser/BrowserSecurityConfig.java b/34.Start-Spring-Security/src/main/java/cc/mrbird/security/browser/BrowserSecurityConfig.java new file mode 100644 index 0000000..06adbd5 --- /dev/null +++ b/34.Start-Spring-Security/src/main/java/cc/mrbird/security/browser/BrowserSecurityConfig.java @@ -0,0 +1,19 @@ +package cc.mrbird.security.browser; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.formLogin() // 表单登录 + // http.httpBasic() // HTTP Basic + .and() + .authorizeRequests() // 授权配置 + .anyRequest() // 所有请求 + .authenticated(); // 都需要认证 + } +} diff --git a/34.Start-Spring-Security/src/main/java/cc/mrbird/web/controller/TestController.java b/34.Start-Spring-Security/src/main/java/cc/mrbird/web/controller/TestController.java new file mode 100644 index 0000000..0319c6e --- /dev/null +++ b/34.Start-Spring-Security/src/main/java/cc/mrbird/web/controller/TestController.java @@ -0,0 +1,12 @@ +package cc.mrbird.web.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class TestController { + @GetMapping("hello") + public String hello() { + return "hello spring security"; + } +} diff --git a/34.Start-Spring-Security/src/main/resources/application.yml b/34.Start-Spring-Security/src/main/resources/application.yml new file mode 100644 index 0000000..a618ee1 --- /dev/null +++ b/34.Start-Spring-Security/src/main/resources/application.yml @@ -0,0 +1,3 @@ +security: + basic: + enabled: true \ No newline at end of file