【问题标题】:Spring Boot OAuth doesn't return a refresh token for clientsSpring Boot OAuth 不会为客户端返回刷新令牌
【发布时间】:2018-01-15 12:20:22
【问题描述】:

我有一个在 Spring Boot 中开发的 API,我刚刚注意到当您请求访问令牌时它没有返回刷新令牌。

API 的响应如下所示;

{
    "access_token": "ed0bdc62-dccf-4f58-933c-e28ad9598843",
    "token_type": "bearer",
    "expires_in": 2589494,
    "scope": "read write"
}

我的配置是这样的;

@Configuration
public class OAuth2ServerConfiguration {

    private static final String RESOURCE_ID = "myapi";

    @Autowired
    DataSource dataSource;

    @Bean
    public TokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
    }

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

        @Autowired
        TokenStore tokenStore;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources) {
            resources
                    .resourceId(RESOURCE_ID)
                    .tokenStore(tokenStore);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                    .csrf().disable()
                    .authorizeRequests()
                    .antMatchers("/oauth/**", "/view/**").permitAll()
                    .anyRequest().authenticated();
        }
    }

    @Configuration
    @EnableAuthorizationServer
    protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
        @Autowired
        private JwtAccessTokenConverter jwtAccessTokenConverter;

        @Autowired
        private DataSource dataSource;

        @Autowired
        private TokenStore tokenStore;

        @Autowired
        private CustomUserDetailsService userDetailsService;

        @Autowired
        @Qualifier("authenticationManagerBean")
        private AuthenticationManager authenticationManager;

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

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients
                    .jdbc(dataSource);
        }
    }

}

我之前的项目设置是使用 JWT 获取访问令牌,并且确实返回了刷新令牌,但是我不得不删除 JWT,因为它与使用令牌存储不兼容。

为了确认,它会在 grant_type = 密码时返回一个刷新令牌,但在它设置为 'client_credentials' 时不返回。

有人对我的配置不返回刷新令牌有什么建议吗?

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    RFC 6749(OAuth 2.0 授权框架)中的4.3.3. Access Token Response“不应包含刷新令牌。”因此,OAuth 2.0 授权服务器的大多数实现不会生成刷新令牌在Client Credentials flow

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,然后我改变了这个方法,我添加了 REFRESH_TOKEN 然后在响应中我得到了 refresh_token 值。

      静态最终字符串 REFRESH_TOKEN = "refresh_token";

      @Override
          public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
                  configurer
                      .inMemory()
                      .withClient(CLIENT_ID)
                      .secret(messageDigestPasswordEncoder.encode(CLIENT_SECRET))
                      .authorizedGrantTypes(GRANT_TYPE,REFRESH_TOKEN)
                      .scopes(SCOPE_READ, SCOPE_WRITE ,TRUST)
                      .accessTokenValiditySeconds(ACCESS_TOKEN_VALIDITY_SECONDS).
                       refreshTokenValiditySeconds(REFRESH_TOKEN_VALIDITY_SECONDS);
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-09
        • 2017-09-06
        • 2020-02-17
        • 2018-04-22
        • 1970-01-01
        • 2018-02-24
        • 2021-04-11
        • 2021-03-12
        相关资源
        最近更新 更多