【问题标题】:Spring Boot OAuth 2 - expiring refresh tokens when password changedSpring Boot OAuth 2 - 更改密码时刷新令牌到期
【发布时间】:2018-06-17 09:46:51
【问题描述】:

我使用 Spring Boot/OAuth 创建了一个 API。当前设置为 access_tokens 有效期为 30 天,refresh_tokens 有效期为 5 年。有人要求 OAuth 以这种方式工作,以便可以反复使用单个 refresh_token。我们还需要做的是实现某种方式在用户更改密码时使刷新令牌过期,这是我正在努力解决的问题,因为我们没有使用令牌存储,因为我们使用的是 JWT,所以没有必要存储令牌,即使我们将其存储在数据库中,我们也经常收到“无效刷新令牌”错误,因此删除了令牌存储。

我的问题是,您如何处理过期的刷新令牌,例如,当用户更改密码时(如 OAuth 所建议的那样)。

我的客户特别要求返回的 refresh_token 是长寿命的,但我担心长寿命的刷新令牌不是很安全,好像任何人都持有该令牌,他们可以访问用户帐户,直到该令牌自然会过期。就我个人而言,我更愿意将 refresh_tokens 的到期时间设置为 45 天,强制客户端至少每 45 天存储一个新的 refresh_token。

这是我的一些安全配置类,以显示我当前的设置方式;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private Environment env;

    @Autowired
    private CustomUserDetailsService userDetailsService;

    @Autowired
    private AccountAuthenticationProvider accountAuthenticationProvider;

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

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

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

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        final JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey(env.getProperty("jwt.secret"));
        return jwtAccessTokenConverter;
    }

}



@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)
                    .accessTokenConverter(jwtAccessTokenConverter);
        }

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

}

【问题讨论】:

    标签: spring-boot spring-security jwt spring-security-oauth2


    【解决方案1】:

    如果使用 JWT,则不支持撤销令牌。如果你想实现这个功能,你应该考虑使用 JdbcTokenStore。

    @Bean
    public TokenStore tokenStore() { 
        return new JdbcTokenStore(dataSource()); 
    }
    
    @Bean
    public DataSource dataSource() { 
        DriverManagerDataSource jdbcdataSource =  new DriverManagerDataSource();
        jdbcdataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        jdbcdataSource.setUrl(env.getProperty("jdbc.url"));//connection String
        jdbcdataSource.setUsername(env.getProperty("jdbc.user"));
        jdbcdataSource.setPassword(env.getProperty("jdbc.pass")); 
        return dataSource;
    }
    

    当用户更改密码时,应调用revokeToken API

    @Resource(name="tokenServices")
    ConsumerTokenServices tokenServices;
    
    @RequestMapping(method = RequestMethod.POST, value = "/tokens/revoke/{tokenId:.*}")
    @ResponseBody
    public String revokeToken(@PathVariable String tokenId) {
        tokenServices.revokeToken(tokenId);
        return tokenId;
    }
    

    JDBCTokenStore 还公开了一种方法,您可以使用该方法使刷新令牌无效

    @RequestMapping(method = RequestMethod.POST, value = "/tokens/revokeRefreshToken/{tokenId:.*}")
    @ResponseBody
    public String revokeRefreshToken(@PathVariable String tokenId) {
        if (tokenStore instanceof JdbcTokenStore){
            ((JdbcTokenStore) tokenStore).removeRefreshToken(tokenId);
        }
        return tokenId;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-17
      • 2021-07-03
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 2015-06-14
      • 2016-06-01
      相关资源
      最近更新 更多