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