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

1.2、勾選對(duì)應(yīng)的maven依賴(lài)

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

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

2、基本原理
2.1、Security的本質(zhì)
- SpringSecurity的本質(zhì)是Interceptor攔截器 + Filter過(guò)濾器的執(zhí)行鏈,而攔截器的本質(zhì)是AOP。
- 可以在security包中看到大量的攔截器類(lèi)、過(guò)濾器類(lèi)等等,它們分別負(fù)責(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裝載過(guò)程(一)
- 根據(jù)SpringBoot自動(dòng)裝配原理可以得知,SpringBoot在啟動(dòng)時(shí)會(huì)自動(dòng)加載spring-boot-autoconfigure包下的spring.factories中的配置,其中有做安全認(rèn)證的security組件!

- 雖然自動(dòng)裝配了security的組件,但是并沒(méi)有完全生效,還需要導(dǎo)入security的依賴(lài),這時(shí)才會(huì)根據(jù)@Condition進(jìn)行條件裝配bean。其中最主要的是會(huì)裝載一個(gè)DelegatingFilterProxy類(lèi)。
- DelegatingFilterProxy類(lèi)的作用是將上述所有的過(guò)濾器進(jìn)行串起來(lái)
// 過(guò)濾器執(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裝載過(guò)程(二)
- initDelegate方法中的getTargetBeanName為springSecurityFilterChain,由FilterChainProxy類(lèi)生成
- 內(nèi)部核心方法doFilterInternal中可以看到獲取到的所有過(guò)濾器。
@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個(gè)自動(dòng)裝配好的過(guò)濾器、攔截器
2.4、UsernamePasswordAuthenticationFilter過(guò)濾器
- 這是Security中的一個(gè)過(guò)濾器,用戶(hù)對(duì)登錄/login請(qǐng)求進(jìn)行攔截驗(yàn)證的實(shí)現(xiàn)類(lèi)。
@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()方法,在這里對(duì)用戶(hù)提交的賬號(hào)和密碼進(jìn)行驗(yàn)證。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
HTTP基本認(rèn)證(Basic Authentication)的JAVA實(shí)例代碼
下面小編就為大家?guī)?lái)一篇HTTP基本認(rèn)證(Basic Authentication)的JAVA實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
SpringBoot2整合JTA組件實(shí)現(xiàn)多數(shù)據(jù)源事務(wù)管理
這篇文章主要介紹了SpringBoot2整合JTA組件實(shí)現(xiàn)多數(shù)據(jù)源事務(wù)管理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Spring?Boot多個(gè)定時(shí)任務(wù)阻塞問(wèn)題的解決方法
在日常的項(xiàng)目開(kāi)發(fā)中,往往會(huì)涉及到一些需要做到定時(shí)執(zhí)行的代碼,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot多個(gè)定時(shí)任務(wù)阻塞問(wèn)題的解決方法,需要的朋友可以參考下2022-01-01
Spring整合websocket整合應(yīng)用示例(下)
這篇文章主要介紹了Spring整合websocket整合應(yīng)用示例(下)的相關(guān)資料,需要的朋友可以參考下2016-04-04
feign調(diào)用中文參數(shù)被encode編譯的問(wèn)題
這篇文章主要介紹了feign調(diào)用中文參數(shù)被encode編譯的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
一文帶你熟練掌握J(rèn)ava中的日期時(shí)間相關(guān)類(lèi)
我們?cè)陂_(kāi)發(fā)時(shí),除了數(shù)字、數(shù)學(xué)這樣的常用API之外,還有日期時(shí)間類(lèi),更是會(huì)被經(jīng)常使用,比如我們項(xiàng)目中必備的日志功能,需要記錄異常等信息產(chǎn)生的時(shí)間,本文就帶各位來(lái)學(xué)習(xí)一下相關(guān)的日期時(shí)間類(lèi)有哪些2023-05-05
零基礎(chǔ)寫(xiě)Java知乎爬蟲(chóng)之準(zhǔn)備工作
上個(gè)系列我們從易到難介紹了如何使用python編寫(xiě)爬蟲(chóng),小伙伴們反響挺大,這個(gè)系列我們來(lái)研究下使用Java編寫(xiě)知乎爬蟲(chóng),小伙伴們可以對(duì)比這看下。2014-11-11
JAVA實(shí)現(xiàn)較完善的布隆過(guò)濾器的示例代碼
這篇文章主要介紹了JAVA實(shí)現(xiàn)較完善的布隆過(guò)濾器的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10

