添加自定义断言
This commit is contained in:
parent
c1f4d21caf
commit
c3e1562478
|
|
@ -1,10 +1,12 @@
|
||||||
package com.ruoyi.gateway.config;
|
package com.ruoyi.gateway.config;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
||||||
import com.google.code.kaptcha.util.Config;
|
import com.google.code.kaptcha.util.Config;
|
||||||
|
|
||||||
import static com.google.code.kaptcha.Constants.*;
|
import static com.google.code.kaptcha.Constants.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -13,11 +15,9 @@ import static com.google.code.kaptcha.Constants.*;
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class CaptchaConfig
|
public class CaptchaConfig {
|
||||||
{
|
|
||||||
@Bean(name = "captchaProducer")
|
@Bean(name = "captchaProducer")
|
||||||
public DefaultKaptcha getKaptchaBean()
|
public DefaultKaptcha getKaptchaBean() {
|
||||||
{
|
|
||||||
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
// 是否有边框 默认为true 我们可以自己设置yes,no
|
// 是否有边框 默认为true 我们可以自己设置yes,no
|
||||||
|
|
@ -44,8 +44,7 @@ public class CaptchaConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean(name = "captchaProducerMath")
|
@Bean(name = "captchaProducerMath")
|
||||||
public DefaultKaptcha getKaptchaBeanMath()
|
public DefaultKaptcha getKaptchaBeanMath() {
|
||||||
{
|
|
||||||
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
// 是否有边框 默认为true 我们可以自己设置yes,no
|
// 是否有边框 默认为true 我们可以自己设置yes,no
|
||||||
|
|
|
||||||
|
|
@ -14,19 +14,16 @@ import com.ruoyi.gateway.handler.SentinelFallbackHandler;
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class GatewayConfig
|
public class GatewayConfig {
|
||||||
{
|
|
||||||
@Bean
|
@Bean
|
||||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
public SentinelFallbackHandler sentinelGatewayExceptionHandler()
|
public SentinelFallbackHandler sentinelGatewayExceptionHandler() {
|
||||||
{
|
|
||||||
return new SentinelFallbackHandler();
|
return new SentinelFallbackHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Order(-1)
|
@Order(-1)
|
||||||
public GlobalFilter sentinelGatewayFilter()
|
public GlobalFilter sentinelGatewayFilter() {
|
||||||
{
|
|
||||||
return new SentinelGatewayFilter();
|
return new SentinelGatewayFilter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.ruoyi.gateway.config;
|
package com.ruoyi.gateway.config;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
|
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -8,62 +9,47 @@ import com.google.code.kaptcha.text.impl.DefaultTextCreator;
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
public class KaptchaTextCreator extends DefaultTextCreator
|
public class KaptchaTextCreator extends DefaultTextCreator {
|
||||||
{
|
|
||||||
private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");
|
private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getText()
|
public String getText() {
|
||||||
{
|
|
||||||
Integer result = 0;
|
Integer result = 0;
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
int x = random.nextInt(10);
|
int x = random.nextInt(10);
|
||||||
int y = random.nextInt(10);
|
int y = random.nextInt(10);
|
||||||
StringBuilder suChinese = new StringBuilder();
|
StringBuilder suChinese = new StringBuilder();
|
||||||
int randomoperands = (int) Math.round(Math.random() * 2);
|
int randomoperands = (int) Math.round(Math.random() * 2);
|
||||||
if (randomoperands == 0)
|
if (randomoperands == 0) {
|
||||||
{
|
|
||||||
result = x * y;
|
result = x * y;
|
||||||
suChinese.append(CNUMBERS[x]);
|
suChinese.append(CNUMBERS[x]);
|
||||||
suChinese.append("*");
|
suChinese.append("*");
|
||||||
suChinese.append(CNUMBERS[y]);
|
suChinese.append(CNUMBERS[y]);
|
||||||
}
|
} else if (randomoperands == 1) {
|
||||||
else if (randomoperands == 1)
|
if (!(x == 0) && y % x == 0) {
|
||||||
{
|
|
||||||
if (!(x == 0) && y % x == 0)
|
|
||||||
{
|
|
||||||
result = y / x;
|
result = y / x;
|
||||||
suChinese.append(CNUMBERS[y]);
|
suChinese.append(CNUMBERS[y]);
|
||||||
suChinese.append("/");
|
suChinese.append("/");
|
||||||
suChinese.append(CNUMBERS[x]);
|
suChinese.append(CNUMBERS[x]);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
result = x + y;
|
result = x + y;
|
||||||
suChinese.append(CNUMBERS[x]);
|
suChinese.append(CNUMBERS[x]);
|
||||||
suChinese.append("+");
|
suChinese.append("+");
|
||||||
suChinese.append(CNUMBERS[y]);
|
suChinese.append(CNUMBERS[y]);
|
||||||
}
|
}
|
||||||
}
|
} else if (randomoperands == 2) {
|
||||||
else if (randomoperands == 2)
|
if (x >= y) {
|
||||||
{
|
|
||||||
if (x >= y)
|
|
||||||
{
|
|
||||||
result = x - y;
|
result = x - y;
|
||||||
suChinese.append(CNUMBERS[x]);
|
suChinese.append(CNUMBERS[x]);
|
||||||
suChinese.append("-");
|
suChinese.append("-");
|
||||||
suChinese.append(CNUMBERS[y]);
|
suChinese.append(CNUMBERS[y]);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
result = y - x;
|
result = y - x;
|
||||||
suChinese.append(CNUMBERS[y]);
|
suChinese.append(CNUMBERS[y]);
|
||||||
suChinese.append("-");
|
suChinese.append("-");
|
||||||
suChinese.append(CNUMBERS[x]);
|
suChinese.append(CNUMBERS[x]);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
result = x + y;
|
result = x + y;
|
||||||
suChinese.append(CNUMBERS[x]);
|
suChinese.append(CNUMBERS[x]);
|
||||||
suChinese.append("+");
|
suChinese.append("+");
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.ruoyi.gateway.filter;
|
package com.ruoyi.gateway.filter;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
@ -27,13 +28,14 @@ import com.ruoyi.gateway.config.properties.IgnoreWhiteProperties;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 全局过滤器
|
||||||
|
*
|
||||||
* 网关鉴权
|
* 网关鉴权
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class AuthFilter implements GlobalFilter, Ordered
|
public class AuthFilter implements GlobalFilter, Ordered {
|
||||||
{
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(AuthFilter.class);
|
private static final Logger log = LoggerFactory.getLogger(AuthFilter.class);
|
||||||
|
|
||||||
private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60;
|
private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60;
|
||||||
|
|
@ -49,29 +51,24 @@ public class AuthFilter implements GlobalFilter, Ordered
|
||||||
private RedisService redisService;
|
private RedisService redisService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
{
|
|
||||||
String url = exchange.getRequest().getURI().getPath();
|
String url = exchange.getRequest().getURI().getPath();
|
||||||
// 跳过不需要验证的路径
|
// 跳过不需要验证的路径
|
||||||
if (StringUtils.matches(url, ignoreWhite.getWhites()))
|
if (StringUtils.matches(url, ignoreWhite.getWhites())) {
|
||||||
{
|
|
||||||
return chain.filter(exchange);
|
return chain.filter(exchange);
|
||||||
}
|
}
|
||||||
String token = getToken(exchange.getRequest());
|
String token = getToken(exchange.getRequest());
|
||||||
if (StringUtils.isBlank(token))
|
if (StringUtils.isBlank(token)) {
|
||||||
{
|
|
||||||
return setUnauthorizedResponse(exchange, "令牌不能为空");
|
return setUnauthorizedResponse(exchange, "令牌不能为空");
|
||||||
}
|
}
|
||||||
String userStr = sops.get(getTokenKey(token));
|
String userStr = sops.get(getTokenKey(token));
|
||||||
if (StringUtils.isNull(userStr))
|
if (StringUtils.isNull(userStr)) {
|
||||||
{
|
|
||||||
return setUnauthorizedResponse(exchange, "登录状态已过期");
|
return setUnauthorizedResponse(exchange, "登录状态已过期");
|
||||||
}
|
}
|
||||||
JSONObject obj = JSONObject.parseObject(userStr);
|
JSONObject obj = JSONObject.parseObject(userStr);
|
||||||
String userid = obj.getString("userid");
|
String userid = obj.getString("userid");
|
||||||
String username = obj.getString("username");
|
String username = obj.getString("username");
|
||||||
if (StringUtils.isBlank(userid) || StringUtils.isBlank(username))
|
if (StringUtils.isBlank(userid) || StringUtils.isBlank(username)) {
|
||||||
{
|
|
||||||
return setUnauthorizedResponse(exchange, "令牌验证失败");
|
return setUnauthorizedResponse(exchange, "令牌验证失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -85,8 +82,7 @@ public class AuthFilter implements GlobalFilter, Ordered
|
||||||
return chain.filter(mutableExchange);
|
return chain.filter(mutableExchange);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mono<Void> setUnauthorizedResponse(ServerWebExchange exchange, String msg)
|
private Mono<Void> setUnauthorizedResponse(ServerWebExchange exchange, String msg) {
|
||||||
{
|
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||||
response.setStatusCode(HttpStatus.OK);
|
response.setStatusCode(HttpStatus.OK);
|
||||||
|
|
@ -99,27 +95,23 @@ public class AuthFilter implements GlobalFilter, Ordered
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getTokenKey(String token)
|
private String getTokenKey(String token) {
|
||||||
{
|
|
||||||
return CacheConstants.LOGIN_TOKEN_KEY + token;
|
return CacheConstants.LOGIN_TOKEN_KEY + token;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取请求token
|
* 获取请求token
|
||||||
*/
|
*/
|
||||||
private String getToken(ServerHttpRequest request)
|
private String getToken(ServerHttpRequest request) {
|
||||||
{
|
|
||||||
String token = request.getHeaders().getFirst(CacheConstants.HEADER);
|
String token = request.getHeaders().getFirst(CacheConstants.HEADER);
|
||||||
if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX))
|
if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX)) {
|
||||||
{
|
|
||||||
token = token.replace(CacheConstants.TOKEN_PREFIX, "");
|
token = token.replace(CacheConstants.TOKEN_PREFIX, "");
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getOrder()
|
public int getOrder() {
|
||||||
{
|
|
||||||
return -200;
|
return -200;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,13 +1,12 @@
|
||||||
package com.ruoyi.gateway.filter;
|
package com.ruoyi.gateway.filter;
|
||||||
|
|
||||||
import java.nio.CharBuffer;
|
import com.alibaba.fastjson.JSON;
|
||||||
import java.nio.charset.StandardCharsets;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import java.util.Map;
|
import com.ruoyi.common.core.utils.StringUtils;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||||
|
import com.ruoyi.gateway.service.ValidateCodeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
||||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||||
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
||||||
import org.springframework.core.io.buffer.DataBuffer;
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
|
|
@ -15,15 +14,14 @@ import org.springframework.core.io.buffer.DataBufferUtils;
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.ruoyi.common.core.utils.StringUtils;
|
|
||||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
|
||||||
import com.ruoyi.gateway.service.ValidateCodeService;
|
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.nio.CharBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证码过滤器
|
* 验证码过滤器
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.ruoyi.gateway.predicate;
|
||||||
|
|
||||||
|
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
|
||||||
|
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义断言器
|
||||||
|
* 1. 方法命名必须 配置名+RoutePredicateFactory
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class MyTimeRoutePredicateFactory extends AbstractRoutePredicateFactory<MyTimeRoutePredicateFactory.Config> {
|
||||||
|
|
||||||
|
|
||||||
|
public MyTimeRoutePredicateFactory() {
|
||||||
|
super(MyTimeRoutePredicateFactory.Config.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> shortcutFieldOrder() {
|
||||||
|
return Arrays.asList("datetime");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Predicate<ServerWebExchange> apply(MyTimeRoutePredicateFactory.Config config) {
|
||||||
|
return (GatewayPredicate) serverWebExchange -> {
|
||||||
|
System.out.println("自定义断言生效" + serverWebExchange.getRequest().getURI());
|
||||||
|
LocalDate ld = LocalDate.now();
|
||||||
|
return config.getDatetime() > ld.getYear();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
//配置类,接收配置文件中参数
|
||||||
|
public static class Config {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private Integer datetime;
|
||||||
|
|
||||||
|
public Integer getDatetime() {
|
||||||
|
return datetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDatetime(Integer datetime) {
|
||||||
|
this.datetime = datetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue