|
|
@@ -4,17 +4,18 @@ import io.jsonwebtoken.Claims;
|
|
|
import io.jsonwebtoken.ExpiredJwtException;
|
|
|
import io.jsonwebtoken.Jwts;
|
|
|
import io.jsonwebtoken.security.Keys;
|
|
|
-
|
|
|
-import javax.crypto.SecretKey;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.security.MessageDigest;
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
|
+import org.springframework.security.core.userdetails.User;
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.crypto.SecretKey;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.*;
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
@@ -28,14 +29,15 @@ public class JwtService {
|
|
|
@Value("${jwt.secret}")
|
|
|
private String secret;
|
|
|
|
|
|
- private static final Map<String, Long> ROLE_EXPIRATION_HOURS = Map.of(
|
|
|
- "ADMIN", 1L,
|
|
|
- "SERVICE", 12L,
|
|
|
- "VPN", 24L,
|
|
|
- "USER", 48L
|
|
|
+ private static final Map<String, Long> ROLE_EXPIRATION_MINUTES = Map.of(
|
|
|
+ "SYSTEM", 1L,
|
|
|
+ "ADMIN", 60L,
|
|
|
+ "SERVICE", 720L,
|
|
|
+ "VPN", 1440L,
|
|
|
+ "USER", 2880L
|
|
|
);
|
|
|
|
|
|
- private static final List<String> ROLE_PRIORITY = List.of("ADMIN", "SERVICE", "VPN");
|
|
|
+ private static final List<String> ROLE_PRIORITY = List.of("SYSTEM", "ADMIN", "SERVICE", "VPN");
|
|
|
|
|
|
private SecretKey getSigningKey() {
|
|
|
try {
|
|
|
@@ -60,16 +62,36 @@ public class JwtService {
|
|
|
return claimsResolver.apply(claims);
|
|
|
}
|
|
|
|
|
|
+ public Authentication generateToken(UserDetails userDetails, long minutes) {
|
|
|
+ Map<String, Object> claims = new HashMap<>();
|
|
|
+ claims.put("authorities", userDetails.getAuthorities().stream()
|
|
|
+ .map(GrantedAuthority::getAuthority)
|
|
|
+ .collect(toSet())
|
|
|
+ );
|
|
|
+ return generateToken(claims, userDetails, minutes);
|
|
|
+ }
|
|
|
+
|
|
|
public Authentication generateToken(UserDetails userDetails) {
|
|
|
Map<String, Object> claims = new HashMap<>();
|
|
|
- claims.put(
|
|
|
- "authorities", userDetails.getAuthorities().stream()
|
|
|
- .map(GrantedAuthority::getAuthority)
|
|
|
- .collect(toSet())
|
|
|
+ claims.put("authorities", userDetails.getAuthorities().stream()
|
|
|
+ .map(GrantedAuthority::getAuthority)
|
|
|
+ .collect(toSet())
|
|
|
);
|
|
|
return generateToken(claims, userDetails);
|
|
|
}
|
|
|
|
|
|
+ public Authentication generateSystemToken() {
|
|
|
+ Map<String, Object> claims = new HashMap<>();
|
|
|
+ claims.put("authorities", Set.of("SYSTEM"));
|
|
|
+
|
|
|
+ UserDetails systemUser = User.builder()
|
|
|
+ .username("system")
|
|
|
+ .authorities(new SimpleGrantedAuthority("SYSTEM"))
|
|
|
+ .build();
|
|
|
+
|
|
|
+ return generateToken(claims, systemUser);
|
|
|
+ }
|
|
|
+
|
|
|
public Boolean isTokenValid(String token, UserDetails userDetails) {
|
|
|
final String username = extractUsername(token);
|
|
|
return (username.equals(userDetails.getUsername())) && !isTokenExpired(token);
|
|
|
@@ -80,7 +102,24 @@ public class JwtService {
|
|
|
}
|
|
|
|
|
|
private Authentication generateToken(Map<String, Object> claims, UserDetails userDetails) {
|
|
|
- Date expirationDate = new Date(currentTimeMillis() + 1000 * 60 * 60 * hoursByRole(claims));
|
|
|
+ Date expirationDate = new Date(currentTimeMillis() + 1000 * 60 * minutesByRole(claims));
|
|
|
+ String token = Jwts.builder()
|
|
|
+ .claims(claims)
|
|
|
+ .subject(userDetails.getUsername())
|
|
|
+ .issuedAt(new Date(currentTimeMillis()))
|
|
|
+ .expiration(expirationDate)
|
|
|
+ .signWith(getSigningKey())
|
|
|
+ .compact();
|
|
|
+
|
|
|
+ return new Authentication(token,
|
|
|
+ expirationDate.toInstant(),
|
|
|
+ userDetails.getUsername(),
|
|
|
+ userDetails.getAuthorities().stream().map(GrantedAuthority::getAuthority).toList()
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private Authentication generateToken(Map<String, Object> claims, UserDetails userDetails, long minutes) {
|
|
|
+ Date expirationDate = new Date(currentTimeMillis() + 1000 * 60 * minutes);
|
|
|
String token = Jwts.builder()
|
|
|
.claims(claims)
|
|
|
.subject(userDetails.getUsername())
|
|
|
@@ -109,15 +148,15 @@ public class JwtService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private long hoursByRole(Map<String, Object> claims) {
|
|
|
+ private long minutesByRole(Map<String, Object> claims) {
|
|
|
@SuppressWarnings("unchecked")
|
|
|
Set<String> authorities = (Set<String>) claims.get("authorities");
|
|
|
|
|
|
return ROLE_PRIORITY.stream()
|
|
|
.filter(authorities::contains)
|
|
|
.findFirst()
|
|
|
- .map(ROLE_EXPIRATION_HOURS::get)
|
|
|
- .orElse(48L);
|
|
|
+ .map(ROLE_EXPIRATION_MINUTES::get)
|
|
|
+ .orElse(2880L);
|
|
|
}
|
|
|
|
|
|
}
|