英文:
Spring Boot: Read username value of Bearer Token Authorization from Postman request
问题
我正在尝试使用 HttpServletRequest 的 getRemoteUser() 方法来获取用户名。当请求通过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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论