知識儲備:html
關於SpringSecurity的詳細學習能夠查看SpringSecurity的官方文檔。java
Spring Security概覽spring
應用程序的兩個主要區域是"認證"和"受權"(訪問控制)。這兩個主要區域是Spring Security的兩個目標。cookie
"認證"(Authentication),是創建一個他聲明的主體的過程(一個"主體"通常是指用戶,設備或一些能夠在你的應用程序中執行動做的其餘系統)。session
"受權"(Authorization)指肯定一個主體是否容許在你的應用程序執行一個動做的過程。爲了抵達須要受權的點, 主體的身份已經有認證過程創建。app
一.Spring Security快速入門ide
1.1 導入spring-boot-starter-securityspring-boot
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
1.2 編寫SpringSecurity的配置類,該類須要繼承WebSecurityConfigurerAdapterpost
這邊須要開啓基於WebSecurity的註解,因爲這個註解內部以及有了@Configuration,因此不須要再加上@Configuration了。學習
@EnableWebSecurity //開啓基於WebSecurity的註解(已經開啓了@Configuration) public class MySecurityConfig extends WebSecurityConfigurerAdapter { }
1.3 在配置類中定製受權規則
@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(); //該功能開啓以後的效果:SpringSecurity自動處理的請求 //一、/login 來到登陸頁 //二、重定向到 /login?error 表示登陸失敗 //三、更多信息 }
1.4 定義認證規則
這邊須要注意的是若是不用PasswordEncoder去驗證密碼會報錯誤,這裏是解決方案
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //super.configure(auth); //auth.jdbcAuthentication() //內存用戶驗證 /* auth.inMemoryAuthentication().withUser("wang").password("123456").roles("VIP1","VIP2").and() .withUser("xia").password("654321").roles("VIP2","VIP3"); //表單提交的時候密碼是以密文匹配,會報錯*/ auth.inMemoryAuthentication() .passwordEncoder(new MyPasswordEncoder()) .withUser("wang").password("123456").roles("VIP1","VIP2").and() .withUser("yun").password("123456").roles("VIP3"); //表單提交的時候密碼是以明文匹配 }
二.註銷
在protected void configure(HttpSecurity http) throws Exception(){}方法中開啓自動配置的註銷功能:
http.logout()
開啓後默認規則:
1.訪問/logout表示用戶註銷,清空session
2.註銷成功會返回/login?logout頁面
配置註銷之後直接來到首頁;
http.logout().logoutSuccessUrl("/"); //註銷之後來到首頁
完整代碼:
@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"); //訪問測試 //開啓自動配置的登錄功能,若是沒有登陸,則會來到登陸頁面 //SpringSecurity自動處理的請求 //默認規則 //一、/login 來到登陸頁 //二、重定向到 /login?error 表示登陸失敗 //三、更多信息 http.formLogin(); //開啓自動配置的註銷功能 //默認規則 //1.訪問/logout表示用戶註銷,清空session //2.註銷成功會返回/login?logout頁面 http.logout().logoutSuccessUrl("/"); //註銷之後來到首頁 }
三.權限控制
3.1 版本依賴
使用這項功能前須要先將SpringBoot版本切換爲1.5.6.RELEASE,SpringBoot2.x的版本使用時遇到了一些問題因此我將SpringBoot版本降級了。若是有Springboot2.x整合Spring Security成功使用權限控制的能夠教教我。。。
將SpringBoot版本變爲1.5.6.RELEASE後須要將thymeleaf的版本也切換一下:
<properties>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
<thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version>
</properties>
在html使用spring security標籤還須要導入thymeleaf-extra-springsecurity4包,版本爲上面的版本3.0.2.RELEASE
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> </dependency>
3.2 權限控制
首先引入標籤庫
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
若是沒有認證則顯示下面內容:
<div sec:authorize="!isAuthenticated()"> <h2 align="center">遊客您好,若是想查看武林祕籍 <a th:href="@{/login}">請登陸</a></h2> </div>
若是角色認證了顯示出角色的信息:
<!--若是認證了--> <div sec:authorize="isAuthenticated()"> <h2><span sec:authentication="name"></span>,您好,您的角色有<span sec:authentication="principal.authorities"></span></h2> <form th:action="@{/logout}" method="post"> <input type="submit" value="註銷"/> </form> </div>
控制只有擁有某個權限時才能顯示該內容
<div sec:authorize="hasRole('VIP1')"> <h3>普通武功祕籍</h3> <ul> <li><a th:href="@{/level1/1}">羅漢拳</a></li> <li><a th:href="@{/level1/2}">武當長拳</a></li> <li><a th:href="@{/level1/3}">全真劍法</a></li> </ul> </div>
四.記住我
在protected void configure(HttpSecurity http) throws Exception(){}方法中開啓記住我功能:
//開啓記住我功能 http.rememberMe();
開啓該功能後,登錄頁面會自動多一個記住個人複選框按鈕。
勾選登錄後,會自動生成cookie,下次訪問時無需登錄便可訪問。
五.自定義登錄頁
上述功能都是基於默認規則的登陸功能,那麼如何制定本身的登陸頁呢?
5.1 定製Controller登陸頁
/* * 登錄頁 * @return */ @GetMapping("/userlogin") public String loginPage() { return PREFIX+"login"; }
5.2 在protected void configure(HttpSecurity http) throws Exception(){}中配置:
http.formLogin().usernameParameter("user").passwordParameter("pwd").loginPage("/userlogin");
一旦定製loginPage,那麼loginPage的post請求就是登陸。
5.3 登陸的表單
<div align="center"> <form th:action="@{/userlogin}" method="post"> 用戶名:<input name="user"/><br> 密碼:<input name="pwd"><br/> <input type="checkbox" name="remeber"/>記住我 <br/> <input type="submit" value="登錄"> </form> </div>
5.4 自定義記住我功能
//開啓記住我功能 http.rememberMe().rememberMeParameter("remeber");