JwtAccessTokenConverter 和 JwtTokenStore 已弃用。可以用什么代替?

问题描述 投票:0回答:3

我正在使用 Spring Security 创建一个应用程序。我需要将 JWT 与 JwtAccessTokenConverter 一起使用,并且 JwtTokenStore 已被弃用。当我导入时,它显示它已折旧。可以用什么代替?


package com.devsuperior.dscatalog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

@Configuration
public class AppConfig {

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(); // return a instance
    }
    
    
    // Objects can access a token JWT: ready, write, create, etc
    
    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
        tokenConverter.setSigningKey("MY-JWT-SECRET");
        return tokenConverter;
    }

    @Bean
    public JwtTokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

}

java spring jwt
3个回答
0
投票

实际上,整个 Spring Security OAuth 项目 已被弃用。我假设您需要授权服务器的替代方案。如果是这种情况,你可以尝试使用 Spring 授权服务器

Spring 授权服务器项目由 Spring Security 团队领导,专注于向 Spring 社区提供 OAuth 2.1 授权服务器支持。

它现在支持很多功能,您可以等待下一个版本中完全支持的 OIDC 实现。


0
投票

我将 pom.xml 调整为该依赖项:

<dependency>
        <groupId>org.springframework.security.oauth.boot</groupId>
        <artifactId>spring-security-oauth2-autoconfigure</artifactId>
</dependency>

0
投票

不确定您是否出于同样的原因要求,但在 accessTokenConverter 的早期版本中,我可以覆盖 extractAuthentication 方法,因此我可以将权限从令牌映射到 spring 角色。在 Spring Security 6 资源服务器中,我可以使用这段代码

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.oauth2ResourceServer(oauth2 -> oauth2.jwt(customizer -> customizer
                .jwtAuthenticationConverter(jwt -> getJwtAuthenticationToken(jwt)))
        );
        return http.build();
    }

    private AbstractAuthenticationToken getJwtAuthenticationToken(Jwt jwt) {
        return new JwtAuthenticationToken(jwt, getGrantedAuthorities(jwt));
    }
© www.soinside.com 2019 - 2024. All rights reserved.