首页 > 解决方案 > 如何通过添加附加参数来实现 OAuth2.0 与 Spring Security

问题描述

我正在使用基于以下链接的 springboot2 使用 spring-security 实现 oauth2。

https://www.devglan.com/spring-security/spring-boot-oauth2-jwt-example

我成功地实施了它。

在这里,我在验证用户时对上述代码有不同的用例。

我需要在邮递员的“x-www-form-urlencoded”选项卡中传递公司ID以及用户名和密码以及grant_type(密码)。我必须根据用户名和 companyId 获取用户。

所以请帮我看看我需要在上面的链接代码中做些什么改变,这样我才能达到我的要求。

在这里我只收到电子邮件。我需要电子邮件和 companyId。

@Override
@Transactional
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {

    User user = userRepository.findByEmail(email).orElseThrow(
            () -> new UsernameNotFoundException("User Not Found with -> username or email : " + email));
    return UserPrinciple.build(user);
}

Expected:

@Override
@Transactional
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {

    User user = userRepository.findByEmailAndCompanyId(email,companyId).orElseThrow(
            () -> new UsernameNotFoundException("User Not Found with -> username or email : " + email));
    return UserPrinciple.build(user);
}

目前使用用户名和密码它工作正常。

但是在我的系统中,我将同一用户映射到不同的公司。如果发现同一用户映射到多个公司,我会收到如下错误。

{ "error": "unauthorized", "error_description": "查询没有返回唯一结果:2;嵌套异常是 javax.persistence.NonUniqueResultException:查询没有返回唯一结果:2" }

我需要根据用户名和密码以及导致单个用户的 companyId 获取用户。

标签: javaspring-bootspring-security-oauth2

解决方案


如果您想要访问令牌的其他信息,您可以使用 TokenEnhancer 类来做到这一点。

CustomTokenEnhancer.java

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("id", user.getCompanyId());
        additionalInfo.put("authorities", user.getAuthorities());

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

        return accessToken;
    }

}

然后使用此类的实例来 void configure(AuthorizationServerEndpointsConfigurer endpoints) 像这样的方法

AuthorizationServerConfig.java

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.authenticationManager(authenticationManager)
            .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
            .tokenEnhancer(new CustomTokenEnhancer());
}

推荐阅读