楔子
本文適合: 對Spring Security有一點了解或者跑過簡單demo但是對整體運行流程不明白的同學(xué),對SpringSecurity有興趣的也可以當(dāng)作你們的入門教程,示例代碼中也有很多注釋。
大家在做系統(tǒng)的時候,一般做的第一個模塊就是認(rèn)證與授權(quán)模塊,因為這是一個系統(tǒng)的入口,也是一個系統(tǒng)最重要最基礎(chǔ)的一環(huán),在認(rèn)證與授權(quán)服務(wù)設(shè)計搭建好了之后,剩下的模塊才得以安全訪問。
市面上一般做認(rèn)證授權(quán)的框架就是shiro和Spring Security,也有大部分公司選擇自己研制。出于之前看過很多Spring Security的入門教程,但都覺得講的不是太好,所以我這兩天在自己鼓搗Spring Security的時候萌生了分享一下的想法,希望可以幫助到有興趣的人。
Spring Security框架我們主要用它就是解決一個認(rèn)證授權(quán)功能,所以我的文章主要會分為兩部分:
- 第一部分認(rèn)證(本篇)
- 第二部分授權(quán)(放在下一篇)
我會為大家用一個Spring Security + JWT + 緩存的一個demo來展現(xiàn)我要講的東西,畢竟腦子的東西要體現(xiàn)在具體事物上才可以更直觀地讓大家去了解去認(rèn)識。
學(xué)習(xí)一件新事物的時候,我推薦使用自頂向下的學(xué)習(xí)方法,這樣可以更好的認(rèn)識新事物,而不是盲人摸象。
注:只涉及到用戶認(rèn)證授權(quán)不涉及oauth2之類的第三方授權(quán)。
1. SpringSecurity的工作流程
想上手 Spring Security 一定要先了解它的工作流程,因為它不像工具包一樣,拿來即用,必須要對它有一定的了解,再根據(jù)它的用法進(jìn)行自定義操作。
我們可以先來看看它的工作流程:
在Spring Security的官方文檔上有這么一句話:
Spring Security’s web infrastructure is based entirely on standard servlet filters.
Spring Security 的web基礎(chǔ)是Filters。
這句話展示了Spring Security的設(shè)計思想:即通過一層層的Filters來對web請求做處理。
放到真實的Spring Security中,用文字表述的話可以這樣說:
一個web請求會經(jīng)過一條過濾器鏈,在經(jīng)過過濾器鏈的過程中會完成認(rèn)證與授權(quán),如果中間發(fā)現(xiàn)這條請求未認(rèn)證或者未授權(quán),會根據(jù)被保護(hù)API的權(quán)限去拋出異常,然后由異常處理器去處理這些異常。
用圖片表述的話可以這樣畫,這是我在百度找到的一張圖片:
[圖片上傳失敗...(image-d865ec-1619762261565)]
如上圖,一個請求想要訪問到API就會以從左到右的形式經(jīng)過藍(lán)線框框里面的過濾器,其中綠色部分是我們本篇主要講的負(fù)責(zé)認(rèn)證的過濾器,藍(lán)色部分負(fù)責(zé)異常處理,橙色部分則是負(fù)責(zé)授權(quán)。
圖中的這兩個綠色過濾器我們今天不會去說,因為這是Spring Security對form表單認(rèn)證和Basic認(rèn)證內(nèi)置的兩個Filter,而我們的demo是JWT認(rèn)證方式所以用不上。
如果你用過Spring Security就應(yīng)該知道配置中有兩個叫formLogin和httpBasic的配置項,在配置中打開了它倆就對應(yīng)著打開了上面的過濾器。
- formLogin對應(yīng)著你form表單認(rèn)證方式,即UsernamePasswordAuthenticationFilter。
- httpBasic對應(yīng)著Basic認(rèn)證方式,即BasicAuthenticationFilter。
換言之,你配置了這兩種認(rèn)證方式,過濾器鏈中才會加入它們,否則它們是不會被加到過濾器鏈中去的。
因為Spring Security自帶的過濾器中是沒有針對JWT這種認(rèn)證方式的,所以我們的demo中會寫一個JWT的認(rèn)證過濾器,然后放在綠色的位置進(jìn)行認(rèn)證工作。
2. SpringSecurity的重要概念
知道了Spring Security的大致工作流程之后,我們還需要知道一些非常重要的概念也可以說是組件:
- SecurityContext:上下文對象,Authentication對象會放在里面。
- SecurityContextHolder:用于拿到上下文對象的靜態(tài)工具類。
- Authentication:認(rèn)證接口,定義了認(rèn)證對象的數(shù)據(jù)形式。
- AuthenticationManager:用于校驗Authentication,返回一個認(rèn)證完成后的Authentication對象。
1.SecurityContext
上下文對象,認(rèn)證后的數(shù)據(jù)就放在這里面,接口定義如下:
public interface SecurityContext extends Serializable {
// 獲取Authentication對象
Authentication getAuthentication();
// 放入Authentication對象
void setAuthentication(Authentication authentication);
}
復(fù)制代碼
這個接口里面只有兩個方法,其主要作用就是get or set Authentication。
2. SecurityContextHolder
public class SecurityContextHolder {
public static void clearContext() {
strategy.clearContext();
}
public static SecurityContext getContext() {
return strategy.getContext();
}
public static void setContext(SecurityContext context) {
strategy.setContext(context);
}
}
復(fù)制代碼
可以說是SecurityContext的工具類,用于get or set or clear SecurityContext,默認(rèn)會把數(shù)據(jù)都存儲到當(dāng)前線程中。
3. Authentication
public interface Authentication extends Principal, Serializable {
Collection<? extends GrantedAuthority> getAuthorities();
Object getCredentials();
Object getDetails();
Object getPrincipal();
boolean isAuthenticated();
void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}
復(fù)制代碼
這幾個方法效果如下:
- getAuthorities: 獲取用戶權(quán)限,一般情況下獲取到的是用戶的角色信息。
- getCredentials: 獲取證明用戶認(rèn)證的信息,通常情況下獲取到的是密碼等信息。
- getDetails: 獲取用戶的額外信息,(這部分信息可以是我們的用戶表中的信息)。
- getPrincipal: 獲取用戶身份信息,在未認(rèn)證的情況下獲取到的是用戶名,在已認(rèn)證的情況下獲取到的是 UserDetails。
- isAuthenticated: 獲取當(dāng)前 Authentication 是否已認(rèn)證。
- setAuthenticated: 設(shè)置當(dāng)前 Authentication 是否已認(rèn)證(true or false)。
Authentication只是定義了一種在SpringSecurity進(jìn)行認(rèn)證過的數(shù)據(jù)的數(shù)據(jù)形式應(yīng)該是怎么樣的,要有權(quán)限,要有密碼,要有身份信息,要有額外信息。
4. AuthenticationManager
public interface AuthenticationManager {
// 認(rèn)證方法
Authentication authenticate(Authentication authentication)
throws AuthenticationException;
}
復(fù)制代碼
AuthenticationManager定義了一個認(rèn)證方法,它將一個未認(rèn)證的Authentication傳入,返回一個已認(rèn)證的Authentication,默認(rèn)使用的實現(xiàn)類為:ProviderManager。
接下來大家可以構(gòu)思一下如何將這四個部分,串聯(lián)起來,構(gòu)成Spring Security進(jìn)行認(rèn)證的流程:
1. 先是一個請求帶著身份信息進(jìn)來
2. 經(jīng)過AuthenticationManager的認(rèn)證,
3. 再通過SecurityContextHolder獲取SecurityContext,
4. 最后將認(rèn)證后的信息放入到SecurityContext。
3. 代碼前的準(zhǔn)備工作
真正開始講訴我們的認(rèn)證代碼之前,我們首先需要導(dǎo)入必要的依賴,數(shù)據(jù)庫相關(guān)的依賴可以自行選擇什么JDBC框架,我這里用的是國人二次開發(fā)的myabtis-plus。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
復(fù)制代碼
接著,我們需要定義幾個必須的組件。
由于我用的Spring-Boot是2.X所以必須要我們自己定義一個加密器:
1. 定義加密器Bean
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
復(fù)制代碼
這個Bean是不必可少的,Spring Security在認(rèn)證操作時會使用我們定義的這個加密器,如果沒有則會出現(xiàn)異常。
2. 定義AuthenticationManager
@Bean
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
復(fù)制代碼
這里將Spring Security自帶的authenticationManager聲明成Bean,聲明它的作用是用它幫我們進(jìn)行認(rèn)證操作,調(diào)用這個Bean的authenticate方法會由Spring Security自動幫我們做認(rèn)證。
3. 實現(xiàn)UserDetailsService
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
private UserService userService;
@Autowired
private RoleInfoService roleInfoService;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
log.debug("開始登陸驗證,用戶名為: {}",s);
// 根據(jù)用戶名驗證用戶
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(UserInfo::getLoginAccount,s);
UserInfo userInfo = userService.getOne(queryWrapper);
if (userInfo == null) {
throw new UsernameNotFoundException("用戶名不存在,登陸失敗。");
}
// 構(gòu)建UserDetail對象
UserDetail userDetail = new UserDetail();
userDetail.setUserInfo(userInfo);
List<RoleInfo> roleInfoList = roleInfoService.listRoleByUserId(userInfo.getUserId());
userDetail.setRoleInfoList(roleInfoList);
return userDetail;
}
}
復(fù)制代碼
實現(xiàn)UserDetailsService的抽象方法并返回一個UserDetails對象,認(rèn)證過程中SpringSecurity會調(diào)用這個方法訪問數(shù)據(jù)庫進(jìn)行對用戶的搜索,邏輯什么都可以自定義,無論是從數(shù)據(jù)庫中還是從緩存中,但是我們需要將我們查詢出來的用戶信息和權(quán)限信息組裝成一個UserDetails返回。
UserDetails 也是一個定義了數(shù)據(jù)形式的接口,用于保存我們從數(shù)據(jù)庫中查出來的數(shù)據(jù),其功能主要是驗證賬號狀態(tài)和獲取權(quán)限,具體實現(xiàn)可以查閱我倉庫的代碼。
4. TokenUtil
由于我們是JWT的認(rèn)證模式,所以我們也需要一個幫我們操作Token的工具類,一般來說它具有以下三個方法就夠了:
- 創(chuàng)建token
- 驗證token
- 反解析token中的信息
在下文我的代碼里面,JwtProvider充當(dāng)了Token工具類的角色,具體實現(xiàn)可以查閱我倉庫的代碼。
4. ?代碼中的具體實現(xiàn)
有了前面的講解之后,大家應(yīng)該都知道用SpringSecurity做JWT認(rèn)證需要我們自己寫一個過濾器來做JWT的校驗,然后將這個過濾器放到綠色部分。
在我們編寫這個過濾器之前,我們還需要進(jìn)行一個認(rèn)證操作,因為我們要先訪問認(rèn)證接口拿到token,才能把token放到請求頭上,進(jìn)行接下來請求。
如果你不太明白,不要緊,先接著往下看我會在這節(jié)結(jié)束再次梳理一下。
1. 認(rèn)證方法
訪問一個系統(tǒng),一般最先訪問的是認(rèn)證方法,這里我寫了最簡略的認(rèn)證需要的幾個步驟,因為實際系統(tǒng)中我們還要寫登錄記錄啊,前臺密碼解密啊這些操作。
@Override
public ApiResult login(String loginAccount, String password) {
// 1 創(chuàng)建UsernamePasswordAuthenticationToken
UsernamePasswordAuthenticationToken usernameAuthentication = new UsernamePasswordAuthenticationToken(loginAccount, password);
// 2 認(rèn)證
Authentication authentication = this.authenticationManager.authenticate(usernameAuthentication);
// 3 保存認(rèn)證信息
SecurityContextHolder.getContext().setAuthentication(authentication);
// 4 生成自定義token
UserDetail userDetail = (UserDetail) authentication.getPrincipal();
AccessToken accessToken = jwtProvider.createToken((UserDetails) authentication.getPrincipal());
// 5 放入緩存
caffeineCache.put(CacheName.USER, userDetail.getUsername(), userDetail);
return ApiResult.ok(accessToken);
}
復(fù)制代碼
這里一共五個步驟,大概只有前四步是比較陌生的:
- 傳入用戶名和密碼創(chuàng)建了一個UsernamePasswordAuthenticationToken對象,這是我們前面說過的Authentication的實現(xiàn)類,傳入用戶名和密碼做構(gòu)造參數(shù),這個對象就是我們創(chuàng)建出來的未認(rèn)證的Authentication對象。
- 使用我們先前已經(jīng)聲明過的Bean-authenticationManager調(diào)用它的authenticate方法進(jìn)行認(rèn)證,返回一個認(rèn)證完成的Authentication對象。
- 認(rèn)證完成沒有出現(xiàn)異常,就會走到第三步,使用SecurityContextHolder獲取SecurityContext之后,將認(rèn)證完成之后的Authentication對象,放入上下文對象。
- 從Authentication對象中拿到我們的UserDetails對象,之前我們說過,認(rèn)證后的Authentication對象調(diào)用它的getPrincipal()方法就可以拿到我們先前數(shù)據(jù)庫查詢后組裝出來的UserDetails對象,然后創(chuàng)建token。
- 把UserDetails對象放入緩存中,方便后面過濾器使用。
這樣的話就算完成了,感覺上很簡單,因為主要認(rèn)證操作都會由
authenticationManager.authenticate()幫我們完成。
接下來我們可以看看源碼,從中窺得Spring Security是如何幫我們做這個認(rèn)證的(省略了一部分):
// AbstractUserDetailsAuthenticationProvider
public Authentication authenticate(Authentication authentication){
// 校驗未認(rèn)證的Authentication對象里面有沒有用戶名
String username = (authentication.getPrincipal() == null) ? "NONE_PROVIDED"
: authentication.getName();
boolean cacheWasUsed = true;
// 從緩存中去查用戶名為XXX的對象
UserDetails user = this.userCache.getUserFromCache(username);
// 如果沒有就進(jìn)入到這個方法
if (user == null) {
cacheWasUsed = false;
try {
// 調(diào)用我們重寫UserDetailsService的loadUserByUsername方法
// 拿到我們自己組裝好的UserDetails對象
user = retrieveUser(username,
(UsernamePasswordAuthenticationToken) authentication);
}
catch (UsernameNotFoundException notFound) {
logger.debug("User '" + username + "' not found");
if (hideUserNotFoundExceptions) {
throw new BadCredentialsException(messages.getMessage(
"AbstractUserDetailsAuthenticationProvider.badCredentials",
"Bad credentials"));
}
else {
throw notFound;
}
}
Assert.notNull(user,
"retrieveUser returned null - a violation of the interface contract");
}
try {
// 校驗賬號是否禁用
preAuthenticationChecks.check(user);
// 校驗數(shù)據(jù)庫查出來的密碼,和我們傳入的密碼是否一致
additionalAuthenticationChecks(user,
(UsernamePasswordAuthenticationToken) authentication);
}
}
復(fù)制代碼
看了源碼之后你會發(fā)現(xiàn)和我們平常寫的一樣,其主要邏輯也是查數(shù)據(jù)庫然后對比密碼。
登錄之后效果如下:
[圖片上傳失敗...(image-6860a9-1619762261564)]
我們返回token之后,下次請求其他API的時候就要在請求頭中帶上這個token,都按照J(rèn)WT的標(biāo)準(zhǔn)來做就可以。
2. JWT過濾器
有了token之后,我們要把過濾器放在過濾器鏈中,用于解析token,因為我們沒有session,所以我們每次去辨別這是哪個用戶的請求的時候,都是根據(jù)請求中的token來解析出來當(dāng)前是哪個用戶。
所以我們需要一個過濾器去攔截所有請求,前文我們也說過,這個過濾器我們會放在綠色部分用來替代
UsernamePasswordAuthenticationFilter,所以我們新建一個JwtAuthenticationTokenFilter,然后將它注冊為Bean,并在編寫配置文件的時候需要加上這個:
@Bean
public JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter() {
return new JwtAuthenticationTokenFilter();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(jwtAuthenticationTokenFilter(),
UsernamePasswordAuthenticationFilter.class);
}
復(fù)制代碼
addFilterBefore的語義是添加一個Filter到XXXFilter之前,放在這里就是把
JwtAuthenticationTokenFilter放在UsernamePasswordAuthenticationFilter之前,因為filter的執(zhí)行也是有順序的,我們必須要把我們的filter放在過濾器鏈中綠色的部分才會起到自動認(rèn)證的效果。
接下來我們可以看看
JwtAuthenticationTokenFilter的具體實現(xiàn)了:
@Override
protected void doFilterInternal(@NotNull HttpServletRequest request,
@NotNull HttpServletResponse response,
@NotNull FilterChain chain) throws ServletException, IOException {
log.info("JWT過濾器通過校驗請求頭token進(jìn)行自動登錄...");
// 拿到Authorization請求頭內(nèi)的信息
String authToken = jwtProvider.getToken(request);
// 判斷一下內(nèi)容是否為空且是否為(Bearer )開頭
if (StrUtil.isNotEmpty(authToken) && authToken.startsWith(jwtProperties.getTokenPrefix())) {
// 去掉token前綴(Bearer ),拿到真實token
authToken = authToken.substring(jwtProperties.getTokenPrefix().length());
// 拿到token里面的登錄賬號
String loginAccount = jwtProvider.getSubjectFromToken(authToken);
if (StrUtil.isNotEmpty(loginAccount) && SecurityContextHolder.getContext().getAuthentication() == null) {
// 緩存里查詢用戶,不存在需要重新登陸。
UserDetail userDetails = caffeineCache.get(CacheName.USER, loginAccount, UserDetail.class);
// 拿到用戶信息后驗證用戶信息與token
if (userDetails != null && jwtProvider.validateToken(authToken, userDetails)) {
// 組裝authentication對象,構(gòu)造參數(shù)是Principal Credentials 與 Authorities
// 后面的攔截器里面會用到 grantedAuthorities 方法
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
// 將authentication信息放入到上下文對象中
SecurityContextHolder.getContext().setAuthentication(authentication);
log.info("JWT過濾器通過校驗請求頭token自動登錄成功, user : {}", userDetails.getUsername());
}
}
}
chain.doFilter(request, response);
}
復(fù)制代碼
代碼里步驟雖然說得很詳細(xì)了,但是可能因為代碼過長不利于閱讀,我還是簡單說說,也可以直接去倉庫查看源碼:
- 拿到Authorization請求頭對應(yīng)的token信息
- 去掉token的頭部(Bearer )
- 解析token,拿到我們放在里面的登陸賬號
- 因為我們之前登陸過,所以我們直接從緩存里面拿我們的UserDetail信息即可
- 查看是否UserDetail為null,以及查看token是否過期,UserDetail用戶名與token中的是否一致。
- 組裝一個authentication對象,把它放在上下文對象中,這樣后面的過濾器看到我們上下文對象中有authentication對象,就相當(dāng)于我們已經(jīng)認(rèn)證過了。
這樣的話,每一個帶有正確token的請求進(jìn)來之后,都會找到它的賬號信息,并放在上下文對象中,我們可以使用SecurityContextHolder很方便的拿到上下文對象中的Authentication對象。
完成之后,啟動我們的demo,可以看到過濾器鏈中有以下過濾器,其中我們自定義的是第5個:
[圖片上傳失敗...(image-a64412-1619762261563)]
就醬,我們登錄完了之后獲取到的賬號信息與角色信息我們都會放到緩存中,當(dāng)帶著token的請求來到時,我們就把它從緩存中拿出來,再次放到上下文對象中去。
結(jié)合認(rèn)證方法,我們的邏輯鏈就變成了:
登錄拿到token請求帶上tokenJWT過濾器攔截校驗token將從緩存中查出來的對象放到上下文中
這樣之后,我們認(rèn)證的邏輯就算完成了。
4. 代碼優(yōu)化
認(rèn)證和JWT過濾器完成后,這個JWT的項目其實就可以跑起來了,可以實現(xiàn)我們想要的效果,如果想讓程序更健壯,我們還需要再加一些輔助功能,讓代碼更友好。
1. 認(rèn)證失敗處理器
當(dāng)用戶未登錄或者token解析失敗時會觸發(fā)這個處理器,返回一個非法訪問的結(jié)果。
2. 權(quán)限不足處理器
[圖片上傳失敗...(image-8d232-1619762261563)]
當(dāng)用戶本身權(quán)限不滿足所訪問API需要的權(quán)限時,觸發(fā)這個處理器,返回一個權(quán)限不足的結(jié)果。
3. 退出方法
用戶退出一般就是清除掉上下文對象和緩存就行了,你也可以做一下附加操作,這兩步是必須的。
4. token刷新
[圖片上傳失敗...(image-ffe51a-1619762261563)]
JWT的項目token刷新也是必不可少的,這里刷新token的主要方法放在了token工具類里面,刷新完了把緩存重載一遍就行了,因為緩存是有有效期的,重新put可以重置失效時間。
后記
這篇文我從上周日就開始構(gòu)思了,為了能講的老嫗?zāi)芙?,修修改改了幾遍才發(fā)出來。
Spring Security的上手的確有點難度,在我第一次去了解它的時候看的是尚硅谷的教程,那個視頻的講師拿它和Thymeleaf結(jié)合,這就導(dǎo)致網(wǎng)上也有很多博客去講Spring Security的時候也是這種方式,而沒有去關(guān)注前后端分離。
也有教程做過濾器的時候是直接繼承
UsernamePasswordAuthenticationFilter,這樣的方法也是可行的,不過我們了解了整體的運行流程之后你就知道沒必要這樣做,不需要去繼承XXX,只要寫個過濾器然后放在那個位置就可以了。
好了,認(rèn)證篇結(jié)束后,下篇就是動態(tài)鑒權(quán)了,這是我在掘金的第一篇文,我的第一次知識輸出,希望大家持續(xù)關(guān)注。
你們的每個點贊收藏與評論都是對我知識輸出的莫大肯定,如果有文中有什么錯誤或者疑點或者對我的指教都可以在評論區(qū)下方留言,一起討論。