我有一个api,它使用AD令牌进行授权。我试图在服务组件中获取用户的用户名。但我没能。我试过这个。
val authentication: Authentication = SecurityContextHolder.getContext().authentication
println(authentication.name) // Random short string with 3 "-". Not JWT
println(authentication.details.toString()) // WebAuthenticationDetails [RemoteIpAddress=127.0.0.1, SessionId=null]
println(authentication.authorities.toString()) // Prints Scope [SCOPE_User.Read]
println(authentication.principal) // org.springframework.security.oauth2.jwt.Jwt@xxxxxxxx令牌来自AD,它确实包含用户数据。有效负载包含这些字段以及与用户相关的内容。我把剩下的都拿走了。
{,
"family_name": "Wick",
"given_name": "John",
"name": "WickJohn",
"roles": [
"User"
],
"scp": "User.Read",
"unique_name": "wickjo@gmail.com",
"upn": "wickjo@gmail.com",,
}有人知道吗?
发布于 2022-05-20 09:32:33
我只需手动阅读jwt就可以轻松地解决这个问题。
val authentication = SecurityContextHolder.getContext().authentication
val jwt = authentication.principal as Jwt
println(jwt.claims["name"])如果能找出为什么我没有自动得到它还是很有趣的
发布于 2022-05-19 10:48:00
若要获取中的用户名/用户信息,请尝试以下操作:
resource server中配置Authorization Server。resource server提供了一个筛选器resource server。SecurityContextHolder.。如果上面的代码不起作用,那么尝试使用On Behalf Of Flow和这个https://github.com/gary-archer/oauth.websample.azure/blob/master/api/src/host/oauth/authenticator.ts博客中提到的代码。
有关更多信息,请参考下面的链接:
https://stackoverflow.com/questions/72300798
复制相似问题