Spring Boot: 从Postman请求中读取Bearer Token授权的用户名值

huangapple go评论71阅读模式
英文:

Spring Boot: Read username value of Bearer Token Authorization from Postman request

问题

我正在尝试使用 HttpServletRequestgetRemoteUser() 方法来获取用户名。当请求通过Web应用程序发起时,这个方法可以正常工作。然而,当请求通过Postman发起时,getRemoteUser() 的值是null。

我发现唯一从Bearer令牌中获取用户名的方法是在Postman中使用Authorization。我需要用户名来调用另一个需要用户名在负载中的REST API。我甚至尝试将Authorization Bearer令牌传递给REST API,看它是否可以返回所需的结果。但由于REST API期望用户名在负载中,这种方法并不起作用。

有人可以帮助我从Bearer令牌中提取用户名吗?

英文:

I am trying to get the username using the method getRemoteUser() of HttpServletRequest. This works fine when the request is initiated through the web application. However when the request is initiated through Postman. The value of getRemoteUser() is null.

I found out that the only way to get the username would be though the Bearer token used as Authorization in Postman.

I need the username to call another REST API which takes username in payload. I even tried to pass the Authorization Bearer token to the REST API if it can return the required result. But since it is expecting the username in payload this did not work.

Can anyone help me extract username from Bearer Token?

答案1

得分: 1

使用 SecurityContextHolder.getContext().getAuthentication().getName() 代替以获取已认证用户的用户名。

英文:

Use SecurityContextHolder.getContext().getAuthentication().getName() instead to get the username of the authenticated user

huangapple
  • 本文由 发表于 2023年7月17日 23:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定