修改配置文件

pull/98/head
youbaokun 5 years ago
parent fd0e9202d8
commit c1f4d21caf

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 127.0.0.1:8848 server-addr: 192.168.50.129:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 127.0.0.1:8848 server-addr: 192.168.50.129:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

@ -3,6 +3,7 @@ package com.ruoyi.gateway;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/** /**
* *

@ -2,6 +2,7 @@ package com.ruoyi.gateway.filter;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
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.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter; import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
@ -17,40 +18,32 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@Component @Component
public class CacheRequestFilter extends AbstractGatewayFilterFactory<CacheRequestFilter.Config> public class CacheRequestFilter extends AbstractGatewayFilterFactory<CacheRequestFilter.Config> {
{ public CacheRequestFilter() {
public CacheRequestFilter()
{
super(Config.class); super(Config.class);
} }
@Override @Override
public String name() public String name() {
{
return "CacheRequestFilter"; return "CacheRequestFilter";
} }
@Override @Override
public GatewayFilter apply(Config config) public GatewayFilter apply(Config config) {
{
CacheRequestGatewayFilter cacheRequestGatewayFilter = new CacheRequestGatewayFilter(); CacheRequestGatewayFilter cacheRequestGatewayFilter = new CacheRequestGatewayFilter();
Integer order = config.getOrder(); Integer order = config.getOrder();
if (order == null) if (order == null) {
{
return cacheRequestGatewayFilter; return cacheRequestGatewayFilter;
} }
return new OrderedGatewayFilter(cacheRequestGatewayFilter, order); return new OrderedGatewayFilter(cacheRequestGatewayFilter, order);
} }
public static class CacheRequestGatewayFilter implements GatewayFilter public static class CacheRequestGatewayFilter implements GatewayFilter {
{
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
{
// GET DELETE 不过滤 // GET DELETE 不过滤
HttpMethod method = exchange.getRequest().getMethod(); HttpMethod method = exchange.getRequest().getMethod();
if (method == null || method.matches("GET") || method.matches("DELETE")) if (method == null || method.matches("GET") || method.matches("DELETE")) {
{
return chain.filter(exchange); return chain.filter(exchange);
} }
return DataBufferUtils.join(exchange.getRequest().getBody()).map(dataBuffer -> { return DataBufferUtils.join(exchange.getRequest().getBody()).map(dataBuffer -> {
@ -60,13 +53,10 @@ public class CacheRequestFilter extends AbstractGatewayFilterFactory<CacheReques
return bytes; return bytes;
}).defaultIfEmpty(new byte[0]).flatMap(bytes -> { }).defaultIfEmpty(new byte[0]).flatMap(bytes -> {
DataBufferFactory dataBufferFactory = exchange.getResponse().bufferFactory(); DataBufferFactory dataBufferFactory = exchange.getResponse().bufferFactory();
ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator(exchange.getRequest()) ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator(exchange.getRequest()) {
{
@Override @Override
public Flux<DataBuffer> getBody() public Flux<DataBuffer> getBody() {
{ if (bytes.length > 0) {
if (bytes.length > 0)
{
return Flux.just(dataBufferFactory.wrap(bytes)); return Flux.just(dataBufferFactory.wrap(bytes));
} }
return Flux.empty(); return Flux.empty();
@ -78,22 +68,18 @@ public class CacheRequestFilter extends AbstractGatewayFilterFactory<CacheReques
} }
@Override @Override
public List<String> shortcutFieldOrder() public List<String> shortcutFieldOrder() {
{
return Collections.singletonList("order"); return Collections.singletonList("order");
} }
static class Config static class Config {
{
private Integer order; private Integer order;
public Integer getOrder() public Integer getOrder() {
{
return order; return order;
} }
public void setOrder(Integer order) public void setOrder(Integer order) {
{
this.order = order; this.order = order;
} }
} }

@ -2,10 +2,14 @@ package com.ruoyi.gateway.filter;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
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.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
@ -16,6 +20,7 @@ import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.gateway.service.ValidateCodeService; 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;
@ -25,8 +30,7 @@ import reactor.core.publisher.Mono;
* @author ruoyi * @author ruoyi
*/ */
@Component @Component
public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object> public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object> {
{
private final static String AUTH_URL = "/auth/login"; private final static String AUTH_URL = "/auth/login";
@Autowired @Autowired
@ -37,25 +41,24 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
private static final String UUID = "uuid"; private static final String UUID = "uuid";
@Override @Override
public GatewayFilter apply(Object config) public GatewayFilter apply(Object config) {
{
return (exchange, chain) -> { return (exchange, chain) -> {
Map<String, String> exchange1 = ServerWebExchangeUtils.getUriTemplateVariables(exchange);
String segment = exchange1.get("segment");
ServerHttpRequest request = exchange.getRequest(); ServerHttpRequest request = exchange.getRequest();
// 非登录请求,不处理 // 非登录请求,不处理
if (!StringUtils.containsIgnoreCase(request.getURI().getPath(), AUTH_URL)) if (!StringUtils.containsIgnoreCase(request.getURI().getPath(), AUTH_URL)) {
{
return chain.filter(exchange); return chain.filter(exchange);
} }
try try {
{
String rspStr = resolveBodyFromRequest(request); String rspStr = resolveBodyFromRequest(request);
JSONObject obj = JSONObject.parseObject(rspStr); JSONObject obj = JSONObject.parseObject(rspStr);
validateCodeService.checkCapcha(obj.getString(CODE), obj.getString(UUID)); validateCodeService.checkCapcha(obj.getString(CODE), obj.getString(UUID));
} } catch (Exception e) {
catch (Exception e)
{
ServerHttpResponse response = exchange.getResponse(); ServerHttpResponse response = exchange.getResponse();
response.getHeaders().add("Content-Type", "application/json;charset=UTF-8"); response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
return exchange.getResponse().writeWith( return exchange.getResponse().writeWith(
@ -65,8 +68,7 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
}; };
} }
private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest) private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest) {
{
// 获取请求体 // 获取请求体
Flux<DataBuffer> body = serverHttpRequest.getBody(); Flux<DataBuffer> body = serverHttpRequest.getBody();
AtomicReference<String> bodyRef = new AtomicReference<>(); AtomicReference<String> bodyRef = new AtomicReference<>();

@ -2,10 +2,13 @@ package com.ruoyi.gateway.service.impl;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.FastByteArrayOutputStream; import org.springframework.util.FastByteArrayOutputStream;
import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.Producer;
@ -24,8 +27,7 @@ import com.ruoyi.gateway.service.ValidateCodeService;
* @author ruoyi * @author ruoyi
*/ */
@Service @Service
public class ValidateCodeServiceImpl implements ValidateCodeService public class ValidateCodeServiceImpl implements ValidateCodeService {
{
@Resource(name = "captchaProducer") @Resource(name = "captchaProducer")
private Producer captchaProducer; private Producer captchaProducer;
@ -36,14 +38,14 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
private RedisService redisService; private RedisService redisService;
// 验证码类型 // 验证码类型
private String captchaType = "math"; private String captchaType = "char";
/** /**
* *
*/ */
@Override @Override
public AjaxResult createCapcha() throws IOException, CaptchaException public AjaxResult createCapcha() throws IOException, CaptchaException {
{
// 保存验证码信息 // 保存验证码信息
String uuid = IdUtils.simpleUUID(); String uuid = IdUtils.simpleUUID();
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
@ -52,15 +54,12 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
BufferedImage image = null; BufferedImage image = null;
// 生成验证码 // 生成验证码
if ("math".equals(captchaType)) if ("math".equals(captchaType)) {
{
String capText = captchaProducerMath.createText(); String capText = captchaProducerMath.createText();
capStr = capText.substring(0, capText.lastIndexOf("@")); capStr = capText.substring(0, capText.lastIndexOf("@"));
code = capText.substring(capText.lastIndexOf("@") + 1); code = capText.substring(capText.lastIndexOf("@") + 1);
image = captchaProducerMath.createImage(capStr); image = captchaProducerMath.createImage(capStr);
} } else if ("char".equals(captchaType)) {
else if ("char".equals(captchaType))
{
capStr = code = captchaProducer.createText(); capStr = code = captchaProducer.createText();
image = captchaProducer.createImage(capStr); image = captchaProducer.createImage(capStr);
} }
@ -68,12 +67,9 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
// 转换流信息写出 // 转换流信息写出
FastByteArrayOutputStream os = new FastByteArrayOutputStream(); FastByteArrayOutputStream os = new FastByteArrayOutputStream();
try try {
{
ImageIO.write(image, "jpg", os); ImageIO.write(image, "jpg", os);
} } catch (IOException e) {
catch (IOException e)
{
return AjaxResult.error(e.getMessage()); return AjaxResult.error(e.getMessage());
} }
@ -87,22 +83,18 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
* *
*/ */
@Override @Override
public void checkCapcha(String code, String uuid) throws CaptchaException public void checkCapcha(String code, String uuid) throws CaptchaException {
{ if (StringUtils.isEmpty(code)) {
if (StringUtils.isEmpty(code))
{
throw new CaptchaException("验证码不能为空"); throw new CaptchaException("验证码不能为空");
} }
if (StringUtils.isEmpty(uuid)) if (StringUtils.isEmpty(uuid)) {
{
throw new CaptchaException("验证码已失效"); throw new CaptchaException("验证码已失效");
} }
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
String captcha = redisService.getCacheObject(verifyKey); String captcha = redisService.getCacheObject(verifyKey);
redisService.deleteObject(verifyKey); redisService.deleteObject(verifyKey);
if (!code.equalsIgnoreCase(captcha)) if (!code.equalsIgnoreCase(captcha)) {
{
throw new CaptchaException("验证码错误"); throw new CaptchaException("验证码错误");
} }
} }

@ -16,10 +16,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 127.0.0.1:8848 server-addr: 192.168.50.129:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 127.0.0.1:8848 server-addr: 192.168.50.129:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置
@ -30,12 +30,12 @@ spring:
eager: true eager: true
transport: transport:
# 控制台地址 # 控制台地址
dashboard: 127.0.0.1:8718 dashboard: 192.168.50.129:8858
# nacos配置持久化 # nacos配置持久化
datasource: datasource:
ds1: ds1:
nacos: nacos:
server-addr: 127.0.0.1:8848 server-addr: 192.168.50.129:8848
dataId: sentinel-ruoyi-gateway dataId: sentinel-ruoyi-gateway
groupId: DEFAULT_GROUP groupId: DEFAULT_GROUP
data-type: json data-type: json

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 127.0.0.1:8848 server-addr: 192.168.50.129:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 127.0.0.1:8848 server-addr: 192.168.50.129:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

Loading…
Cancel
Save