英文:
How to get JWT Token from RequestHeader
问题
[![在这里输入图片描述][1]][1]我试图从get请求的头部获取我的Bearer令牌。如果我想要显示令牌,那么就会返回NULL。有人知道为什么我没有获得令牌吗?
```java
public String sendSmsToCustomer(HttpServletRequest request, @PathVariable("name") String name, @PathVariable("customer_phone_number") String customer_phone_number, @PathVariable("text") String text) throws Exception {
    String user = request.getHeader("Authorization");
    System.out.println(user);
}
更新 1
public static String getUsernameFromToken(HttpServletRequest request) {
    String token = request.getHeader(HEADER_STRING);
    String user = null;
    if (token != null) {
        // 解析令牌。
        try {
            user = Jwts.parser()
                    .setSigningKey(SECRET)
                    .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
                    .getBody()
                    .getSubject();
        } catch (Exception e) {
            throw e;
        }
        return user;
    } else
        return null;
}
更新 2
@GetMapping("/contacts/token")
public String getToken(HttpServletRequest request) throws Exception {
    String user = request.getHeader(HEADER_STRING);
    System.out.println(user);
    return user;
}
更新 3
在我获得完整的头部输出后,我发现 "Authorization" 不可用。
{
    "x-forwarded-host": "localhost:8800",
    "x-forwarded-proto": "http",
    "x-forwarded-prefix": "/contacts",
    "postman-token": "081ea72c-e270-46db-8703-e5afc9887ba1",
    "host": "localhost:8100",
    "x-forwarded-port": "8800",
    "content-type": "application/json",
    "connection": "Keep-Alive",
    "x-forwarded-for": "0:0:0:0:0:0:0:1",
    "accept-encoding": "gzip, deflate, br",
    "user-agent": "PostmanRuntime/7.26.2",
    "accept": "*/*"
}
因此,我将无法在那里找到令牌。也许是我的依赖关系吗?我只集成了 jjwt 用于令牌。有人有解决方案吗?我已经为这个错误困扰了几个小时。
英文:
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?
public String sendSmsToCustomer(HttpServletRequest request, @PathVariable("name") String name,@PathVariable("customer_phone_number") String customer_phone_number, @PathVariable("text") String text) throws Exception
			{
			String user = request.getHeader("Authorization");
			System.out.println(user);
UPDATE 1
public static String getUsernameFromToken(HttpServletRequest request) {
        String token = request.getHeader(HEADER_STRING);
        String user = null;
        if (token != null) {
            // parse the token.
            try {
                user = Jwts.parser()
                        .setSigningKey(SECRET)
                        .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
                        .getBody()
                        .getSubject();
            } catch (Exception e) {
                throw e;
            }
            return user;
        }
        else
        return null;
    }
UPDATE 2
		@GetMapping("/contacts/token")
			public String getToken(HttpServletRequest request) throws Exception
			{
			String user = request.getHeader(HEADER_STRING);
			System.out.println(user);
			return user;
			}
UPDATE 3
After I have had the complete header output, I see that "Authorization" is not available.
{
    "x-forwarded-host": "localhost:8800",
    "x-forwarded-proto": "http",
    "x-forwarded-prefix": "/contacts",
    "postman-token": "081ea72c-e270-46db-8703-e5afc9887ba1",
    "host": "localhost:8100",
    "x-forwarded-port": "8800",
    "content-type": "application/json",
    "connection": "Keep-Alive",
    "x-forwarded-for": "0:0:0:0:0:0:0:1",
    "accept-encoding": "gzip, deflate, br",
    "user-agent": "PostmanRuntime/7.26.2",
    "accept": "*/*"
}
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
你应该提供给我们你正在发送的请求,以便检查令牌是否已发送。
也许你可以尝试通过添加这个头部来接收它,像这样:
@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
@RequestHeader("Authorization") String token
答案2
得分: 0
如果request是请求对象,则可以执行以下操作:
request.getHeader(HttpHeaders.AUTHORIZATION);
英文:
If request is the Request object, then you can do:
request.getHeader(HttpHeaders.AUTHORIZATION);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论