spring - 我可以在颁发访问 token 时包含用户信息吗?

标签 spring spring-security spring-security-oauth2

我在一些 oauth2 实现中看到了有关授权服务器在发布访问 token 时返回的响应的附加信息。我想知道是否有办法使用 spring-security-oauth2 来实现这一点。我希望能够在访问 token 响应中包含一些用户权限,这样我的消费应用程序就不需要管理用户权限,但仍然可以在他们自己的安全上下文中设置用户并应用他们自己的任何 spring-security检查。

  1. 如何获取有关访问 token 响应的信息?
  2. 如何在 oauth2 客户端拦截该信息并将其设置在安全上下文中?

我想另一种选择是使用 JWT token 并与客户端应用程序共享适当的信息,以便他们可以从 token 中解析用户/权限并将其设置在上下文中。这让我更不舒服,因为我更愿意控制哪些客户端应用程序可以访问这些信息(仅限受信任的应用程序),而 AFAIK 只有授权服务器和资源服务器应该知道如何解析 JWT token 。

最佳答案

您将需要像这样实现自定义 TokenEnhancer:

public class CustomTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        User user = (User) authentication.getPrincipal();
        final Map<String, Object> additionalInfo = new HashMap<>();

        additionalInfo.put("customInfo", "some_stuff_here");
        additionalInfo.put("authorities", user.getAuthorities());

        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);

        return accessToken;
    }

}

并将其作为具有相应 setter 的 bean 添加到您的 AuthorizationServerConfigurerAdapter

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    // Some autowired stuff here

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        // @formatter:off
        endpoints
            // ...
            .tokenEnhancer(tokenEnhancer());
        // @formatter:on
    }

    @Bean
    @Primary
    public AuthorizationServerTokenServices tokenServices() {
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        // ...
        tokenServices.setTokenEnhancer(tokenEnhancer());
        return tokenServices;
    }

    // Some @Bean here like tokenStore

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

}

然后在 Controller 中(例如)

@RestController
public class MyController {

    @Autowired
    private AuthorizationServerTokenServices tokenServices;

    @RequestMapping(value = "/getSomething", method = RequestMethod.GET)
    public String getSection(OAuth2Authentication authentication) {
        Map<String, Object> additionalInfo = tokenServices.getAccessToken(authentication).getAdditionalInformation();

        String customInfo = (String) additionalInfo.get("customInfo");
        Collection<? extends GrantedAuthority> authorities = (Collection<? extends GrantedAuthority>) additionalInfo.get("authorities");

        // Play with authorities

        return customInfo;
    }

}

我个人使用 JDBC TokenStore,所以我的“这里的一些 Autowiring 的东西”对应于一些 @Autowired Datasource、PasswordEncoder 等等。

希望这有帮助!

关于spring - 我可以在颁发访问 token 时包含用户信息吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28492116/

相关文章:

java - 使用 linkedin 进行 Spring OAuth 登录

java - Spring Security OAuth - 访问被拒绝

spring - Bean 引用被 [com.sun.proxy.$Proxy159] 类型的不兼容 bean 实例覆盖

spring - 哪个 Spring View 解析器与 AngularJS 配合得很好?

spring - 没有这样的属性 : org. codehaus.grails.INCLUDED_JS_LIBRARIES

java - 尝试将 spring boot war 部署到 tomcat8 时 Unresolved 循环引用?

java - Spring Oauth2 重定向 uri 没有改变

java - "No Hibernate Session bound to thread",事务方法中断下一个事务的执行

spring - 尝试解析 AuthenticationManager(Spring Security 和 Oauth)时检测到依赖循环

java - 使用 Spring-Boot 和 OAuth2 实现 JdbcTokenStore