diff --git a/ruoyi-auth/src/main/resources/bootstrap.yml b/ruoyi-auth/src/main/resources/bootstrap.yml index f456b03c..58e5c939 100644 --- a/ruoyi-auth/src/main/resources/bootstrap.yml +++ b/ruoyi-auth/src/main/resources/bootstrap.yml @@ -14,10 +14,10 @@ spring: nacos: discovery: # 服务注册地址 - server-addr: 127.0.0.1:8848 + server-addr: 192.168.50.129:8848 config: # 配置中心地址 - server-addr: 127.0.0.1:8848 + server-addr: 192.168.50.129:8848 # 配置文件格式 file-extension: yml # 共享配置 diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/RuoYiGatewayApplication.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/RuoYiGatewayApplication.java index c6f7db68..23c4be6d 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/RuoYiGatewayApplication.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/RuoYiGatewayApplication.java @@ -3,6 +3,7 @@ package com.ruoyi.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * 网关启动程序 diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/CacheRequestFilter.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/CacheRequestFilter.java index 2045d3ed..9f0229e5 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/CacheRequestFilter.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/CacheRequestFilter.java @@ -2,6 +2,7 @@ package com.ruoyi.gateway.filter; import java.util.Collections; import java.util.List; + import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.OrderedGatewayFilter; @@ -17,40 +18,32 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @Component -public class CacheRequestFilter extends AbstractGatewayFilterFactory -{ - public CacheRequestFilter() - { +public class CacheRequestFilter extends AbstractGatewayFilterFactory { + public CacheRequestFilter() { super(Config.class); } @Override - public String name() - { + public String name() { return "CacheRequestFilter"; } @Override - public GatewayFilter apply(Config config) - { + public GatewayFilter apply(Config config) { CacheRequestGatewayFilter cacheRequestGatewayFilter = new CacheRequestGatewayFilter(); Integer order = config.getOrder(); - if (order == null) - { + if (order == null) { return cacheRequestGatewayFilter; } return new OrderedGatewayFilter(cacheRequestGatewayFilter, order); } - public static class CacheRequestGatewayFilter implements GatewayFilter - { + public static class CacheRequestGatewayFilter implements GatewayFilter { @Override - public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) - { + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { // GET DELETE 不过滤 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 DataBufferUtils.join(exchange.getRequest().getBody()).map(dataBuffer -> { @@ -60,13 +53,10 @@ public class CacheRequestFilter extends AbstractGatewayFilterFactory { DataBufferFactory dataBufferFactory = exchange.getResponse().bufferFactory(); - ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator(exchange.getRequest()) - { + ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator(exchange.getRequest()) { @Override - public Flux getBody() - { - if (bytes.length > 0) - { + public Flux getBody() { + if (bytes.length > 0) { return Flux.just(dataBufferFactory.wrap(bytes)); } return Flux.empty(); @@ -78,22 +68,18 @@ public class CacheRequestFilter extends AbstractGatewayFilterFactory shortcutFieldOrder() - { + public List shortcutFieldOrder() { return Collections.singletonList("order"); } - static class Config - { + static class Config { private Integer order; - public Integer getOrder() - { + public Integer getOrder() { return order; } - public void setOrder(Integer order) - { + public void setOrder(Integer order) { this.order = order; } } diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/ValidateCodeFilter.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/ValidateCodeFilter.java index 561111ba..4c69daca 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/ValidateCodeFilter.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/ValidateCodeFilter.java @@ -2,10 +2,14 @@ package com.ruoyi.gateway.filter; import java.nio.CharBuffer; import java.nio.charset.StandardCharsets; +import java.util.Map; import java.util.concurrent.atomic.AtomicReference; + import org.springframework.beans.factory.annotation.Autowired; 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.support.ServerWebExchangeUtils; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.http.server.reactive.ServerHttpRequest; @@ -16,17 +20,17 @@ 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.Mono; /** * 验证码过滤器 - * + * * @author ruoyi */ @Component -public class ValidateCodeFilter extends AbstractGatewayFilterFactory -{ +public class ValidateCodeFilter extends AbstractGatewayFilterFactory { private final static String AUTH_URL = "/auth/login"; @Autowired @@ -37,25 +41,24 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory private static final String UUID = "uuid"; @Override - public GatewayFilter apply(Object config) - { + public GatewayFilter apply(Object config) { return (exchange, chain) -> { + Map exchange1 = ServerWebExchangeUtils.getUriTemplateVariables(exchange); + String segment = exchange1.get("segment"); + + ServerHttpRequest request = exchange.getRequest(); // 非登录请求,不处理 - if (!StringUtils.containsIgnoreCase(request.getURI().getPath(), AUTH_URL)) - { + if (!StringUtils.containsIgnoreCase(request.getURI().getPath(), AUTH_URL)) { return chain.filter(exchange); } - try - { + try { String rspStr = resolveBodyFromRequest(request); JSONObject obj = JSONObject.parseObject(rspStr); validateCodeService.checkCapcha(obj.getString(CODE), obj.getString(UUID)); - } - catch (Exception e) - { + } catch (Exception e) { ServerHttpResponse response = exchange.getResponse(); response.getHeaders().add("Content-Type", "application/json;charset=UTF-8"); return exchange.getResponse().writeWith( @@ -65,8 +68,7 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory }; } - private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest) - { + private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest) { // 获取请求体 Flux body = serverHttpRequest.getBody(); AtomicReference bodyRef = new AtomicReference<>(); diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java index 873ef39b..9ef3fa35 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java @@ -2,10 +2,13 @@ package com.ruoyi.gateway.service.impl; import java.awt.image.BufferedImage; import java.io.IOException; +import java.util.Map; import java.util.concurrent.TimeUnit; import javax.annotation.Resource; import javax.imageio.ImageIO; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; import org.springframework.stereotype.Service; import org.springframework.util.FastByteArrayOutputStream; import com.google.code.kaptcha.Producer; @@ -24,8 +27,7 @@ import com.ruoyi.gateway.service.ValidateCodeService; * @author ruoyi */ @Service -public class ValidateCodeServiceImpl implements ValidateCodeService -{ +public class ValidateCodeServiceImpl implements ValidateCodeService { @Resource(name = "captchaProducer") private Producer captchaProducer; @@ -36,14 +38,14 @@ public class ValidateCodeServiceImpl implements ValidateCodeService private RedisService redisService; // 验证码类型 - private String captchaType = "math"; + private String captchaType = "char"; /** * 生成验证码 */ @Override - public AjaxResult createCapcha() throws IOException, CaptchaException - { + public AjaxResult createCapcha() throws IOException, CaptchaException { + // 保存验证码信息 String uuid = IdUtils.simpleUUID(); String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; @@ -52,15 +54,12 @@ public class ValidateCodeServiceImpl implements ValidateCodeService BufferedImage image = null; // 生成验证码 - if ("math".equals(captchaType)) - { + if ("math".equals(captchaType)) { String capText = captchaProducerMath.createText(); capStr = capText.substring(0, capText.lastIndexOf("@")); code = capText.substring(capText.lastIndexOf("@") + 1); image = captchaProducerMath.createImage(capStr); - } - else if ("char".equals(captchaType)) - { + } else if ("char".equals(captchaType)) { capStr = code = captchaProducer.createText(); image = captchaProducer.createImage(capStr); } @@ -68,12 +67,9 @@ public class ValidateCodeServiceImpl implements ValidateCodeService redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); // 转换流信息写出 FastByteArrayOutputStream os = new FastByteArrayOutputStream(); - try - { + try { ImageIO.write(image, "jpg", os); - } - catch (IOException e) - { + } catch (IOException e) { return AjaxResult.error(e.getMessage()); } @@ -87,22 +83,18 @@ public class ValidateCodeServiceImpl implements ValidateCodeService * 校验验证码 */ @Override - public void checkCapcha(String code, String uuid) throws CaptchaException - { - if (StringUtils.isEmpty(code)) - { + public void checkCapcha(String code, String uuid) throws CaptchaException { + if (StringUtils.isEmpty(code)) { throw new CaptchaException("验证码不能为空"); } - if (StringUtils.isEmpty(uuid)) - { + if (StringUtils.isEmpty(uuid)) { throw new CaptchaException("验证码已失效"); } String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; String captcha = redisService.getCacheObject(verifyKey); redisService.deleteObject(verifyKey); - if (!code.equalsIgnoreCase(captcha)) - { + if (!code.equalsIgnoreCase(captcha)) { throw new CaptchaException("验证码错误"); } } diff --git a/ruoyi-gateway/src/main/resources/bootstrap.yml b/ruoyi-gateway/src/main/resources/bootstrap.yml index 0aa81c22..2f9babca 100644 --- a/ruoyi-gateway/src/main/resources/bootstrap.yml +++ b/ruoyi-gateway/src/main/resources/bootstrap.yml @@ -3,7 +3,7 @@ server: port: 8080 # Spring -spring: +spring: application: # 应用名称 name: ruoyi-gateway @@ -16,10 +16,10 @@ spring: nacos: discovery: # 服务注册地址 - server-addr: 127.0.0.1:8848 + server-addr: 192.168.50.129:8848 config: # 配置中心地址 - server-addr: 127.0.0.1:8848 + server-addr: 192.168.50.129:8848 # 配置文件格式 file-extension: yml # 共享配置 @@ -30,12 +30,12 @@ spring: eager: true transport: # 控制台地址 - dashboard: 127.0.0.1:8718 + dashboard: 192.168.50.129:8858 # nacos配置持久化 datasource: ds1: nacos: - server-addr: 127.0.0.1:8848 + server-addr: 192.168.50.129:8848 dataId: sentinel-ruoyi-gateway groupId: DEFAULT_GROUP data-type: json diff --git a/ruoyi-modules/ruoyi-system/src/main/resources/bootstrap.yml b/ruoyi-modules/ruoyi-system/src/main/resources/bootstrap.yml index 40ab7816..d18d2703 100644 --- a/ruoyi-modules/ruoyi-system/src/main/resources/bootstrap.yml +++ b/ruoyi-modules/ruoyi-system/src/main/resources/bootstrap.yml @@ -14,10 +14,10 @@ spring: nacos: discovery: # 服务注册地址 - server-addr: 127.0.0.1:8848 + server-addr: 192.168.50.129:8848 config: # 配置中心地址 - server-addr: 127.0.0.1:8848 + server-addr: 192.168.50.129:8848 # 配置文件格式 file-extension: yml # 共享配置