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工具類
該工具類主要功能是創(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)文章!
- SpringBoot整合SpringSecurity和JWT的示例
- SpringBoot集成SpringSecurity和JWT做登陸鑒權(quán)的實(shí)現(xiàn)
- SpringBoot3.0+SpringSecurity6.0+JWT的實(shí)現(xiàn)
- 詳解SpringBoot+SpringSecurity+jwt整合及初體驗(yàn)
- SpringBoot集成Spring security JWT實(shí)現(xiàn)接口權(quán)限認(rèn)證
- SpringBoot3.x接入Security6.x實(shí)現(xiàn)JWT認(rèn)證的完整步驟
- SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證
- SpringBoot Security+JWT簡(jiǎn)單搭建的實(shí)現(xiàn)示例
相關(guān)文章
Java中動(dòng)態(tài)設(shè)置JVM參數(shù)的方法總結(jié)
通過(guò)動(dòng)態(tài)設(shè)置JVM參數(shù),開(kāi)發(fā)者可以更有效地管理資源使用和優(yōu)化性能,本文將詳細(xì)闡述如何在Java中動(dòng)態(tài)設(shè)置JVM參數(shù),感興趣的小伙伴可以了解下2024-12-12TreeSet詳解和使用示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
TreeSet是一個(gè)有序的集合,它的作用是提供有序的Set集合。這篇文章主要介紹了TreeSet使用示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Maven 版本管理與 flatten-maven-plugin 插件的使用解析
這篇文章主要介紹了Maven 版本管理與 flatten-maven-plugin 插件的使用解析,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07Spring中@Autowired @Resource @Inject三個(gè)注解有什么區(qū)別
在我們使用Spring框架進(jìn)行日常開(kāi)發(fā)過(guò)程中,經(jīng)常會(huì)使用@Autowired, @Resource, @Inject注解來(lái)進(jìn)行依賴注入,下面來(lái)介紹一下這三個(gè)注解有什么區(qū)別2023-03-03Java代碼實(shí)現(xiàn)對(duì)properties文件有序的讀寫(xiě)的示例
本篇文章主要介紹了Java代碼實(shí)現(xiàn)對(duì)properties文件有序的讀寫(xiě)的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11spring?boot集成smart-doc自動(dòng)生成接口文檔詳解
這篇文章主要介紹了spring?boot集成smart-doc自動(dòng)生成接口文檔詳解,smart-doc是一款同時(shí)支持java?restful?api和Apache?Dubbo?rpc接口文檔生成的工具,smart-doc顛覆了傳統(tǒng)類似swagger這種大量采用注解侵入來(lái)生成文檔的實(shí)現(xiàn)方法2022-09-09