13-Pandas資料預處理之資料轉換(啞變數編碼pd.get_dummies())
認識SpringSecurity
Spring Security 是針對Spring專案的安全框架,也是Spring Boot底層安全模組預設的技術選型,他可以實現強大的Web安全控制,對於安全控制,我們僅需要引入spring-boot-starter-security模組,進行少量的配置,即可實現強大的安全管理!
記住幾個類:
-
WebSecurityConfigurerAdapter:自定義Security策略
-
AuthenticationManagerBuilder:自定義認證策略
-
@EnableWebSecurity:開啟WebSecurity模式
Spring Security的兩個主要目標是 “認證” 和 “授權”(訪問控制)。
“認證”(Authentication)
身份驗證是關於驗證您的憑據,如使用者名稱/使用者ID和密碼,以驗證您的身份。
身份驗證通常通過使用者名稱和密碼完成,有時與身份驗證因素結合使用。
“授權” (Authorization)
授權發生在系統成功驗證您的身份後,最終會授予您訪問資源(如資訊,檔案,資料庫,資金,位置,幾乎任何內容)的完全許可權。
這個概念是通用的,而不是隻在Spring Security 中存在。
認證和授權
目前,我們的測試環境,是誰都可以訪問的,我們使用Spring Security增加上認證和授權的功能
1、引入 Spring Security 模塊
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2、編寫 Spring Security 配置類
參考官網:https://spring.io/projects/spring-security
檢視我們自己專案中的版本,找到對應的幫助文件:
https://docs.spring.io/spring-security/site/docs/current/reference/html5/
3、編寫基礎配置類、定製請求的授權規則
@Override protected void configure(HttpSecurity http) throws Exception { // 定製請求的授權規則 http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") // 首頁所有人可以訪問
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3"); }
4、測試一下:發現除了首頁都進不去了!因為我們目前沒有登入的角色,因為請求需要登入的角色擁有對應的許可權才可以!
5、在configure()方法中加入以下配置,開啟自動配置的登入功能!
// 開啟自動配置的登入功能 // /login 請求來到登入頁 // /login?error 重定向到這裡表示登入失敗 http.formLogin();
6、測試一下:發現,沒有許可權的時候,會跳轉到登入的頁面!
7、檢視剛才登入頁的註釋資訊;
我們可以定義認證規則,重寫configure(AuthenticationManagerBuilder auth)方法
//定義認證規則 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //在記憶體中定義,也可以在jdbc中去拿.... auth.inMemoryAuthentication() .withUser("admin").password("123456").roles("vip2","vip3") .and() .withUser("root").password("123456").roles("vip1","vip2","vip3") .and() .withUser("guest").password("123456").roles("vip1","vip2"); }
8、測試,我們可以使用這些賬號登入進行測試!發現會報錯!
There is no PasswordEncoder mapped for the id “null”
9、原因,我們要將前端傳過來的密碼進行某種方式加密,否則就無法登入,修改程式碼
//定義認證規則 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //在記憶體中定義,也可以在jdbc中去拿.... //Spring security 5.0中新增了多種加密方式,也改變了密碼的格式。 //要想我們的專案還能夠正常登陸,需要修改一下configure中的程式碼。我們要將前端傳過來的密碼進行某種方式加密 //spring security 官方推薦的是使用bcrypt加密方式。 auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3") .and() .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2"); }
10、測試,發現,登入成功,並且每個角色只能訪問自己認證下的規則!搞定
許可權控制和登出
1、開啟自動配置的登出的功能
//定製請求的授權規則 @Override protected void configure(HttpSecurity http) throws Exception { //.... //開啟自動配置的登出的功能 http.logout(); }
2、我們在前端,增加一個登出的按鈕,index.html 導航欄中
<a class="item" th:href="@{/logout}"> <i class="address card icon"></i> 登出 </a>
3、我們可以去測試一下,登入成功後點擊登出,發現登出完畢會跳轉到登入頁面!
4、但是,我們想讓他登出成功後,依舊可以跳轉到首頁,該怎麼處理呢?
// .logoutSuccessUrl("/"); 登出成功來到首頁 http.logout().logoutSuccessUrl("/");
5、測試,登出完畢後,發現跳轉到首頁OK
6、我們現在又來一個需求:使用者沒有登入的時候,導航欄上只顯示登入按鈕,使用者登入之後,導航欄可以顯示登入的使用者資訊及登出按鈕!還有就是,比如admin這個使用者,它只有 vip2,vip3功能,那麼登入則只顯示這兩個功能,而vip1的功能選單不顯示!這個就是真實的網站情況了!該如何做呢?
我們需要結合thymeleaf中的一些功能
sec:authorize="isAuthenticated()":是否認證登入!來顯示不同的頁面
Maven依賴:
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 --> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> <version>3.0.4.RELEASE</version> </dependency>
7、修改我們的 前端頁面
匯入名稱空間
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
修改導航欄,增加認證判斷
<!--登入登出--> <div class="right menu"> <!--如果未登入--> <div sec:authorize="!isAuthenticated()"> <a class="item" th:href="@{/login}"> <i class="address card icon"></i> 登入 </a> </div> <!--如果已登入--> <div sec:authorize="isAuthenticated()"> <a class="item"> <i class="address card icon"></i> 使用者名稱:<span sec:authentication="principal.username"></span> 角色:<span sec:authentication="principal.authorities"></span> </a> </div> <div sec:authorize="isAuthenticated()"> <a class="item" th:href="@{/logout}"> <i class="address card icon"></i> 登出 </a> </div> </div>
8、重啟測試,我們可以登入試試看,登入成功後確實,顯示了我們想要的頁面;
9、如果登出404了,就是因為它預設防止csrf跨站請求偽造,因為會產生安全問題,我們可以將請求改為post表單提交,或者在spring security中關閉csrf功能;我們試試:在 配置中增加 http.csrf().disable();
http.csrf().disable();//關閉csrf功能:跨站請求偽造,預設只能通過post方式提交logout請求 http.logout().logoutSuccessUrl("/");
記住我
現在的情況,我們只要登入之後,關閉瀏覽器,再登入,就會讓我們重新登入,但是很多網站的情況,就是有一個記住密碼的功能,這個該如何實現呢?很簡單
1、開啟記住我功能
//定製請求的授權規則 @Override protected void configure(HttpSecurity http) throws Exception { //。。。。。。。。。。。 //記住我 http.rememberMe(); }
2、我們再次啟動專案測試一下,發現登入頁多了一個記住我功能,我們登入之後關閉 瀏覽器,然後重新開啟瀏覽器訪問,發現使用者依舊存在!
思考:如何實現的呢?其實非常簡單,我們可以檢視瀏覽器的cookie,登入成功後,將cookie傳送給瀏覽器儲存,以後登入帶上這個cookie,只要通過檢查就可以免登入了。如果點選登出,則會刪除這個cookie!
3、我們點選登出的時候,可以發現,spring security 幫我們自動刪除了這個 cookie