JwtUtils 修改为静态方法
This commit is contained in:
parent
fe062a709a
commit
c15c58ee02
|
@ -1,56 +1,55 @@
|
|||
package com.ossez.spring.security.security.jwt;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.ossez.spring.security.security.services.UserDetailsImpl;
|
||||
import io.jsonwebtoken.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.ossez.spring.security.security.services.UserDetailsImpl;
|
||||
import io.jsonwebtoken.*;
|
||||
import java.util.Date;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class JwtUtils {
|
||||
private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class);
|
||||
|
||||
@Value("${bezkoder.app.jwtSecret}")
|
||||
private String jwtSecret;
|
||||
private static String jwtSecret;
|
||||
|
||||
@Value("${bezkoder.app.jwtExpirationMs}")
|
||||
private int jwtExpirationMs;
|
||||
private static int jwtExpirationMs;
|
||||
|
||||
public String generateJwtToken(Authentication authentication) {
|
||||
public static String generateJwtToken(Authentication authentication) {
|
||||
|
||||
UserDetailsImpl userPrincipal = (UserDetailsImpl) authentication.getPrincipal();
|
||||
|
||||
return Jwts.builder()
|
||||
.setSubject((userPrincipal.getUsername()))
|
||||
.setIssuedAt(new Date())
|
||||
.setExpiration(new Date((new Date()).getTime() + jwtExpirationMs))
|
||||
.signWith(SignatureAlgorithm.HS512, jwtSecret)
|
||||
.compact();
|
||||
return Jwts.builder().setSubject((userPrincipal.getUsername())).setIssuedAt(new Date()).setExpiration(new Date((new Date()).getTime() + jwtExpirationMs)).signWith(SignatureAlgorithm.HS512, jwtSecret).compact();
|
||||
}
|
||||
|
||||
public String getUserNameFromJwtToken(String token) {
|
||||
public static String getUserNameFromJwtToken(String token) {
|
||||
return Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody().getSubject();
|
||||
}
|
||||
|
||||
public boolean validateJwtToken(String authToken) {
|
||||
/**
|
||||
* Validate Jwt Token
|
||||
*
|
||||
* @param authToken
|
||||
* @return
|
||||
*/
|
||||
public static boolean validateJwtToken(String authToken) {
|
||||
try {
|
||||
Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken);
|
||||
return true;
|
||||
} catch (SignatureException e) {
|
||||
logger.error("Invalid JWT signature: {}", e.getMessage());
|
||||
log.error("Invalid JWT signature: {}", e.getMessage());
|
||||
} catch (MalformedJwtException e) {
|
||||
logger.error("Invalid JWT token: {}", e.getMessage());
|
||||
log.error("Invalid JWT token: {}", e.getMessage());
|
||||
} catch (ExpiredJwtException e) {
|
||||
logger.error("JWT token is expired: {}", e.getMessage());
|
||||
log.error("JWT token is expired: {}", e.getMessage());
|
||||
} catch (UnsupportedJwtException e) {
|
||||
logger.error("JWT token is unsupported: {}", e.getMessage());
|
||||
log.error("JWT token is unsupported: {}", e.getMessage());
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.error("JWT claims string is empty: {}", e.getMessage());
|
||||
log.error("JWT claims string is empty: {}", e.getMessage());
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue