【问题标题】:Spring oauth2 server, can not validate tokenSpring oauth2服务器,无法验证令牌
【发布时间】:2020-06-30 14:59:43
【问题描述】:

我从 Spring Boot oauth2 服务器生成 JWT 令牌,但是当我想使用 'http://auth_server/oauth/check_token' 端点验证此 jwt 令牌时,我使用授权标头中的 jwt 向此端点发送 post 请求,我得到 401 - 未经授权的 http错误代码

我从服务器得到的结果:

{
    "timestamp": "2020-03-19T16:28:39.999+0000",
    "status": 401,
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/oauth/check_token"
}

为什么 oauth 服务器返回 401?

这是我的 oauth 服务器的实现:

    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

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

        @Autowired
        private ClientDetailsServiceImpl clientDetailsService;

        private String privateKey = "private key";

        @Value("${jwt.accessTokenValidititySeconds:43200}") // 12 hours
        private int accessTokenValiditySeconds;

        @Value("${jwt.authorizedGrantTypes: authorization_code}")
        private String[] authorizedGrantTypes;

        @Value("${jwt.refreshTokenValiditySeconds:2592000}") // 30 days
        private int refreshTokenValiditySeconds;

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
                clients.withClientDetails(clientDetailsService);
        }

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {      
            TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
            tokenEnhancerChain.setTokenEnhancers(
              Arrays.asList(tokenEnhancer(), accessTokenConverter()));

            endpoints.tokenStore(tokenStore())
            .accessTokenConverter(accessTokenConverter())
            .authenticationManager(authenticationManager);
        }

        @Override
        public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
            oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()").allowFormAuthenticationForClients();
        }


        @Bean
        public JwtAccessTokenConverter accessTokenConverter() {
           JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
           converter.setSigningKey(privateKey);
           return converter;
        }


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

        @Bean
        public TokenEnhancer tokenEnhancer() {
            return new CustomTokenEnhancer();
        }

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

    }

【问题讨论】:

    标签: spring spring-boot oauth-2.0 spring-security-oauth2


    【解决方案1】:

    最后我发现GET 请求应该发送到http://server.com/oauth/check_token?token=[access_token]client namepasswordauthorization header 以检查访问令牌的有效性。下面是邮递员的例子:

    【讨论】:

    • checkTokenAccess("isAuthenticqted()") 更改为 checkTokenAccess("permitAll()") 应该可以工作
    • 我们都知道checkTokenAccess("permitAll()") 有效,但它不安全!,isAuthenticated();这比打开它更安全。任何想要检查令牌的客户都必须提供他们的客户凭据。我们需要使用基本授权标头。
    • allowFormAuthenticationForClients被调用时,框架添加一个名为ClientCredentialsTokenEndpointFilter的过滤器,我们可以通过查询参数发送集合client_idclient_secret以从/oauth/token检索access_token。由于/oauth/check_token已经设置为checkTokenAccess("isAuthenticqted()"),我想让/oauth/check_token也支持表单提交。
    • 所以我根据filter for check_token endpoint添加了一个自定义的ClientCredentialsTokenEndpointFilter但它不起作用,我发现`@Autowired AuthenticationManager authenticationManager`与http.getSharedObject(AuthenticationManager.class)不同。 autowired authenticationManager 的提供程序列表没有 DaoAuthenticationProvider ,但 ClientDetailsUserDetailsServiceDaoAuthenticationProvider 中。所以我的过滤器将始终得到 ` {"error": "invalid_client", "error_description": "Bad client credentials"}`
    猜你喜欢
    • 2015-11-02
    • 2012-04-11
    • 2013-04-11
    • 2015-10-23
    • 2019-10-08
    • 2022-08-21
    • 2020-01-16
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多