SpringSecurity6.0 如何通過(guò)JWTtoken進(jìn)行認(rèn)證授權(quán)
之前寫(xiě)過(guò)一個(gè)文章,從SpringSecurity 5.x升級(jí)到6.0,當(dāng)時(shí)是為了配合公司的大版本升級(jí)做的,里面的各項(xiàng)配置都是前人留下來(lái)的,其實(shí)沒(méi)有花時(shí)間進(jìn)行研究SpringSecurity的工作機(jī)制?,F(xiàn)在新東家有一個(gè)簡(jiǎn)單的系統(tǒng)要搭建,用戶(hù)的認(rèn)證授權(quán)流程也比較簡(jiǎn)單,通過(guò)用戶(hù)/密碼進(jìn)行登錄,登錄后生成JWT token返回給前端,后續(xù)認(rèn)證通過(guò)token進(jìn)行,就把SpringSecurity重新?lián)炝似饋?lái),搭建整個(gè)系統(tǒng)的安全認(rèn)證框架。
項(xiàng)目依賴(lài)
<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)依賴(lài)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> <dependencies>
項(xiàng)目后端整體還是通過(guò)Springboot來(lái)搭建,Springboot3.0中把JWT相關(guān)的依賴(lài)都整合到了spring-boot-starter-oauth2-resource-server中,無(wú)需再單獨(dú)指定
認(rèn)證
首先我們先完成通過(guò)賬號(hào)密碼進(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)注幾個(gè)重點(diǎn):
HttpSecurity#httpBasic,這個(gè)方法表明通過(guò)基于HTTP Basic認(rèn)證協(xié)議anyRequest().authenticated()表明所有請(qǐng)求都需要經(jīng)過(guò)認(rèn)證UserDetailsService,這里創(chuàng)建了一個(gè)僅存在于內(nèi)存中的用戶(hù),用戶(hù)名和密碼是user/password,密碼中添加的前綴{noop}和userDetailService的作用我們稍后再說(shuō)oauth2ResourceServer設(shè)置jwt token相關(guān)的配置,Spring推薦情況是配置一個(gè)第三方的校驗(yàn)服務(wù),我們這里為了簡(jiǎn)化將相關(guān)的生成和校驗(yà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;
}這個(gè)接口里只定義了一個(gè)方法,loadUserByUsername在通過(guò)用戶(hù)名/密碼進(jìn)行認(rèn)證時(shí),需要通過(guò)來(lái)判斷用戶(hù)是否存在,在生產(chǎn)中,我們可以根據(jù)自己的需要通過(guò)數(shù)據(jù)庫(kù)等獲取用戶(hù)信息。
拿到用戶(hù)信息之后,要怎么校驗(yàn)密碼呢?SpringSecurity提供了另外一個(gè)接口PasswordEncoder進(jìn)行密碼的編碼和校驗(yàn),

