【问题标题】:Unauthorized status when accessing /oauth/token_key访问 /oauth/token_key 时的未授权状态
【发布时间】:2019-09-19 14:15:12
【问题描述】:

我正在使用 Spring 和 tutorial 构建 Oauth 服务器。

在资源服务器实施期间,我注意到令牌密钥端点 (/oauth/token_key) 不是公开的。

基于this doc,我尝试将以下内容添加到 AuthorizationServerConfigurerAdapter:

security.tokenKeyAccess("permitAll()")
                .checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')");

还有:

security.tokenKeyAccess("isAnonymous() || hasAuthority('ROLE_TRUSTED_CLIENT')")
               .checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')");

两种配置均无效。我还尝试在我的 WebSecurityAdapter 上添加规则:

protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/oauth/token_key").permitAll();
}

现在我会看到一个对话框,询问登录名和密码,当我点击取消时,我会收到以下信息:

There was an unexpected error (type=Unauthorized, status=401).
Unauthorized
org.springframework.security.access.AccessDeniedException: You need to authenticate to see a shared key
    at org.springframework.security.oauth2.provider.endpoint.TokenKeyEndpoint.getKey(TokenKeyEndpoint.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    (...)

我错过了什么吗?

【问题讨论】:

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


    【解决方案1】:

    经过一番调查,我发现了问题:我忘记将keyPair注入用于转换为JWT的tokenConverter并添加一些额外的用户信息:

    //Inside AuthorizationServerConfigurerAdapter
    
    @Autowired
    private MyJwtTokenEnhancer jwtTokenEnhancer;
    @Autowired
    private KeystoreService keystoreService;
    
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
            //this line solved the issue
            jwtTokenEnhancer.setKeyPair(keystoreService.getKeyPair());
    
            endpoints.authenticationManager(manager)
                .accessTokenConverter(jwtTokenEnhancer)
                .tokenStore(tokenStore())
                .addInterceptor(new AuditInterceptor());
    }
    

    【讨论】:

    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 2021-02-23
    • 2020-09-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    相关资源
    最近更新 更多