SpringSecurity6.0 如何通過JWTtoken進(jìn)行認(rèn)證授權(quán)
之前寫過一個文章,從SpringSecurity 5.x升級到6.0,當(dāng)時是為了配合公司的大版本升級做的,里面的各項配置都是前人留下來的,其實沒有花時間進(jìn)行研究SpringSecurity的工作機(jī)制?,F(xiàn)在新東家有一個簡單的系統(tǒng)要搭建,用戶的認(rèn)證授權(quán)流程也比較簡單,通過用戶/密碼進(jìn)行登錄,登錄后生成JWT token返回給前端,后續(xù)認(rèn)證通過token進(jìn)行,就把SpringSecurity重新?lián)炝似饋?,搭建整個系統(tǒng)的安全認(rèn)證框架。
項目依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- jwt token相關(guān)依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> <dependencies>
項目后端整體還是通過Springboot來搭建,Springboot3.0中把JWT相關(guān)的依賴都整合到了spring-boot-starter-oauth2-resource-server中,無需再單獨指定
認(rèn)證
首先我們先完成通過賬號密碼進(jìn)行登錄相關(guān)代碼
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Value("${jwt.public.key}")
RSAPublicKey key;
@Value("${jwt.private.key}")
RSAPrivateKey priv;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeHttpRequests((authorize) -> authorize
.anyRequest().authenticated()
)
.csrf((csrf) -> csrf.ignoringRequestMatchers("/token"))
.httpBasic(Customizer.withDefaults())
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)
.sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.exceptionHandling((exceptions) -> exceptions
.authenticationEntryPoint(new BearerTokenAuthenticationEntryPoint())
.accessDeniedHandler(new BearerTokenAccessDeniedHandler())
);
// @formatter:on
return http.build();
}
@Bean
UserDetailsService users() {
// @formatter:off
return new InMemoryUserDetailsManager(
User.withUsername("user")
.password("{noop}password")
.authorities("app")
.build()
);
// @formatter:on
}
@Bean
JwtDecoder jwtDecoder() {
return NimbusJwtDecoder.withPublicKey(this.key).build();
}
@Bean
JwtEncoder jwtEncoder() {
JWK jwk = new RSAKey.Builder(this.key).privateKey(this.priv).build();
JWKSource<SecurityContext> jwks = new ImmutableJWKSet<>(new JWKSet(jwk));
return new NimbusJwtEncoder(jwks);
}
}這里關(guān)注幾個重點:
HttpSecurity#httpBasic,這個方法表明通過基于HTTP Basic認(rèn)證協(xié)議anyRequest().authenticated()表明所有請求都需要經(jīng)過認(rèn)證UserDetailsService,這里創(chuàng)建了一個僅存在于內(nèi)存中的用戶,用戶名和密碼是user/password,密碼中添加的前綴{noop}和userDetailService的作用我們稍后再說oauth2ResourceServer設(shè)置jwt token相關(guān)的配置,Spring推薦情況是配置一個第三方的校驗服務(wù),我們這里為了簡化將相關(guān)的生成和校驗都在本地進(jìn)行。
UserDetailService
public interface UserDetailsService {
/**
* Locates the user based on the username. In the actual implementation, the search
* may possibly be case sensitive, or case insensitive depending on how the
* implementation instance is configured. In this case, the <code>UserDetails</code>
* object that comes back may have a username that is of a different case than what
* was actually requested..
* @param username the username identifying the user whose data is required.
* @return a fully populated user record (never <code>null</code>)
* @throws UsernameNotFoundException if the user could not be found or the user has no
* GrantedAuthority
*/
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
}這個接口里只定義了一個方法,loadUserByUsername在通過用戶名/密碼進(jìn)行認(rèn)證時,需要通過來判斷用戶是否存在,在生產(chǎn)中,我們可以根據(jù)自己的需要通過數(shù)據(jù)庫等獲取用戶信息。
拿到用戶信息之后,要怎么校驗密碼呢?SpringSecurity提供了另外一個接口PasswordEncoder進(jìn)行密碼的編碼和校驗,

