spring-boot - 如何使用 Spring-Boot 2.0 和 OAuth2 登录不同类型的用户
问题描述
我有不同类型的用户要使用单个授权服务器进行身份验证。每个人都有一个数据库表,并将通过不同的客户端登录。我希望能够知道我必须从哪个数据库表中对其进行身份验证。所以我尝试从请求中获取clientID,但我不能。这是我尝试过的:
@Override
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String clientID = ((OAuth2Authentication) authentication).getOAuth2Request().getClientId();
if (clientID.equals("clientID1")){
//look at the user table
}
else if (clientID.equals("clientID2")){
//look at the company table
}
else
throw new UsernameNotFoundException("ClientID " + clientID + " not found.");
}
上面的代码编译得很好,但问题发生在运行时,出现如下强制转换异常:
{
"error": "unauthorized",
"error_description": "org.springframework.security.authentication.UsernamePasswordAuthenticationToken cannot be cast to org.springframework.security.oauth2.provider.OAuth2Authentication"
}
所以我的问题是:如何从请求中获取 clientID ?或者我可以使用另一种策略来验证我的不同用户吗?任何帮助表示赞赏。
解决方案
推荐阅读
- python - 如何从一个 Python 文件调用同一目录中的另一个函数
- android - 使用自定义摄像头设置 Antmedia 视频流
- c# - 试图用一个视图发布两个模型
- python - 出于某种原因,在 pygame 中我无法移动我的太空船
- jsp - 如何在 vscode 中运行 jsp 文件?
- laravel - Laravel artisan 有没有命令管理器
- yii2 - 我无法从我的 yii 框架后端访问 Gii 界面
- swiftui - 有没有办法在针对 iOS 13 时有条件地使用 @StateObject?
- javascript - 赛普拉斯测试自动化中的 CORS 问题。"chromeWebSecuirty": false 在 Chrome 最新版本中不起作用
- javascript - 如何从 woocommerce 订单中删除“价格后缀”