Spring Security是針對Spring項目的安全框架,也是Spring Boot底層安全模塊默認的技術選型。他能夠實現強大的web安全控制。對於安全控制,咱們僅需引入spring-boot-starter-security模塊,進行少許的配置,便可實現強大的安全管理。html
WebSecurityConfigurerAdapter:自定義Security策略web
AuthenticationManagerBuilder:自定義認證策略spring
@EnableWebSecurity:開啓WebSecurity模式安全
應用程序的兩個主要區域是'認證'和'受權'(或者訪問控制)。app
'認證'和'受權'主要區域是Spring Security 的兩個目標。框架
認證(Authentication),是創建一個他聲明的主體的過程(一個'主體'通常是指用戶,設備或一些能夠在你的應用程序中執行動做的其餘系統)ide
'受權'(Authorization)指肯定一個主體是否容許在你的應用程序執行一個動做的過程。爲了抵達須要受權的店,主體的身份已經有認證過程創建。spring-boot
|
<!--security-->web安全 <dependency>post <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> |
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"); |
<!--springsecurity5--> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> |
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://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="註銷" |