OAuth2ResourceServerConfig 中的字段 tokenServices 需要一个类型为“ResourceServerTokenServices”的 bean

问题描述

当令牌无效时,我试图覆盖检查令牌 Oauth 响应消息,但出现以下错误

Field tokenServices in com.krishantha.rentcloud.authorizationserver.config.OAuth2ResourceServerConfig required a bean of type 'org.springframework.security.oauth2.provider.token.ResourceServerTokenServices' that Could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but Could not be injected:
    - Bean method 'jwkTokenServices' in 'ResourceServerTokenServicesConfiguration.JwkTokenStoreConfiguration' not loaded because OAuth JWK Condition did not find key jwk set URI not provided
    - Bean method 'jwkTokenServices' in 'ResourceServerTokenServicesConfiguration.JwkTokenStoreConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration did not match
    - Bean method 'jwtTokenServices' in 'ResourceServerTokenServicesConfiguration.JwtTokenServicesConfiguration' not loaded because OAuth JWT Condition did not find provided public key
    - Bean method 'jwtTokenServices' in 'ResourceServerTokenServicesConfiguration.JwtTokenServicesConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration did not match
    - Bean method 'socialTokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.socialTokenServicesConfiguration' not loaded because @ConditionalOnClass did not find required class 'org.springframework.social.connect.support.OAuth2ConnectionFactory'
    - Bean method 'userInfoTokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.socialTokenServicesConfiguration' not loaded because @ConditionalOnClass did not find required class 'org.springframework.social.connect.support.OAuth2ConnectionFactory'
    - Bean method 'socialTokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.socialTokenServicesConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration did not match
    - Bean method 'userInfoTokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.socialTokenServicesConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration did not match
    - Bean method 'socialTokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.socialTokenServicesConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration$RemotetokenServicesConfiguration did not match
    - Bean method 'userInfoTokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.socialTokenServicesConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration$RemotetokenServicesConfiguration did not match
    - Bean method 'remotetokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.TokenInfoServicesConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration did not match
    - Bean method 'remotetokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.TokenInfoServicesConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration$RemotetokenServicesConfiguration did not match
    - Bean method 'userInfoTokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.UserInfoTokenServicesConfiguration' not loaded because OAuth TokenInfo Condition did not find user-info-uri property
    - Bean method 'userInfoTokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.UserInfoTokenServicesConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration did not match
    - Bean method 'userInfoTokenServices' in 'ResourceServerTokenServicesConfiguration.RemotetokenServicesConfiguration.UserInfoTokenServicesConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration$RemotetokenServicesConfiguration did not match


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.security.oauth2.provider.token.ResourceServerTokenServices' in your configuration.

我的配置文件是:

package com.krishantha.rentcloud.authorizationserver.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
import org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.stereotype.Component;

@Component
@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Autowired
    public ResourceServerTokenServices tokenServices;

    @Autowired
    private WebResponseExceptionTranslator<?> oauth2ResponseExceptionTranslator;
    

    @Override
    public void configure(final ResourceServerSecurityConfigurer config) {
        OAuth2AccessDeniedHandler auth2AccessDeniedHandler = new OAuth2AccessDeniedHandler();
        auth2AccessDeniedHandler.setExceptionTranslator(oauth2ResponseExceptionTranslator);
        OAuth2AuthenticationEntryPoint authenticationEntryPoint = new OAuth2AuthenticationEntryPoint();
        authenticationEntryPoint.setExceptionTranslator(oauth2ResponseExceptionTranslator);
        config.tokenServices(tokenServices).accessDeniedHandler(auth2AccessDeniedHandler).authenticationEntryPoint(authenticationEntryPoint);
    }

}

和 CustomWebResponseExceptionTranslator 为:

@Component  
public class CustomWebResponseExceptionTranslator extends DefaultWebResponseExceptionTranslator {

    /**
     * Modify OAuth2.0 Error Response
     * @param e
     * @return ResponseEntity<OAuth2Exception>
     * @throws Exception
     */

    @Override
    public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
        ResponseEntity responseEntity = super.translate(e);
        OAuth2Exception auth2Exception = (OAuth2Exception)responseEntity.getBody();
        if (auth2Exception != null) {
            auth2Exception.addAdditional@R_441_4045@ion("data",null);
            auth2Exception.addAdditional@R_441_4045@ion("message",auth2Exception.getMessage());
            auth2Exception.addAdditional@R_441_4045@ion("statusCode",String.valueOf(auth2Exception.getHttpErrorCode()));
        }
        return new ResponseEntity<OAuth2Exception>(auth2Exception,responseEntity.getHeaders(),responseEntity.getStatusCode());
    }
}

谁能帮我摆脱这个excelption?我在这里尝试在令牌过期时修改 oauth 的响应消息。请帮忙。

提前致谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)