如何从请求头获取JWT令牌

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

How to get JWT Token from RequestHeader

问题

  1. [![在这里输入图片描述][1]][1]我试图从get请求的头部获取我的Bearer令牌如果我想要显示令牌那么就会返回NULL有人知道为什么我没有获得令牌吗
  2. ```java
  3. public String sendSmsToCustomer(HttpServletRequest request, @PathVariable("name") String name, @PathVariable("customer_phone_number") String customer_phone_number, @PathVariable("text") String text) throws Exception {
  4. String user = request.getHeader("Authorization");
  5. System.out.println(user);
  6. }

更新 1

  1. public static String getUsernameFromToken(HttpServletRequest request) {
  2. String token = request.getHeader(HEADER_STRING);
  3. String user = null;
  4. if (token != null) {
  5. // 解析令牌。
  6. try {
  7. user = Jwts.parser()
  8. .setSigningKey(SECRET)
  9. .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
  10. .getBody()
  11. .getSubject();
  12. } catch (Exception e) {
  13. throw e;
  14. }
  15. return user;
  16. } else
  17. return null;
  18. }

更新 2

  1. @GetMapping("/contacts/token")
  2. public String getToken(HttpServletRequest request) throws Exception {
  3. String user = request.getHeader(HEADER_STRING);
  4. System.out.println(user);
  5. return user;
  6. }

更新 3
在我获得完整的头部输出后,我发现 "Authorization" 不可用。

  1. {
  2. "x-forwarded-host": "localhost:8800",
  3. "x-forwarded-proto": "http",
  4. "x-forwarded-prefix": "/contacts",
  5. "postman-token": "081ea72c-e270-46db-8703-e5afc9887ba1",
  6. "host": "localhost:8100",
  7. "x-forwarded-port": "8800",
  8. "content-type": "application/json",
  9. "connection": "Keep-Alive",
  10. "x-forwarded-for": "0:0:0:0:0:0:0:1",
  11. "accept-encoding": "gzip, deflate, br",
  12. "user-agent": "PostmanRuntime/7.26.2",
  13. "accept": "*/*"
  14. }

因此,我将无法在那里找到令牌。也许是我的依赖关系吗?我只集成了 jjwt 用于令牌。有人有解决方案吗?我已经为这个错误困扰了几个小时。

英文:

如何从请求头获取JWT令牌I try to get my bearer token from the header of the getrequest. If I want to display the token, then NULL is returned. Does anyone have any idea why I don't get the token?

  1. public String sendSmsToCustomer(HttpServletRequest request, @PathVariable("name") String name,@PathVariable("customer_phone_number") String customer_phone_number, @PathVariable("text") String text) throws Exception
  2. {
  3. String user = request.getHeader("Authorization");
  4. System.out.println(user);

UPDATE 1

  1. public static String getUsernameFromToken(HttpServletRequest request) {
  2. String token = request.getHeader(HEADER_STRING);
  3. String user = null;
  4. if (token != null) {
  5. // parse the token.
  6. try {
  7. user = Jwts.parser()
  8. .setSigningKey(SECRET)
  9. .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
  10. .getBody()
  11. .getSubject();
  12. } catch (Exception e) {
  13. throw e;
  14. }
  15. return user;
  16. }
  17. else
  18. return null;
  19. }

UPDATE 2

  1. @GetMapping("/contacts/token")
  2. public String getToken(HttpServletRequest request) throws Exception
  3. {
  4. String user = request.getHeader(HEADER_STRING);
  5. System.out.println(user);
  6. return user;
  7. }

UPDATE 3
After I have had the complete header output, I see that "Authorization" is not available.

  1. {
  2. "x-forwarded-host": "localhost:8800",
  3. "x-forwarded-proto": "http",
  4. "x-forwarded-prefix": "/contacts",
  5. "postman-token": "081ea72c-e270-46db-8703-e5afc9887ba1",
  6. "host": "localhost:8100",
  7. "x-forwarded-port": "8800",
  8. "content-type": "application/json",
  9. "connection": "Keep-Alive",
  10. "x-forwarded-for": "0:0:0:0:0:0:0:1",
  11. "accept-encoding": "gzip, deflate, br",
  12. "user-agent": "PostmanRuntime/7.26.2",
  13. "accept": "*/*"
  14. }

Therefore I will not be able to find the token there either. Is it maybe my dependencies? I only integrated the jjwt dependency for the token. Does anyone have a solution? Been sitting on this bug for several hours

答案1

得分: 1

你应该提供给我们你正在发送的请求,以便检查令牌是否已发送。
也许你可以尝试通过添加这个头部来接收它,像这样:

  1. @RequestHeader("Authorization") String token
英文:

You should provide us the request you are sending to check if the token was send.
Maybe you can also try receiving it like this by adding this header

  1. @RequestHeader("Authorization") String token

答案2

得分: 0

如果request是请求对象,则可以执行以下操作:

  1. request.getHeader(HttpHeaders.AUTHORIZATION);
英文:

If request is the Request object, then you can do:

  1. request.getHeader(HttpHeaders.AUTHORIZATION);

huangapple
  • 本文由 发表于 2020年7月27日 15:35:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63110697.html
匿名

发表评论

匿名网友

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

确定