這里提供了非常多的實(shí)現(xiàn)方式,默認(rèn)情況下Spring會(huì)加載DelegatingPasswordEncoder,同時(shí)將其他的實(shí)現(xiàn)都包含進(jìn)去,那在進(jìn)行密碼校驗(yàn)的時(shí)候要匹配哪一個(gè)Encoder呢,這里{noop}password中的前綴就發(fā)揮作用了,{noop}表明使用NoOpPasswordEncoder進(jìn)行處理,即不僅限任何編碼處理,直接通過(guò)明文進(jìn)行對(duì)比,這里當(dāng)然不符合安全要求,在實(shí)際工作中我們根據(jù)需要直接指定一個(gè)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();
}
}這里有一個(gè)小提示,我們?cè)趧?chuàng)建UserDetail的時(shí)候可以設(shè)置#authorities()和#roles(),但是最終都會(huì)設(shè)置到authorities中,這兩個(gè)在當(dāng)今的SpringSecurity中實(shí)際上是一個(gè)東西,所以我們?cè)?code>Authentication中也只有getAuthorities()這一個(gè)方法
進(jìn)行測(cè)試
curl -XPOST user:password@localhost:8080/token
然后能夠得到類(lèi)似的返回
eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJzZWxmIiwic3ViIjoidXNlciIsImV4cCI6MTYwNDA0MzA1MSwiaWF0IjoxNjA0MDA3MDUxfQ.yDF_JgSwl5sk21CF7AE1AYbYzRd5YYqe3MIgSWpgN0t2UqsjaaEDhmmICKizt-_0iZy8nkEpNnvgqv5bOHDhs7AXlYS1pg8dgPKuyfkhyVIKa3DhuGyb7tFjwJxHpr128BXf1Dbq-p7Njy46tbKsZhP5zGTjdXlqlAhR4Bl5Fxaxr7D0gdTVBVTlUp9DCy6l-pTBpsvHxShkjXJ0GHVpIZdB-c2e_K9PfTW5MDPcHekG9djnWPSEy-fRvKzTsyVFhdy-X3NXQWWkjFv9bNarV-bhxMlzqhujuaeXJGEqUZlkhBxTsqFr1N7XVcmhs3ECdjEyun2fUSge4BoC7budsQ
然后我們把token配置到環(huán)境變量中
export TOKEN=`curl -XPOST user:password@localhost:8080/token`
請(qǐng)求另外一個(gè)接口
curl -H "Authorization: Bearer $TOKEN" localhost:8080 && echo
Hello, user!
權(quán)限控制
在完成認(rèn)證后,后續(xù)我們可以繼續(xù)進(jìn)行授權(quán)相關(guān)的校驗(yàn)工作,SpringSecurity提供兩種授權(quán)校驗(yàn)的方式
- 基于http請(qǐng)求的方式,包括路徑匹配、請(qǐng)求方法匹配等,
- 基于方法的控制,通過(guò)
@PreAuthorize等注解,在方法上進(jìn)行更細(xì)粒度的控制,我采用了這一種方式
@PreAuthorize("hasAuthority('SCOPE_ADMIN')")//JWT token解析后會(huì)加一個(gè)前綴'scope'
@GetMapping("/admin")
public String admin(Authentication authentication){
return authentication.getAuthorities().toString();
}默認(rèn)情況下,Authority中的內(nèi)容會(huì)比你生成token時(shí)多加一個(gè)前綴SCOPE_,當(dāng)然你也可以通過(guò)配置進(jìn)行更改。
小結(jié)
這里簡(jiǎn)單介紹了一下認(rèn)證和授權(quán)的配置,實(shí)際上SpringSecurity要遠(yuǎn)比這些要復(fù)雜的多,有更深入的需求可以參考官方文檔或者源碼
這里推薦一下自己的項(xiàng)目地址,已經(jīng)把用戶(hù)配置到h2數(shù)據(jù)庫(kù)中
https://gitee.com/xiiiao/hello-spring-security
到此這篇關(guān)于SpringSecurity6.0 通過(guò)JWTtoken進(jìn)行認(rèn)證授權(quán)的文章就介紹到這了,更多相關(guān)SpringSecurity認(rèn)證授權(quán)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springSecurity自定義登錄接口和JWT認(rèn)證過(guò)濾器的流程
- SpringSecurity+jwt+captcha登錄認(rèn)證授權(quán)流程總結(jié)
- SpringSecurity+Redis+Jwt實(shí)現(xiàn)用戶(hù)認(rèn)證授權(quán)
- SpringBoot整合SpringSecurity和JWT和Redis實(shí)現(xiàn)統(tǒng)一鑒權(quán)認(rèn)證
- SpringSecurity+jwt+redis基于數(shù)據(jù)庫(kù)登錄認(rèn)證的實(shí)現(xiàn)
- SpringBoot+SpringSecurity+JWT實(shí)現(xiàn)系統(tǒng)認(rèn)證與授權(quán)示例
- SpringBoot整合SpringSecurity實(shí)現(xiàn)JWT認(rèn)證的項(xiàng)目實(shí)踐
- SpringSecurity整合jwt權(quán)限認(rèn)證的全流程講解
- SpringSecurity構(gòu)建基于JWT的登錄認(rèn)證實(shí)現(xiàn)
- SpringSecurity JWT基于令牌的無(wú)狀態(tài)認(rèn)證實(shí)現(xiàn)
相關(guān)文章
SpringBoot實(shí)戰(zhàn)記錄之?dāng)?shù)據(jù)訪(fǎng)問(wèn)
對(duì)于數(shù)據(jù)訪(fǎng)問(wèn)層,無(wú)論是SQL還是NOSQL,Spring Boot默認(rèn)采用整合Spring Data的方式進(jìn)行統(tǒng)一處理,添加大量自動(dòng)配置,屏蔽了很多設(shè)置,下面這篇文章主要介紹了SpringBoot實(shí)戰(zhàn)記錄之?dāng)?shù)據(jù)訪(fǎng)問(wèn),需要的朋友可以參考下2022-04-04
利用java和sqlserver建立簡(jiǎn)易圖書(shū)管理系統(tǒng)的完整步驟
圖書(shū)館管理系統(tǒng)是圖書(shū)館管理工作中不可缺少的部分,它對(duì)于圖書(shū)館的管理者和使用者都非常重要,下面這篇文章主要給大家介紹了關(guān)于利用java和sqlserver建立簡(jiǎn)易圖書(shū)管理系統(tǒng)的完整步驟,需要的朋友可以參考下2022-06-06
Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn)
這篇文章主要介紹了Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
MyBatis saveBatch 性能調(diào)優(yōu)的實(shí)現(xiàn)
本文主要介紹了MyBatis saveBatch 性能調(diào)優(yōu)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
java中排序報(bào):Comparison method violates its general contract異常的解
這篇文章主要給大家介紹了關(guān)于java中排序報(bào):Comparison method violates its general contract異常的解決方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06
詳解mybatis-plus配置找不到Mapper接口路徑的坑
這篇文章主要介紹了詳解mybatis-plus配置找不到Mapper接口路徑的坑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Spring Cloud 配置中心內(nèi)容加密的配置方法
這篇文章主要介紹了Spring Cloud 配置中心內(nèi)容加密的配置方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
MyBatis-Plus 動(dòng)態(tài)表名SQL解析器的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus 動(dòng)態(tài)表名SQL解析器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

