'JWT token does not start with Bearer
i cant find a way to add the Bearer
and on the localhost:8080 I get this
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat May 14 00:13:48 CST 2022 There was an unexpected error (type=Unauthorized, status=401). Unauthorized
JwtUtil.java
package com.ecommerce.site.util;
public class JwtUtil {
@Value("${jwt.secret}")
private String SECRET_KEY;
private static final int TOKEN_VALIDITY = 3600 * 5;
public String getUsernameFromToken(String token) {
return getClaimFromToken(token, Claims::getSubject);
}
public <T> T getClaimFromToken(String token, Function<Claims, T> claimsResolver) {
final Claims claims = getAllClaimsFromToken(token);
return claimsResolver.apply(claims);
}
private Claims getAllClaimsFromToken(String token) {
return Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody();
}
public Boolean validateToken(String token, UserDetails userDetails) {
final String username = getUsernameFromToken(token);
return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));
}
private Boolean isTokenExpired(String token) {
final Date expiration = getExpirationDateFromToken(token);
return expiration.before(new Date());
}
public Date getExpirationDateFromToken(String token) {
return getClaimFromToken(token, Claims::getExpiration);
}
public String generateToken(UserDetails userDetails) {
Map<String, Object> claims = new HashMap<>();
return Jwts.builder()
.setClaims(claims)
.setSubject(userDetails.getUsername())
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + TOKEN_VALIDITY * 1000))
.signWith(SignatureAlgorithm.HS512, SECRET_KEY)
.compact();
}
}
enter code here
Solution 1:[1]
I fixed my error by clearing local storage in my web application, there was an old token stored that messed things up.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Oussama Chaieb |