Java SpringSecurity入門案例與基本原理詳解
1、入門案例
1.1、創(chuàng)建SpringBoot項目

1.2、勾選對應(yīng)的maven依賴

這里一些依賴可以沒有,最主要是要有Web和Security兩個依賴即可!
1.3、編寫Controller路由
@Controller
public class RouterController {
@RequestMapping(value = {"/index","/","/index.html"})
@ResponseBody
public String success(){
return "Hello SpringSecurity";
}
}

1.4、啟動項目
- 啟動項目之后會發(fā)現(xiàn)自動來到了登錄頁面,這個登錄頁面并不是我們寫的,是由Security自帶的并且現(xiàn)在說明Security已經(jīng)開啟了用戶認證。
- 可以在控制臺拿到密碼(隨機),用戶名為user;使用密碼登錄之后就能看到頁面了!

2、基本原理
2.1、Security的本質(zhì)
- SpringSecurity的本質(zhì)是Interceptor攔截器 + Filter過濾器的執(zhí)行鏈,而攔截器的本質(zhì)是AOP。
- 可以在security包中看到大量的攔截器類、過濾器類等等,它們分別負責(zé)不同的功能。
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter org.springframework.security.web.context.SecurityContextPersistenceFilter org.springframework.security.web.header.HeaderWriterFilter org.springframework.security.web.csrf.CsrfFilter org.springframework.security.web.authentication.logout.LogoutFilter org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter org.springframework.security.web.savedrequest.RequestCacheAwareFilter org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter org.springframework.security.web.authentication.AnonymousAuthenticationFilter org.springframework.security.web.session.SessionManagementFilter org.springframework.security.web.access.ExceptionTranslationFilter org.springframework.security.web.access.intercept.FilterSecurityInterceptor
2.2、Security裝載過程(一)
- 根據(jù)SpringBoot自動裝配原理可以得知,SpringBoot在啟動時會自動加載spring-boot-autoconfigure包下的spring.factories中的配置,其中有做安全認證的security組件!

- 雖然自動裝配了security的組件,但是并沒有完全生效,還需要導(dǎo)入security的依賴,這時才會根據(jù)@Condition進行條件裝配bean。其中最主要的是會裝載一個DelegatingFilterProxy類。
- DelegatingFilterProxy類的作用是將上述所有的過濾器進行串起來
// 過濾器執(zhí)行鏈
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
Filter delegateToUse = this.delegate;
if (delegateToUse == null) {
synchronized(this.delegateMonitor) {
delegateToUse = this.delegate;
if (delegateToUse == null) {
WebApplicationContext wac = this.findWebApplicationContext();
if (wac == null) {
throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?");
}
delegateToUse = this.initDelegate(wac);
}
this.delegate = delegateToUse;
}
}
this.invokeDelegate(delegateToUse, request, response, filterChain);
}
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
String targetBeanName = this.getTargetBeanName(); //FilterChainProxy
Assert.state(targetBeanName != null, "No target bean name set");
Filter delegate = (Filter)wac.getBean(targetBeanName, Filter.class);
if (this.isTargetFilterLifecycle()) {
delegate.init(this.getFilterConfig());
}
return delegate;
}
2.3、Security裝載過程(二)
- initDelegate方法中的getTargetBeanName為springSecurityFilterChain,由FilterChainProxy類生成
- 內(nèi)部核心方法doFilterInternal中可以看到獲取到的所有過濾器。
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
....
doFilterInternal(request, response, chain);
....
}
private void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
...
List<Filter> filters = getFilters(firewallRequest);
...
}

一共有14個自動裝配好的過濾器、攔截器
2.4、UsernamePasswordAuthenticationFilter過濾器
- 這是Security中的一個過濾器,用戶對登錄/login請求進行攔截驗證的實現(xiàn)類。
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
if (this.postOnly && !request.getMethod().equals("POST")) {
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
}
String username = obtainUsername(request);
username = (username != null) ? username : "";
username = username.trim();
String password = obtainPassword(request);
password = (password != null) ? password : "";
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
// Allow subclasses to set the "details" property
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
其中核心的方法是authenticate()方法,在這里對用戶提交的賬號和密碼進行驗證。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
基于Java反射的map自動裝配JavaBean工具類設(shè)計示例代碼
這篇文章主要給大家介紹了關(guān)于基于Java反射的map自動裝配JavaBean工具類設(shè)計的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2018-10-10
SpringBoot實現(xiàn)接口返回數(shù)據(jù)脫敏的代碼示例
在當(dāng)今的信息化時代,數(shù)據(jù)安全尤為重要,接口返回數(shù)據(jù)脫敏是一種重要的數(shù)據(jù)保護手段,可以防止敏感信息通過接口返回給客戶端,本文旨在探討如何在SpringBoot應(yīng)用程序中實現(xiàn)接口返回數(shù)據(jù)脫敏,需要的朋友可以參考下2024-07-07
深入解析StringBuffer和StringBuilder的區(qū)別
以下是對java中StringBuffer與StringBuilder的區(qū)別進行了詳細的分析介紹,需要的朋友可以參考下2013-07-07
Spring Cache @Cacheable 緩存在部分Service中不生效的解決辦法
這篇文章主要介紹了Spring Cache @Cacheable 緩存在部分Service中不生效的解決辦法2023-10-10
Java的Struts框架中<results>標(biāo)簽的使用方法
這篇文章主要介紹了Java的Struts框架中<results>標(biāo)簽的使用方法,Struts框架是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-11-11
AndroidStudio無法新建Java工程的簡單解決辦法
AS創(chuàng)建java工程是非常麻煩的,AS沒有提供直接創(chuàng)建java工程的方法且常常無法新建,這篇文章主要給大家介紹了關(guān)于AndroidStudio無法新建Java工程的簡單解決辦法,需要的朋友可以參考下2024-06-06
go語言題解LeetCode88合并兩個有序數(shù)組示例
這篇文章主要為大家介紹了go語言題解LeetCode88合并兩個有序數(shù)組示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12

