java - 如何通过添加附加参数来实现 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 获取用户。
解决方案
如果您想要访问令牌的其他信息,您可以使用 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());
}
推荐阅读
- html - 弹性盒不拉伸
- javascript - combineReducers 是覆盖状态而不是设置不同的状态
- python - 使用给定的熊猫列表对 CSV 文件进行排序
- java - Lock.java 和 LockSupport.java:我们有 LockSupport 的目的是什么?
- vue.js - 更改范围滑块VueJS后如何更新值
- ios - CIImage 范围崩溃
- python - SMTP-Sendgrid Email in Django app: working locally, on Heroku error SMTPServerDisconnected
- c# - 为什么十进制验证不适用于 ASP.NET Core 3.1 Razor 页面?
- javascript - 如何使用 axios 在 React 中同时发送和获取数据?
- ios - 在表格视图单元格之间移动的文本