1.Spring Security是針對Spring項目的安全框架,也是Spring Boot底層安全模塊默認的技術選型。他能夠實現強大的web安全控制。對於安全控制,咱們僅需引入spring-boot-starter-security模塊,進行少許的配置,便可實現強大的安全管理。幾個類:
**WebSecurityConfigurerAdapter:自定義Security策略
AuthenticationManagerBuilder:自定義認證策略
@EnableWebSecurity:開啓WebSecurity模式**java
2.應用程序的兩個主要區域是「認證」和「受權」(或者訪問控制)。這兩個主要區域是Spring Security 的兩個目標。web
3.「認證」(Authentication),是創建一個他聲明執行流程的主體的過程(一個「主體」通常是指用戶,設備或一些能夠在你的應用程序中執行動做的其餘系統)。spring
4.「受權」(Authorization)指肯定一個主體是否容許在你的應用程序執行一個動做的過程。爲了抵達須要受權的店,主體的身份已經有認證過程創建。瀏覽器
5.這個概念是通用的而不僅在Spring Security中。安全
HttpSecurity配置登錄、註銷功能cookie
須要引入thymeleaf-extras-springsecurity4
sec:authentication=「name」得到當前用戶的用戶名
sec:authorize=「hasRole(‘ADMIN’)」當前用戶必須擁有ADMIN權限時纔會顯示標籤內容session
表單添加remember-me的checkbox
配置啓用remember-me功能框架
HttpSecurity啓用csrf功能,會爲表單添加_csrf的值,提交攜帶來預防CSRF;
執行流程ide
一、引入SpringSecurity; 二、編寫SpringSecurity的配置類; @EnableWebSecurity extends WebSecurityConfigurerAdapter 三、控制請求的訪問權限: configure(HttpSecurity http) { http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("VIP1") } 四、定義認證規則: configure(AuthenticationManagerBuilder auth){ auth.inMemoryAuthentication().withUser("zhangsan").password("123456").roles("VIP1","VIP2") } 五、開啓自動配置的登錄功能: configure(HttpSecurity http){ http.formLogin(); } 六、註銷:http.logout(); 七、記住我:Remeberme();
1.spring-boot
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2.
package com.atguigu.security.config; 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; @EnableWebSecurity public class MySecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //super.configure(http); //定製請求的受權規則 http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("VIP1") .antMatchers("/level2/**").hasRole("VIP2") .antMatchers("/level3/**").hasRole("VIP3"); //開啓自動配置的登錄功能,效果,若是沒有登錄,沒有權限就會來到登錄頁面 http.formLogin().usernameParameter("user").passwordParameter("pwd") .loginPage("/userlogin"); //一、/login來到登錄頁 //二、重定向到/login?error表示登錄失敗 //三、更多詳細規定 //四、默認post形式的 /login表明處理登錄 //五、一但定製loginPage;那麼 loginPage的post請求就是登錄 //開啓自動配置的註銷功能。 http.logout().logoutSuccessUrl("/");//註銷成功之後來到首頁 //一、訪問 /logout 表示用戶註銷,清空session //二、註銷成功會返回 /login?logout 頁面; //開啓記住我功能 http.rememberMe().rememberMeParameter("remeber"); //登錄成功之後,將cookie發給瀏覽器保存,之後訪問頁面帶上這個cookie,只要經過檢查就能夠免登陸 //點擊註銷會刪除cookie } //定義認證規則 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //super.configure(auth); auth.inMemoryAuthentication() .withUser("zhangsan").password("123456").roles("VIP1","VIP2") .and() .withUser("lisi").password("123456").roles("VIP2","VIP3") .and() .withUser("wangwu").password("123456").roles("VIP1","VIP3"); } }