9、Spring Boot安全
- 2020 年 11 月 26 日
- 筆記
- springboot
1.Spring Security簡介
Spring Security是針對Spring項目的安全框架,也是Spring Boot底層安全模塊默認的技術選型。他可以實現強大的web安全控制。對於安全控制,我們僅需引入spring-boot-starter-security模塊,進行少量的配置,即可實現強大的安全管理。
WebSecurityConfigurerAdapter:自定義Security策略
AuthenticationManagerBuilder:自定義認證策略
@EnableWebSecurity:開啟WebSecurity模式
應用程序的兩個主要區域是‘認證’和’授權'(或者訪問控制)。
‘認證’和’授權’主要區域是Spring Security 的兩個目標。
認證(Authentication),是建立一個他聲明的主體的過程(一個’主體’一般是指用戶,設備或一些可以在你的應用程序中執行動作的其他系統)
‘授權'(Authorization)指確定一個主體是否允許在你的應用程序執行一個動作的過程。為了抵達需要授權的店,主體的身份已經有認證過程建立。
2.Spring Security使用
(1).創建工程
|
(2).引入SpringSecurity
<!–security–> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> |
(3).導入文件
(4).SpringSecurity配置類
HttpSecurity配置登陸、註銷功能
package com.hosystem.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.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@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(); //1. /login到登錄頁 //2. 重定向到/login?error表示登錄失敗 //3. 更多詳細規定 }
//定義認證規則 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // super.configure(auth);
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser(“tom”).password(new BCryptPasswordEncoder().encode(“123456”)).roles(“VIP1”,“VIP2”) .and() .withUser(“jack”).password(new BCryptPasswordEncoder().encode(“123456”)).roles(“VIP2”,“VIP3”) .and() .withUser(“lucy”).password(new BCryptPasswordEncoder().encode(“123456”)).roles(“VIP1”,“VIP3”);
} } |
註:如果出現There is no PasswordEncoder mapped for the id 「null」
或者 Encoded password does not look like bcrypt(Bad credentials)基本都是springsecurity版本的問題。只需要使用passwordEncoder(new BCryptPasswordEncoder())替換原來的即可。
#老版本springsecurity auth.inMemoryAuthentication().withUser(“user”).password(“123456”).roles(“VIP1”);
#新版本springsecurity auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser(“tom”).password(new BCryptPasswordEncoder().encode(“123456”)).roles(“VIP1”,“VIP2”); |
(5).Thymeleaf提供的SpringSecurity標籤支持
[1].引入thymeleaf-extras-springsecurity5
<!–springsecurity5–> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> |
[2].sec:authorize使用
<!DOCTYPE html> <html xmlns:th=“//www.thymeleaf.org” xmlns:sec=“//www.thymeleaf.org/thymeleaf-extras-springsecurity5”> <head> <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”> <title>Insert title here</title> </head> <body> <h1 align=“center”>歡迎光臨武林秘籍管理系統</h1> <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=“註銷” |