功能:收口小程序前后台安全与防重复提交
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.xiaowang.yumitailgateway.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Gateway JWT 校验配置。
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "yuminail.security.jwt")
|
||||
public class JwtProperties {
|
||||
|
||||
private String secret = "yuminail-secret-key-for-jwt-token-generation-2026-must-be-at-least-512-bits-long-for-hs512-algorithm-security-requirements";
|
||||
|
||||
private String issuer = "yuminail";
|
||||
|
||||
private long clockSkewSeconds = 30;
|
||||
}
|
||||
+11
-3
@@ -59,6 +59,9 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
@Autowired
|
||||
private GatewayConfig gatewayConfig;
|
||||
|
||||
@Autowired
|
||||
private GatewayJwtValidator gatewayJwtValidator;
|
||||
|
||||
private final AntPathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
@Override
|
||||
@@ -70,15 +73,20 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
String token = request.getHeaders().getFirst("Authorization");
|
||||
if (!StringUtils.hasText(token)) {
|
||||
String authorization = request.getHeaders().getFirst("Authorization");
|
||||
if (!StringUtils.hasText(authorization)) {
|
||||
return unAuthorized(exchange, "未携带Token,拒绝访问");
|
||||
}
|
||||
|
||||
if (!token.startsWith("Bearer ")) {
|
||||
if (!authorization.startsWith("Bearer ")) {
|
||||
return unAuthorized(exchange, "Token格式错误");
|
||||
}
|
||||
|
||||
String token = authorization.substring("Bearer ".length());
|
||||
if (!gatewayJwtValidator.validateAccessToken(token)) {
|
||||
return unAuthorized(exchange, "Token无效或已过期");
|
||||
}
|
||||
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.xiaowang.yumitailgateway.filter;
|
||||
|
||||
import com.xiaowang.yumitailgateway.config.JwtProperties;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Gateway 侧 JWT 快速校验。
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class GatewayJwtValidator {
|
||||
|
||||
private final JwtProperties jwtProperties;
|
||||
|
||||
public boolean validateAccessToken(String token) {
|
||||
try {
|
||||
Claims claims = Jwts.parserBuilder()
|
||||
.setSigningKey(getKey())
|
||||
.setAllowedClockSkewSeconds(jwtProperties.getClockSkewSeconds())
|
||||
.requireIssuer(jwtProperties.getIssuer())
|
||||
.build()
|
||||
.parseClaimsJws(token)
|
||||
.getBody();
|
||||
return "access".equals(claims.get("type", String.class));
|
||||
} catch (Exception exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private SecretKey getKey() {
|
||||
return Keys.hmacShaKeyFor(jwtProperties.getSecret().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
@@ -113,6 +113,10 @@ spring:
|
||||
- RewritePath=/api/(?<remaining>.*), /${remaining}
|
||||
|
||||
yuminail:
|
||||
security:
|
||||
jwt:
|
||||
issuer: yuminail
|
||||
clock-skew-seconds: 30
|
||||
gateway:
|
||||
whitelist:
|
||||
- /api/user/login
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
<PatternLayout pattern="${LOG_PATTERN}"/>
|
||||
</Console>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user