【发布时间】:2019-07-22 05:14:31
【问题描述】:
我正在使用 spring-security-oauth2-autoconfigure 版本 2.1.6.RELEASE 来保护资源服务器,并尝试获取我的 jwt 中列出的用户名。我的 jwt 的有效负载包含 client_id 和用户名。当我尝试从 OAuth2Authentication 对象获取 Principal 时,它返回 client_id 而不是用户名。
例如,如果 jwt 有效负载包含:
client_id":"2v3r098kgipu053lph0cb9nfjb","username":"7e953975-0df2-49ff-9b23-cae0864384b7"
我的代码是:
@GetMapping("/whoami")
public String whoami(@CurrentSecurityContext OAuth2Authentication auth) {
logger.debug("auth.name: {}", auth.getName() );
logger.debug("auth.principal: {}", auth.getPrincipal());
OAuth2Request req = auth.getOAuth2Request();
logger.debug("req.clientid: {}", req.getClientId() );
return "success";
}
我在日志中看到以下内容:
auth.name: 2v3r098kgipu053lph0cb9nfjb
auth.principal: 2v3r098kgipu053lph0cb9nfjb
req.clientid: 2v3r098kgipu053lph0cb9nfjb
我需要做什么才能从 jwt 获取用户名?
【问题讨论】:
-
刚试了一下,返回
null。
标签: spring spring-boot spring-security spring-security-oauth2