@@ -25,7 +25,7 @@ public class SecurityConfig {
|
|||||||
.sessionManagement(session ->
|
.sessionManagement(session ->
|
||||||
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
.requestMatchers("api/v{version}/files/public-download/**").permitAll() // логин, регистрация
|
.requestMatchers("api/v{version}/files/public-download/**").permitAll()
|
||||||
.requestMatchers("/api/files/public/**").permitAll()
|
.requestMatchers("/api/files/public/**").permitAll()
|
||||||
.requestMatchers("/api/auth/**").permitAll()
|
.requestMatchers("/api/auth/**").permitAll()
|
||||||
.requestMatchers("/api/file/link/**").permitAll()
|
.requestMatchers("/api/file/link/**").permitAll()
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ import org.springframework.stereotype.Component;
|
|||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class JwtUtil {
|
public class JwtUtil {
|
||||||
@@ -22,53 +20,44 @@ public class JwtUtil {
|
|||||||
@Value("${jwt.expiration:3600000}")
|
@Value("${jwt.expiration:3600000}")
|
||||||
private Long expiration;
|
private Long expiration;
|
||||||
|
|
||||||
|
|
||||||
private SecretKey getSigningKey() {
|
private SecretKey getSigningKey() {
|
||||||
return Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));
|
return Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String generateToken(Long userId, String email) {
|
public String generateToken(Long userId, String email) {
|
||||||
Map<String, Object> claims = new HashMap<>();
|
|
||||||
claims.put("userId", userId);
|
|
||||||
claims.put("email", email);
|
|
||||||
|
|
||||||
return Jwts.builder()
|
return Jwts.builder()
|
||||||
.setClaims(claims)
|
|
||||||
.setSubject(email)
|
.setSubject(email)
|
||||||
|
.claim("userId", userId)
|
||||||
.setIssuedAt(new Date())
|
.setIssuedAt(new Date())
|
||||||
.setExpiration(new Date(System.currentTimeMillis() + expiration))
|
.setExpiration(new Date(System.currentTimeMillis() + expiration))
|
||||||
.signWith(getSigningKey())
|
.signWith(getSigningKey(), SignatureAlgorithm.HS256)
|
||||||
.compact();
|
.compact();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Claims extractClaims(String token) {
|
||||||
|
return Jwts.parser()
|
||||||
|
.setSigningKey(getSigningKey())
|
||||||
|
.build()
|
||||||
|
.parseClaimsJws(token)
|
||||||
|
.getBody();
|
||||||
|
}
|
||||||
|
|
||||||
public String extractEmail(String token) {
|
public String extractEmail(String token) {
|
||||||
return extractAllClaims(token).getSubject();
|
return extractClaims(token).getSubject();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long extractUserId(String token) {
|
public Long extractUserId(String token) {
|
||||||
return extractAllClaims(token).get("userId", Long.class);
|
return extractClaims(token).get("userId", Long.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String extractRole(String token) {
|
public boolean isTokenExpired(String token) {
|
||||||
return extractAllClaims(token).get("role", String.class);
|
return extractClaims(token).getExpiration().before(new Date());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Date extractExpiration(String token) {
|
public boolean validateToken(String token) {
|
||||||
return extractAllClaims(token).getExpiration();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Claims extractAllClaims(String token) {
|
|
||||||
return Jwts.parser()
|
|
||||||
.verifyWith(getSigningKey())
|
|
||||||
.build()
|
|
||||||
.parseSignedClaims(token)
|
|
||||||
.getPayload();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean validateToken(String token) {
|
|
||||||
try {
|
try {
|
||||||
extractAllClaims(token);
|
return !isTokenExpired(token);
|
||||||
return !extractExpiration(token).before(new Date());
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user