|
|
|
@ -6,8 +6,6 @@ import java.util.Date;
|
|
|
|
import jakarta.servlet.http.Cookie;
|
|
|
|
import jakarta.servlet.http.Cookie;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.http.ResponseCookie;
|
|
|
|
import org.springframework.http.ResponseCookie;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
@ -19,10 +17,11 @@ import kr.re.etri.autoflow.security.services.UserDetailsImpl;
|
|
|
|
import io.jsonwebtoken.*;
|
|
|
|
import io.jsonwebtoken.*;
|
|
|
|
import io.jsonwebtoken.io.Decoders;
|
|
|
|
import io.jsonwebtoken.io.Decoders;
|
|
|
|
import io.jsonwebtoken.security.Keys;
|
|
|
|
import io.jsonwebtoken.security.Keys;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
|
|
@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 String jwtSecret;
|
|
|
|
@ -59,18 +58,16 @@ public class JwtUtils {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ResponseCookie getCleanJwtCookie() {
|
|
|
|
public ResponseCookie getCleanJwtCookie() {
|
|
|
|
ResponseCookie cookie = ResponseCookie.from(jwtCookie, null).path("/api").build();
|
|
|
|
return ResponseCookie.from(jwtCookie, null).path("/api").build();
|
|
|
|
return cookie;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ResponseCookie getCleanJwtRefreshCookie() {
|
|
|
|
public ResponseCookie getCleanJwtRefreshCookie() {
|
|
|
|
ResponseCookie cookie = ResponseCookie.from(jwtRefreshCookie, null).path("/api/auth/refreshtoken").build();
|
|
|
|
return ResponseCookie.from(jwtRefreshCookie, null).path("/api/auth/refreshtoken").build();
|
|
|
|
return cookie;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getUserNameFromJwtToken(String token) {
|
|
|
|
public String getUserNameFromJwtToken(String token) {
|
|
|
|
return Jwts.parserBuilder().setSigningKey(key()).build()
|
|
|
|
return Jwts.parserBuilder().setSigningKey(key()).build()
|
|
|
|
.parseClaimsJws(token).getBody().getSubject();
|
|
|
|
.parseClaimsJws(token).getBody().getSubject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Key key() {
|
|
|
|
private Key key() {
|
|
|
|
@ -82,13 +79,13 @@ public class JwtUtils {
|
|
|
|
Jwts.parserBuilder().setSigningKey(key()).build().parse(authToken);
|
|
|
|
Jwts.parserBuilder().setSigningKey(key()).build().parse(authToken);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
} 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;
|
|
|
|
@ -96,24 +93,23 @@ public class JwtUtils {
|
|
|
|
|
|
|
|
|
|
|
|
public String generateTokenFromUsername(String username) {
|
|
|
|
public String generateTokenFromUsername(String username) {
|
|
|
|
return Jwts.builder()
|
|
|
|
return Jwts.builder()
|
|
|
|
.setSubject(username)
|
|
|
|
.setSubject(username)
|
|
|
|
.setIssuedAt(new Date())
|
|
|
|
.setIssuedAt(new Date())
|
|
|
|
.setExpiration(new Date((new Date()).getTime() + jwtExpirationMs))
|
|
|
|
.setExpiration(new Date((new Date()).getTime() + jwtExpirationMs))
|
|
|
|
.signWith(key(), SignatureAlgorithm.HS256)
|
|
|
|
.signWith(key(), SignatureAlgorithm.HS256)
|
|
|
|
.compact();
|
|
|
|
.compact();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ResponseCookie generateCookie(String name, String value, String path) {
|
|
|
|
private ResponseCookie generateCookie(String name, String value, String path) {
|
|
|
|
ResponseCookie cookie = ResponseCookie.from(name, value).path(path).maxAge(24 * 60 * 60).httpOnly(true).build();
|
|
|
|
return ResponseCookie.from(name, value)
|
|
|
|
return cookie;
|
|
|
|
.path(path)
|
|
|
|
|
|
|
|
.maxAge(24 * 60 * 60)
|
|
|
|
|
|
|
|
.httpOnly(true)
|
|
|
|
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String getCookieValueByName(HttpServletRequest request, String name) {
|
|
|
|
private String getCookieValueByName(HttpServletRequest request, String name) {
|
|
|
|
Cookie cookie = WebUtils.getCookie(request, name);
|
|
|
|
Cookie cookie = WebUtils.getCookie(request, name);
|
|
|
|
if (cookie != null) {
|
|
|
|
return cookie != null ? cookie.getValue() : null;
|
|
|
|
return cookie.getValue();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|