来自OAuth2资源服务器的自定义原则提取器



我正在尝试为我的web应用程序实现一个自定义原理提取器。我有一个不同的身份验证应用程序,从中,我正在验证我传递的令牌。对于使用/oauth/check_token路径映射的令牌验证im。一旦令牌有效,API将从内部给出以下响应。

{
"user_id": 13,
"user_name": "test@gmail.com",
"scope": [
"read",
"write"
],
"active": true,
"exp": 1649874072,
"authorities": [
"ROLE_ADMIN",
"ROLE_USER",
],
"client_id": "test"}

我已经自定义了这个请求,并从身份验证应用程序中添加了user_id键值。我想从@AuthenticationPrincipal Principal principal获取这个用户id值。从这个原则对象中,我只得到登录的用户用户名。我想使用自定义原则从check_token API中提取这个用户id。目前,我正在尝试从这个引用中实现一个原则提取器,但我无法从check_token API中提取用户id。任何帮助都会很棒。

基本上,您需要实现自己的AuthenticationProviderauthenticate方法,并注册此提供程序。

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// Get user details from authentication
MyCustomAuthentication customAuthentication = new MyCustomAuthentication();
customAuthentication.setPrincipal(yourPrincipal);
return customAuthentication;
}

然后您可以在控制器方法中使用@AuthenticationPrincipal

最新更新