這里提供了非常多的實現(xiàn)方式,默認(rèn)情況下Spring會加載DelegatingPasswordEncoder,同時將其他的實現(xiàn)都包含進(jìn)去,那在進(jìn)行密碼校驗的時候要匹配哪一個Encoder呢,這里{noop}password中的前綴就發(fā)揮作用了,{noop}表明使用NoOpPasswordEncoder進(jìn)行處理,即不僅限任何編碼處理,直接通過明文進(jìn)行對比,這里當(dāng)然不符合安全要求,在實際工作中我們根據(jù)需要直接指定一個Encoder即可
@Bean
PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}生成JWT token
@RestController
public class TokenController {
@Autowired
JwtEncoder encoder;
@PostMapping("/token")
public String token(Authentication authentication) {
Instant now = Instant.now();
long expiry = 36000L;
// @formatter:off
String scope = authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.joining(" "));
JwtClaimsSet claims = JwtClaimsSet.builder()
.issuer("self")
.issuedAt(now)
.expiresAt(now.plusSeconds(expiry))
.subject(authentication.getName())
.claim("scope", scope)
.build();
// @formatter:on
return this.encoder.encode(JwtEncoderParameters.from(claims)).getTokenValue();
}
}這里有一個小提示,我們在創(chuàng)建UserDetail的時候可以設(shè)置#authorities()和#roles(),但是最終都會設(shè)置到authorities中,這兩個在當(dāng)今的SpringSecurity中實際上是一個東西,所以我們在Authentication中也只有getAuthorities()這一個方法
進(jìn)行測試
curl -XPOST user:password@localhost:8080/token
然后能夠得到類似的返回
eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJzZWxmIiwic3ViIjoidXNlciIsImV4cCI6MTYwNDA0MzA1MSwiaWF0IjoxNjA0MDA3MDUxfQ.yDF_JgSwl5sk21CF7AE1AYbYzRd5YYqe3MIgSWpgN0t2UqsjaaEDhmmICKizt-_0iZy8nkEpNnvgqv5bOHDhs7AXlYS1pg8dgPKuyfkhyVIKa3DhuGyb7tFjwJxHpr128BXf1Dbq-p7Njy46tbKsZhP5zGTjdXlqlAhR4Bl5Fxaxr7D0gdTVBVTlUp9DCy6l-pTBpsvHxShkjXJ0GHVpIZdB-c2e_K9PfTW5MDPcHekG9djnWPSEy-fRvKzTsyVFhdy-X3NXQWWkjFv9bNarV-bhxMlzqhujuaeXJGEqUZlkhBxTsqFr1N7XVcmhs3ECdjEyun2fUSge4BoC7budsQ
然后我們把token配置到環(huán)境變量中
export TOKEN=`curl -XPOST user:password@localhost:8080/token`
請求另外一個接口
curl -H "Authorization: Bearer $TOKEN" localhost:8080 && echo
Hello, user!
權(quán)限控制
在完成認(rèn)證后,后續(xù)我們可以繼續(xù)進(jìn)行授權(quán)相關(guān)的校驗工作,SpringSecurity提供兩種授權(quán)校驗的方式
- 基于http請求的方式,包括路徑匹配、請求方法匹配等,
- 基于方法的控制,通過
@PreAuthorize等注解,在方法上進(jìn)行更細(xì)粒度的控制,我采用了這一種方式
@PreAuthorize("hasAuthority('SCOPE_ADMIN')")//JWT token解析后會加一個前綴'scope'
@GetMapping("/admin")
public String admin(Authentication authentication){
return authentication.getAuthorities().toString();
}默認(rèn)情況下,Authority中的內(nèi)容會比你生成token時多加一個前綴SCOPE_,當(dāng)然你也可以通過配置進(jìn)行更改。
小結(jié)
這里簡單介紹了一下認(rèn)證和授權(quán)的配置,實際上SpringSecurity要遠(yuǎn)比這些要復(fù)雜的多,有更深入的需求可以參考官方文檔或者源碼
這里推薦一下自己的項目地址,已經(jīng)把用戶配置到h2數(shù)據(jù)庫中
https://gitee.com/xiiiao/hello-spring-security
到此這篇關(guān)于SpringSecurity6.0 通過JWTtoken進(jìn)行認(rèn)證授權(quán)的文章就介紹到這了,更多相關(guān)SpringSecurity認(rèn)證授權(quán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springSecurity自定義登錄接口和JWT認(rèn)證過濾器的流程
- SpringSecurity+jwt+captcha登錄認(rèn)證授權(quán)流程總結(jié)
- SpringSecurity+Redis+Jwt實現(xiàn)用戶認(rèn)證授權(quán)
- SpringBoot整合SpringSecurity和JWT和Redis實現(xiàn)統(tǒng)一鑒權(quán)認(rèn)證
- SpringSecurity+jwt+redis基于數(shù)據(jù)庫登錄認(rèn)證的實現(xiàn)
- SpringBoot+SpringSecurity+JWT實現(xiàn)系統(tǒng)認(rèn)證與授權(quán)示例
- SpringBoot整合SpringSecurity實現(xiàn)JWT認(rèn)證的項目實踐
- SpringSecurity整合jwt權(quán)限認(rèn)證的全流程講解
- SpringSecurity構(gòu)建基于JWT的登錄認(rèn)證實現(xiàn)
- SpringSecurity JWT基于令牌的無狀態(tài)認(rèn)證實現(xiàn)
相關(guān)文章
Java關(guān)鍵字instanceof用法及實現(xiàn)策略
instanceof 運算符是用來在運行時判斷對象是否是指定類及其父類的一個實例。這篇文章主要介紹了Java關(guān)鍵字instanceof用法解析,需要的朋友可以參考下2020-08-08
Spring整合CXF webservice restful實例詳解
這篇文章主要為大家詳細(xì)介紹了Spring整合CXF webservice restful的實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Mybatis關(guān)聯(lián)查詢遇到的坑-無主鍵的關(guān)聯(lián)數(shù)據(jù)去重問題
這篇文章主要介紹了Mybatis關(guān)聯(lián)查詢遇到的坑-無主鍵的關(guān)聯(lián)數(shù)據(jù)去重問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot整合Echarts實現(xiàn)數(shù)據(jù)大屏
這篇文章給大家介紹了三步實現(xiàn)SpringBoot全局日志記錄,整合Echarts實現(xiàn)數(shù)據(jù)大屏,文中通過代碼示例給大家介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2024-03-03
Java實現(xiàn)將PPT轉(zhuǎn)為OFD過程詳解
本文將通過Java后端程序代碼展示如何實現(xiàn)將PPT幻燈片轉(zhuǎn)成OFD格式,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定的幫助,需要的可以參考一下2022-01-01
自定義@RequestBody注解如何獲取JSON數(shù)據(jù)
這篇文章主要介紹了自定義@RequestBody注解如何獲取JSON數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

