Spring OAUTH : Override CheckTokenEndpoint 'check_token?token=' response map

标签 spring spring-mvc spring-security oauth spring-security-oauth2

我想重写 CheckTokenEndpoint 以将我自己的自定义输出作为 Map 提供给资源服务器。我已经尝试过以下方法,但不起作用。

  1. 为 (/oauth/check_token) 引入新的自定义 Controller ,但 Spring 拒绝此自定义并注册自己的 Controller 。

Overriding bean definition for bean 'checkTokenEndpoint' with a different definition: replacing [Generic bean: class [com.datami.auth.security.CheckTokenEndpoint]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [/usr/local/Cellar/tomcat/8.5.5/libexec/webapps/oauth-server/WEB-INF/classes/com/datami/auth/security/CheckTokenEndpoint.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration; factoryMethodName=checkTokenEndpoint; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/security/oauth2/config/annotation/web/configuration/AuthorizationServerEndpointsConfiguration.class]]

  • 使用 (/oauth/check_custom_token) 创建了我自己的端点,但不确定下面的 Autowiring ResourceServerTokenServices,@autowire 对我没有帮助。

    @autowire
    私有(private)ResourceServerTokenServices资源服务器 token 服务;

  • Spring 已使用 DefaultTokenServices Autowiring 此功能。

    我还可以在代码中创建 new DefaultTokenServices() ,但是如何在 DefaultTokenServices 中 Autowiring 以下内容?又是同样的问题。

    private TokenStore tokenStore;
    
    private ClientDetailsService clientDetailsService;
    
    private TokenEnhancer accessTokenEnhancer;
    
    private AuthenticationManager authenticationManager; 
    

    你能帮我一下吗?

    最佳答案

    CheckTokenEndpoint 依赖于其 accessTokenConverter 实例来创建和返回映射。

    您可以创建一个自定义 AccessTokenConverter(如果需要,可以从 OOTB DefaultAccessTokenConverter 扩展)并按如下方式使用它:

    @Configuration
    @EnableAuthorizationServer
    public class MyAuthConfig extends AuthorizationServerConfigurerAdapter {
    
        ...
    
        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints.accessTokenConverter(new MyAccessTokenConverter())...
    
            ....
    

    当然,您可能希望使用工厂方法来创建 accessTokenConverter 实例,它允许您将一些属性注入(inject)实例等。

    完成后,在 AuthorizationServerEndpointsConfiguration.checkTokenEndpoint 中,您可以看到上面设置的 accessTokenConverter 将被传递到 CheckTokenEndpoint 的 OOTB 实例,并用于创建 map 。

    关于 Spring OAUTH : Override CheckTokenEndpoint 'check_token?token=' response map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47405585/

    相关文章:

    spring - 如何使用 Mockito 在 Spring 中模拟 Autowiring 的 @Value 字段?

    java - 如果等于 mustache 的条件

    java - Spring REST 模板 POST

    java - 即使使用正确的用户名/密码,Spring 2.5 登录也没有响应

    spring - 在spring数据jpa存储库方法查询上附加自定义条件

    java - Weblogic 12c 上的 Spring 应用程序部署失败

    spring-security - 返回HTTP错误401代码并跳过过滤器链

    spring - Grails Spring Security Core插件无法正常工作

    java - 在Spring MVC中的Web应用程序的web-inf文件夹中写入txt文件

    java - 如何使用 xml 配置文件、JAVA、Spring security 通过 LDAP 对用户进行身份验证