Springboot WebFlux集成Spring Security實(shí)現(xiàn)JWT認(rèn)證的示例
1 簡(jiǎn)介
在之前的文章《Springboot集成Spring Security實(shí)現(xiàn)JWT認(rèn)證》講解了如何在傳統(tǒng)的Web項(xiàng)目中整合Spring Security和JWT,今天我們講解如何在響應(yīng)式WebFlux項(xiàng)目中整合。二者大體是相同的,主要區(qū)別在于Reactive WebFlux與傳統(tǒng)Web的區(qū)別。
2 項(xiàng)目整合
引入必要的依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency>
2.1 JWT工具類(lèi)
該工具類(lèi)主要功能是創(chuàng)建、校驗(yàn)、解析JWT。
@Component public class JwtTokenProvider { private static final String AUTHORITIES_KEY = "roles"; private final JwtProperties jwtProperties; private String secretKey; public JwtTokenProvider(JwtProperties jwtProperties) { this.jwtProperties = jwtProperties; } @PostConstruct public void init() { secretKey = Base64.getEncoder().encodeToString(jwtProperties.getSecretKey().getBytes()); } public String createToken(Authentication authentication) { String username = authentication.getName(); Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); Claims claims = Jwts.claims().setSubject(username); if (!authorities.isEmpty()) { claims.put(AUTHORITIES_KEY, authorities.stream().map(GrantedAuthority::getAuthority).collect(joining(","))); } Date now = new Date(); Date validity = new Date(now.getTime() + this.jwtProperties.getValidityInMs()); return Jwts.builder() .setClaims(claims) .setIssuedAt(now) .setExpiration(validity) .signWith(SignatureAlgorithm.HS256, this.secretKey) .compact(); } public Authentication getAuthentication(String token) { Claims claims = Jwts.parser().setSigningKey(this.secretKey).parseClaimsJws(token).getBody(); Object authoritiesClaim = claims.get(AUTHORITIES_KEY); Collection<? extends GrantedAuthority> authorities = authoritiesClaim == null ? AuthorityUtils.NO_AUTHORITIES : AuthorityUtils.commaSeparatedStringToAuthorityList(authoritiesClaim.toString()); User principal = new User(claims.getSubject(), "", authorities); return new UsernamePasswordAuthenticationToken(principal, token, authorities); } public boolean validateToken(String token) { try { Jws<Claims> claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token); if (claims.getBody().getExpiration().before(new Date())) { return false; } return true; } catch (JwtException | IllegalArgumentException e) { throw new InvalidJwtAuthenticationException("Expired or invalid JWT token"); } } }
2.2 JWT的過(guò)濾器
這個(gè)過(guò)濾器的主要功能是從請(qǐng)求中獲取JWT,然后進(jìn)行校驗(yàn),如何成功則把Authentication放進(jìn)ReactiveSecurityContext里去。當(dāng)然,如果沒(méi)有帶相關(guān)的請(qǐng)求頭,那可能是通過(guò)其它方式進(jìn)行鑒權(quán),則直接放過(guò),讓它進(jìn)入下一個(gè)Filter。
public class JwtTokenAuthenticationFilter implements WebFilter { public static final String HEADER_PREFIX = "Bearer "; private final JwtTokenProvider tokenProvider; public JwtTokenAuthenticationFilter(JwtTokenProvider tokenProvider) { this.tokenProvider = tokenProvider; } @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { String token = resolveToken(exchange.getRequest()); if (StringUtils.hasText(token) && this.tokenProvider.validateToken(token)) { Authentication authentication = this.tokenProvider.getAuthentication(token); return chain.filter(exchange) .subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)); } return chain.filter(exchange); } private String resolveToken(ServerHttpRequest request) { String bearerToken = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION); if (StringUtils.hasText(bearerToken) && bearerToken.startsWith(HEADER_PREFIX)) { return bearerToken.substring(7); } return null; } }
2.3 Security的配置
這里設(shè)置了兩個(gè)異常處理authenticationEntryPoint和accessDeniedHandler。
@Configuration public class SecurityConfig { @Bean SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http, JwtTokenProvider tokenProvider, ReactiveAuthenticationManager reactiveAuthenticationManager) { return http.csrf(ServerHttpSecurity.CsrfSpec::disable) .httpBasic(ServerHttpSecurity.HttpBasicSpec::disable) .authenticationManager(reactiveAuthenticationManager) .exceptionHandling().authenticationEntryPoint( (swe, e) -> { swe.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED); return swe.getResponse().writeWith(Mono.just(new DefaultDataBufferFactory().wrap("UNAUTHORIZED".getBytes()))); }) .accessDeniedHandler((swe, e) -> { swe.getResponse().setStatusCode(HttpStatus.FORBIDDEN); return swe.getResponse().writeWith(Mono.just(new DefaultDataBufferFactory().wrap("FORBIDDEN".getBytes()))); }).and() .securityContextRepository(NoOpServerSecurityContextRepository.getInstance()) .authorizeExchange(it -> it .pathMatchers(HttpMethod.POST, "/auth/login").permitAll() .pathMatchers(HttpMethod.GET, "/admin").hasRole("ADMIN") .pathMatchers(HttpMethod.GET, "/user").hasRole("USER") .anyExchange().permitAll() ) .addFilterAt(new JwtTokenAuthenticationFilter(tokenProvider), SecurityWebFiltersOrder.HTTP_BASIC) .build(); } @Bean public ReactiveAuthenticationManager reactiveAuthenticationManager(CustomUserDetailsService userDetailsService, PasswordEncoder passwordEncoder) { UserDetailsRepositoryReactiveAuthenticationManager authenticationManager = new UserDetailsRepositoryReactiveAuthenticationManager(userDetailsService); authenticationManager.setPasswordEncoder(passwordEncoder); return authenticationManager; } }
2.4 獲取JWT的Controller
先判斷對(duì)用戶密碼進(jìn)行判斷,如果正確則返回對(duì)應(yīng)的權(quán)限用戶,根據(jù)用戶生成JWT,再返回給客戶端。
@RestController @RequestMapping("/auth") public class AuthController { @Autowired ReactiveAuthenticationManager authenticationManager; @Autowired JwtTokenProvider jwtTokenProvider; @PostMapping("/login") public Mono<String> login(@RequestBody AuthRequest request) { String username = request.getUsername(); Mono<Authentication> authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, request.getPassword())); return authentication.map(auth -> jwtTokenProvider.createToken(auth)); } }
3 總結(jié)
其它與之前的大同小異,不一一講解了。
代碼請(qǐng)查看:https://github.com/LarryDpk/pkslow-samples
以上就是Springboot WebFlux集成Spring Security實(shí)現(xiàn)JWT認(rèn)證的示例的詳細(xì)內(nèi)容,更多關(guān)于Springboot WebFlux集成Spring Security的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot3中Spring?WebFlux?SSE服務(wù)器發(fā)送事件的實(shí)現(xiàn)步驟
- SpringBoot深入分析webmvc和webflux的區(qū)別
- springboot webflux 過(guò)濾器(使用RouterFunction實(shí)現(xiàn))
- SpringBoot之webflux全面解析
- 詳解Spring Boot2 Webflux的全局異常處理
- 解決spring-boot2.0.6中webflux無(wú)法獲得請(qǐng)求IP的問(wèn)題
- Spring?Boot?3.4.3?基于?Spring?WebFlux?實(shí)現(xiàn)?SSE?功能(代碼示例)
相關(guān)文章
Java SpringBoot快速集成SpringBootAdmin管控臺(tái)監(jiān)控服務(wù)詳解
這篇文章主要介紹了如何基于springboot-admin管控臺(tái)監(jiān)控服務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09Spring Boot實(shí)現(xiàn)分布式鎖的自動(dòng)釋放的示例代碼
在實(shí)際開(kāi)發(fā)中,我們可以使用 Redis、Zookeeper 等分布式系統(tǒng)來(lái)實(shí)現(xiàn)分布式鎖,本文將介紹如何使用 Spring Boot 來(lái)實(shí)現(xiàn)分布式鎖的自動(dòng)釋放,感興趣的朋友跟隨小編一起看看吧2023-06-06Hadoop之NameNode Federation圖文詳解
今天小編就為大家分享一篇關(guān)于Hadoop之NameNode Federation圖文詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01SpringMVC MethodArgumentResolver的作用與實(shí)現(xiàn)
這篇文章主要介紹了SpringMVC MethodArgumentResolver的作用與實(shí)現(xiàn),MethodArgumentResolver采用一種策略模式,在Handler的方法被調(diào)用前,Spring MVC會(huì)自動(dòng)將HTTP請(qǐng)求中的參數(shù)轉(zhuǎn)換成方法參數(shù)2023-04-04使用Idea或Datagrip導(dǎo)入excel數(shù)據(jù)的方法
這篇文章主要介紹了使用Idea或Datagrip導(dǎo)入excel數(shù)據(jù)的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11FeignClient支持運(yùn)行時(shí)動(dòng)態(tài)指定URL方式
在實(shí)際開(kāi)發(fā)中,我們經(jīng)常通過(guò)FeignClient接口調(diào)用三方API,當(dāng)面對(duì)不同的環(huán)境對(duì)應(yīng)不同的地址時(shí),可以通過(guò)配置文件和占位符來(lái)切換,但在同一個(gè)環(huán)境中需要調(diào)用不同地址的相同接口時(shí),這種方法就失效了,此時(shí),可以通過(guò)實(shí)現(xiàn)RequestInterceptor接口來(lái)動(dòng)態(tài)切換地址2024-11-11詳解java實(shí)現(xiàn)遍歷二叉樹(shù)的三種情況
這篇文章主要介紹了java實(shí)現(xiàn)遍歷二叉樹(shù)的三種情況,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04Java設(shè)計(jì)模式之中介者模式的實(shí)現(xiàn)方式
Java中介者模式是一種行為型設(shè)計(jì)模式,它通過(guò)一個(gè)中介者對(duì)象來(lái)協(xié)調(diào)多個(gè)對(duì)象之間的交互,降低對(duì)象之間的耦合度,提高系統(tǒng)的可維護(hù)性和可擴(kuò)展性。本文將介紹該設(shè)計(jì)模式的原理、使用場(chǎng)景和實(shí)現(xiàn)方法2023-04-04