SpringSecurity6.0 如何通過JWTtoken進(jìn)行認(rèn)證授權(quán)
之前寫過一個(gè)文章,從SpringSecurity 5.x升級(jí)到6.0,當(dāng)時(shí)是為了配合公司的大版本升級(jí)做的,里面的各項(xiàng)配置都是前人留下來的,其實(shí)沒有花時(shí)間進(jìn)行研究SpringSecurity的工作機(jī)制?,F(xiàn)在新東家有一個(gè)簡(jiǎn)單的系統(tǒng)要搭建,用戶的認(rèn)證授權(quán)流程也比較簡(jiǎn)單,通過用戶/密碼進(jìn)行登錄,登錄后生成JWT token返回給前端,后續(xù)認(rèn)證通過token進(jìn)行,就把SpringSecurity重新?lián)炝似饋恚罱ㄕ麄€(gè)系統(tǒng)的安全認(rèn)證框架。
項(xiàng)目依賴
<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>
項(xiàng)目后端整體還是通過Springboot來搭建,Springboot3.0中把JWT相關(guān)的依賴都整合到了spring-boot-starter-oauth2-resource-server
中,無需再單獨(dú)指定
認(rèn)證
首先我們先完成通過賬號(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è)方法表明通過基于HTTP Basic認(rèn)證協(xié)議anyRequest().authenticated()
表明所有請(qǐng)求都需要經(jīng)過認(rèn)證UserDetailsService
,這里創(chuàng)建了一個(gè)僅存在于內(nèi)存中的用戶,用戶名和密碼是user/password,密碼中添加的前綴{noop}和userDetailService的作用我們稍后再說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
在通過用戶名/密碼進(jìn)行認(rèn)證時(shí),需要通過來判斷用戶是否存在,在生產(chǎn)中,我們可以根據(jù)自己的需要通過數(shù)據(jù)庫等獲取用戶信息。
拿到用戶信息之后,要怎么校驗(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)行處理,即不僅限任何編碼處理,直接通過明文進(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
然后能夠得到類似的返回
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)求方法匹配等,
- 基于方法的控制,通過
@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)然你也可以通過配置進(jìn)行更改。
小結(jié)
這里簡(jiǎn)單介紹了一下認(rèn)證和授權(quán)的配置,實(shí)際上SpringSecurity要遠(yuǎn)比這些要復(fù)雜的多,有更深入的需求可以參考官方文檔或者源碼
這里推薦一下自己的項(xiàng)目地址,已經(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springSecurity自定義登錄接口和JWT認(rèn)證過濾器的流程
- SpringSecurity+jwt+captcha登錄認(rèn)證授權(quán)流程總結(jié)
- SpringSecurity+Redis+Jwt實(shí)現(xiàn)用戶認(rèn)證授權(quán)
- SpringBoot整合SpringSecurity和JWT和Redis實(shí)現(xiàn)統(tǒng)一鑒權(quán)認(rèn)證
- SpringSecurity+jwt+redis基于數(shù)據(jù)庫登錄認(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基于令牌的無狀態(tài)認(rèn)證實(shí)現(xiàn)
相關(guān)文章
Java關(guān)鍵字instanceof用法及實(shí)現(xiàn)策略
instanceof 運(yùn)算符是用來在運(yùn)行時(shí)判斷對(duì)象是否是指定類及其父類的一個(gè)實(shí)例。這篇文章主要介紹了Java關(guān)鍵字instanceof用法解析,需要的朋友可以參考下2020-08-08Spring整合CXF webservice restful實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了Spring整合CXF webservice restful的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Mybatis關(guān)聯(lián)查詢遇到的坑-無主鍵的關(guān)聯(lián)數(shù)據(jù)去重問題
這篇文章主要介紹了Mybatis關(guān)聯(lián)查詢遇到的坑-無主鍵的關(guān)聯(lián)數(shù)據(jù)去重問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Springboot 跨域配置無效及接口訪問報(bào)錯(cuò)的解決方法
這篇文章主要介紹了Springboot 跨域配置無效及接口訪問報(bào)錯(cuò)的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12spring cglib 與 jdk 動(dòng)態(tài)代理
本篇文章主要介紹了spring cglib與jdk動(dòng)態(tài)代理的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-05-05SpringBoot整合Echarts實(shí)現(xiàn)數(shù)據(jù)大屏
這篇文章給大家介紹了三步實(shí)現(xiàn)SpringBoot全局日志記錄,整合Echarts實(shí)現(xiàn)數(shù)據(jù)大屏,文中通過代碼示例給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03Java實(shí)現(xiàn)將PPT轉(zhuǎn)為OFD過程詳解
本文將通過Java后端程序代碼展示如何實(shí)現(xiàn)將PPT幻燈片轉(zhuǎn)成OFD格式,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的幫助,需要的可以參考一下2022-01-01自定義@RequestBody注解如何獲取JSON數(shù)據(jù)
這篇文章主要介紹了自定義@RequestBody注解如何獲取JSON數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04