Ale*_*dro 5 spring oauth spring-security jwt spring-security-oauth2
我制作了一个简单的应用程序,它使用带有 oauth/jwt 提供程序的 spring 安全性。我通过自定义 JwtAccessTokenConverter 在 jwt 令牌中添加了额外的信息,并且效果很好。
我的问题是如何在我的 Rest Controller 中获取这些额外信息。
这是我的测试:
@RequestMapping(value = "/test", produces = { "application/json" },method = RequestMethod.GET)
public String testMethod(OAuth2Authentication authentication,
OAuth2AccessToken token,
Principal user){
.....
Object a=token.getAdditionalInformation();
Object b=token.getValue();
...
}
Run Code Online (Sandbox Code Playgroud)
结果是:
一些额外的信息:
我找到了一个可能的解决方案。
我在 JwtAccessTokenConverter 中设置了 DefaultAccessTokenConverter ,在其中设置了自定义 UserTokenConverter 。
所以.. JwtAccessTokenConverter 仅管理访问令牌的 jwt 方面(令牌验证和提取),新的 DefaultAccessTokenConverter 管理访问令牌转换的 oauth 方面,包括使用我的自定义 UserTokenConverter 来创建具有从 jwt 令牌提取的自定义信息的 Prcipal。
public class myUserConverter extends DefaultUserAuthenticationConverter {
public Authentication extractAuthentication(Map<String, ?> map) {
if (map.containsKey(USERNAME)) {
// Object principal = map.get(USERNAME);
Collection<? extends GrantedAuthority> authorities = getAuthorities(map);
UserDto utente = new UserDto();
utente.setUsername(map.get(USERNAME).toString());
utente.setUfficio(map.get("ufficio").toString());
utente.setExtraInfo(map.get("Informazione1").toString());
utente.setNome(map.get("nome").toString());
utente.setCognome(map.get("cognome").toString());
utente.setRuolo(map.get("ruolo").toString());
return new UsernamePasswordAuthenticationToken(utente, "N/A", authorities);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3945 次 |
| 最近记录: |