Spring Security常用配置的使用解讀
Spring Security 是 Spring 家族為我們提供的一款安全管理的框架,它是一個功能強大并且可以靈活定制的身份驗證和訪問控制框架。Spring Security 側重于為 Java 應用程序提供身份驗證和授權。與所有 Spring 項目一樣,Spring Security 的真正強大之處在于它非常容易擴展來滿足我們的不同需求。
在 SSM 時代,Spring Security 因為繁瑣的配置而不被人們常用,但是在 Spring Boot 中為提供了自動化配置方案,可以零配置使用 Spring Security。
初體驗
在 pom.xml 中導入 maven 依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
只要在項目中加入 Spring Security 的依賴,項目中的所有接口都會被保護起來了。
當我們訪問我們的項目的時候,就會先來到 Spring Security 默認的登錄頁面,

默認的用戶名是 user,密碼會在控制臺打印,

登錄后就可以正常訪問項目了。
自定義用戶名密碼
因為密碼是隨機生成的一段密鑰,不方便記憶,所以我們可以自己配置用戶名和密碼。
配置用戶名和密碼的方式有三種,我們可以在配置文件中配置,也可以在 Java 代碼中配置,還可以在數(shù)據(jù)庫中配置,我們先看如何在配置文件中配置。
使用配置文件配置
使用配置文件配置比較簡單,直接在 application.yml 中配置即可。
spring:
security:
user:
name: user
password: 1234使用 Java 代碼配置
使用 Java 代碼配置也比較簡單,我們只需要編寫一個 SecurityConfig 配置類,重寫一個 configure() 方法即可。
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("$2a$10$zwUhw4cAEv1AH6auayRPbePJAKk87peABiKegNMp4mqKXWxJZyDQS").roles("user")
.and()
.withUser("admin").password("$2a$10$mDQiCHTt3RLV.pLozBKOBOVIe7kaa3vYUCqZUu.957mpomdztOr0y").roles("admin");
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}使用 Java 代碼配置比較靈活,我們可以用 and() 方法來配置多個用戶。
在 Spring 5 以后要求我們配置的密碼必須是加密的,我們可以配置一個 BCryptPasswordEncoder 密碼編碼器來幫助我們加密密碼,我們只需要在單元測試中創(chuàng)建一個 BCryptPasswordEncoder 密碼編碼器,調用它的 encode()方法來加密,把得到的值復制到代碼中,然后再將這個密碼編碼器配置到容器中,這個密碼編碼器的的好處是即使是相同的字段也可以得到不同的結果。
自定義攔截規(guī)則
因為 Spring Security 默認攔截所有的請求,但我們實際項目中肯定不能這樣,所以我們應該自定義攔截規(guī)則,針對不同的請求,制定不同的處理方式。
這就需要用到 HttpSecurity 的配置,我們只需要在配置類中實現(xiàn)重載的 configure() 方法
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests() // 驗證請求
// 路徑匹配,參數(shù)是要處理的 url
.antMatchers("/admin/**").hasRole("admin") // 要具有某種權限
.antMatchers("/user/**").hasAnyRole("admin", "user")// 要具有某種權限中的一種
.anyRequest().authenticated();
}
}登錄注銷配置
Spring Security 為我們提供的絕不止上面的那么簡單,我們通過配置 HttpSecurity 還可以定制登錄接口,登錄成功后的響應,登錄失敗后的響應以及注銷的相關操作。
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.formLogin()
// 登錄處理接口
.loginProcessingUrl("/login")
// 定義登錄頁面,未登錄時,訪問一個需要登錄之后才能訪問的接口,會自動跳轉到該頁面
.loginPage("/login")
// 定義登錄時,用戶名的 key,默認為 username
.usernameParameter("uname")
// 定義登錄時,用戶密碼的 key,默認為 password
.passwordParameter("passwd")
// 登錄成功的處理器
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
httpServletResponse.setContentType("application/json;charset=utf-8");
PrintWriter out = httpServletResponse.getWriter();
Map<String, Object> map = new HashMap<>();
map.put("status", 200);
map.put("msg", authentication.getPrincipal());
out.write(new ObjectMapper().writeValueAsString(map));
out.flush();
out.close();
}
})
// 登錄失敗的處理器
.failureHandler(new AuthenticationFailureHandler() {
@Override
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
httpServletResponse.setContentType("application/json;charset=utf-8");
PrintWriter out = httpServletResponse.getWriter();
Map<String, Object> map = new HashMap<>();
map.put("status", 401);
if (e instanceof LockedException) {
map.put("msg", "賬戶被鎖定,登錄失??!");
} else if (e instanceof BadCredentialsException) {
map.put("msg", "用戶名或密碼輸入錯誤,登錄失??!");
} else if (e instanceof DisabledException) {
map.put("msg", "賬戶被禁用,登錄失??!");
} else if (e instanceof AccountExpiredException) {
map.put("msg", "賬戶過期,登錄失??!");
} else if (e instanceof CredentialsExpiredException) {
map.put("msg", "密碼過期,登錄失??!");
} else {
map.put("msg", "登錄失??!");
}
out.write(new ObjectMapper().writeValueAsString(map));
out.flush();
out.close();
}
})
// 和表單登錄相關的接口統(tǒng)統(tǒng)都直接通過
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
// 注銷成功的處理器
.logoutSuccessHandler(new LogoutSuccessHandler() {
@Override
public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
httpServletResponse.setContentType("application/json;charset=utf-8");
PrintWriter out = httpServletResponse.getWriter();
Map<String, Object> map = new HashMap<>();
map.put("status", 200);
map.put("msg", "注銷登錄成功!");
out.write(new ObjectMapper().writeValueAsString(map));
out.flush();
out.close();
}
});
}
}方法安全
Spring Security 還為我們提供了方法級別安全的配置,什么是方法安全呢?就是在調用方法的時候來進行驗證和授權。怎么實現(xiàn)方法安全呢?
首先我們要在配置類上加一個注解 @EnableGlobalMethodSecurity,
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}并將 prePostEnabled 和 securedEnabled 兩個屬性值設置為 true,接下來就可以在方法上加注解來進行權限控制了。
我們先寫一個 MethodService
@Service
public class MethodService {
@PreAuthorize("hasRole('admin')")
public String admin() {
return "hello admin";
}
@Secured("ROLE_user")
public String user() {
return "hello user";
}
@PreAuthorize("hasAnyRole('admin', 'user')")
public String hello() {
return "hello hello";
}
}用@PreAuthorize 注解和@Secured 注解來控制方法的訪問權限,再寫一個 HelloController
@RestController
public class HelloController {
@Autowired
MethodService methodService;
@GetMapping("hello1")
public String hello1() {
return methodService.admin();
}
@GetMapping("hello2")
public String hello2() {
return methodService.user();
}
@GetMapping("hello3")
public String hello3() {
return methodService.hello();
}
}此時啟動項目,我們用 admin 登錄,分別發(fā)送 hello1,hello2,hello3 請求

hello1 請求能夠訪問

因為配置了 user() 方法要具有 user 權限才能訪問,所以報 403 錯誤

總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Springboot中的異步任務執(zhí)行及監(jiān)控詳解
這篇文章主要介紹了Springboot中的異步任務執(zhí)行及監(jiān)控詳解,除了自己實現(xiàn)線程外,springboot本身就提供了通過注解的方式,進行異步任務的執(zhí)行,下面主要記錄一下,在Springboot項目中實現(xiàn)異步任務,以及對異步任務進行封裝監(jiān)控,需要的朋友可以參考下2023-10-10
SpringBoot利用隨機鹽值實現(xiàn)密碼的加密與驗證
這篇文章主要為大家詳細介紹了SpringBoot如何利用隨機鹽值實現(xiàn)密碼的加密與驗證,文中的示例代碼講解詳細,有需要的小伙伴可以參考下2024-02-02
Java使用Queryable-pageable實現(xiàn)分頁效果
這篇文章主要為大家介紹了Java如何使用Queryable-pageable從而實現(xiàn)分頁效果,文中的示例代碼簡潔易懂,感興趣的小伙伴可以動手嘗試一下2022-06-06

