spring oauth2服务器在授权代码流之后没有使用刷新令牌响应

o0lyfsai  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(379)

我已经设置了oauth2身份验证服务器,主要目的是使用授权代码流。据我所知,流正在工作,因为我能够在流的末尾获得有效的访问令牌,但问题是我没有获得请求令牌响应字段。我不确定我是否要做一些与我所做的不同的事情,但是我相信我要做的就是在authorizedgranttypes参数中添加“request\u token”(我已经做了)。我将在下面介绍相关的设置代码。
授权服务器配置服务器

@Import(AuthorizationServerEndpointsConfiguration.class)
@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    PasswordEncoder passwordEncoder;
    AuthenticationManager authenticationManager;
    KeyPair keyPair;
    RedisConnectionFactory redisConnectionFactory;

    public AuthorizationServerConfig(AuthenticationManager authenticationManager, KeyPair keyPair, RedisConnectionFactory redisConnectionFactory,
                                     PasswordEncoder passwordEncoder) {
        this.keyPair = keyPair;
        this.redisConnectionFactory = redisConnectionFactory;
        this.authenticationManager = authenticationManager;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                    .withClient("web")
                    .secret(passwordEncoder.encode("noonewilleverguess"))
                    .scopes("resource:read", "resource:write")
                    .authorizedGrantTypes("authorization_code", "refresh_token")
                    .redirectUris("http://localhost:8080/login-two");
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .authenticationManager(authenticationManager)
                .tokenStore(tokenStore(redisConnectionFactory))
                .accessTokenConverter(accessTokenConverter());
    }

    @Bean
    public TokenStore tokenStore(RedisConnectionFactory redisConnectionFactory) {
        return new RedisTokenStore(redisConnectionFactory);
    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setKeyPair(this.keyPair);
        return converter;
    }
}

授权服务器安全配置

@Configuration
public class JwtSetEndpointConfiguration extends AuthorizationServerSecurityConfiguration {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);

        http
                .requestMatchers()
                    .mvcMatchers("/.well-known/jwks.json")
                    .and()
                .authorizeRequests()
                    .mvcMatchers("/.well-known/jwks.json").permitAll();
    }

}

Web安全配置适配器

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    @Override
    protected UserDetailsService userDetailsService() {
        return new UserDetailsServiceImpl();
    }

    @Bean
    public KeyPair keyPairBean() throws NoSuchAlgorithmException {
        //TODO:drt - probs change
        KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
        gen.initialize(2048);
        return gen.generateKeyPair();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }
}

授权码流响应
我首先将get请求发送到:

http://localhost:8080/oauth/authorize?response_type=code&client_id=web&state=8781487s1

重定向示例:http://localhost:8080/登录2?代码=n-u9xj&状态=8781487s1
所以我发了一封邮件给http://localhost:8080/oauth/token,具有基本身份验证

这是我收到的示例响应,没有刷新令牌字段:

{
    "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTc2MjY1NzQsInVzZXJfbmFtZSI6ImFkbWluIiwiYXV0aG9yaXRpZXMiOlsiUk9MRV9VU0VSIl0sImp0aSI6ImRENUplcWI3QlhHTkd0WkI1QVU4akhNbGR1YyIsImNsaWVudF9pZCI6IndlYiIsInNjb3BlIjpbInJlc291cmNlOndyaXRlIiwicmVzb3VyY2U6cmVhZCJdfQ.D5XKotYYAZkKtZNWdD-wirtoi3prU3CCmibIiQF5kodbXeK5ETdZ5k8CgSBLd7Aq-XEdhYXUEbtzzI0vf1vHf_MyhPFy_owldm_JJf2-2z9jNoU2BSnGMp6TCM00pCSMwbk57paLRZouryHEhTdvixVDmez2e1KmMVmXP6NypARB3Sp5SD2sZ2JN7FBQdkQ0OMVChjAQMTy1M3mDiT5dpT7iD7JxKRFTmD7qKYSF_gbQi6mEF3oH4j40TGI_CpyP3kKdDh4kiEfNeFd84YNHGYZACsYHfjoJrtJV1ECoeLph5CpmSpzt0lhOlzy8Q98OsPR8SdRt5Ou9N-BFmftZDw",
    "token_type": "bearer",
    "expires_in": 13462,
    "scope": "resource:write resource:read",
    "jti": "dD5Jeqb7BXGNGtZB5AU8jHMlduc"
}

希望你们中的一个能帮上忙,因为在这一点上,在尝试了一些东西并在网上查找之后,我不知道我做错了什么。请帮帮我,

cwdobuhd

cwdobuhd1#

所以我设法解决了这个问题,所以它现在在授权流之后返回一个刷新令牌。我所要做的就是添加defaulttokenservices bean,并确保将SupportRefreshToken设置为true。

@Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        return defaultTokenServices;
    }

相关问题