commit 03446dba26e282e6ff588ed697b872eb480fd9f1 Author: Sheyu-yao Date: Thu Feb 22 09:23:01 2024 +0800 初始化 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc3f89c --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/package/微服务打包部署.txt b/package/微服务打包部署.txt new file mode 100644 index 0000000..67f94c9 --- /dev/null +++ b/package/微服务打包部署.txt @@ -0,0 +1,46 @@ + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 + + + true + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + com.ysy.MqServiceApplication + ZIP + + + + + + repackage + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ee04be5 --- /dev/null +++ b/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + com.ysy + ysy-springcloud-module + 3.0.2 + pom + + ysy-common + ysy-gateway + ysy-auth + ysy-modules + ysy-modules/ysy-system + + + + 17 + 17 + UTF-8 + + + + org.springframework.boot + spring-boot-starter-parent + 3.0.2 + + + + + + + org.springframework.cloud + spring-cloud-dependencies + 2022.0.0 + pom + import + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + 2022.0.0.0-RC2 + pom + import + + + + com.ysy + ysy-common + 3.0.2 + + + + \ No newline at end of file diff --git a/ysy-auth/pom.xml b/ysy-auth/pom.xml new file mode 100644 index 0000000..88645b0 --- /dev/null +++ b/ysy-auth/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.ysy + ysy-springcloud-module + 3.0.2 + + + ysy-auth + + + 17 + 17 + UTF-8 + + + + + + com.ysy + ysy-common + + + + + org.projectlombok + lombok + true + + + + + org.springframework.boot + spring-boot-starter-test + test + + + \ No newline at end of file diff --git a/ysy-auth/src/main/java/com/ysy/auth/AuthApplication.java b/ysy-auth/src/main/java/com/ysy/auth/AuthApplication.java new file mode 100644 index 0000000..4d42798 --- /dev/null +++ b/ysy-auth/src/main/java/com/ysy/auth/AuthApplication.java @@ -0,0 +1,13 @@ +package com.ysy.auth; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableFeignClients(basePackages = "com.ysy.common.remote") +public class AuthApplication { + public static void main(String[] args) { + SpringApplication.run(AuthApplication.class, args); + } +} \ No newline at end of file diff --git a/ysy-auth/src/main/java/com/ysy/auth/controller/AuthController.java b/ysy-auth/src/main/java/com/ysy/auth/controller/AuthController.java new file mode 100644 index 0000000..f957e48 --- /dev/null +++ b/ysy-auth/src/main/java/com/ysy/auth/controller/AuthController.java @@ -0,0 +1,15 @@ +package com.ysy.auth.controller; + +import com.ysy.auth.service.AuthService; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/auth") +public class AuthController { + private final AuthService authService; + + public AuthController(AuthService authService) { + this.authService = authService; + } +} diff --git a/ysy-auth/src/main/java/com/ysy/auth/service/AuthService.java b/ysy-auth/src/main/java/com/ysy/auth/service/AuthService.java new file mode 100644 index 0000000..87e0832 --- /dev/null +++ b/ysy-auth/src/main/java/com/ysy/auth/service/AuthService.java @@ -0,0 +1,4 @@ +package com.ysy.auth.service; + +public interface AuthService { +} diff --git a/ysy-auth/src/main/java/com/ysy/auth/service/impl/AuthServiceImpl.java b/ysy-auth/src/main/java/com/ysy/auth/service/impl/AuthServiceImpl.java new file mode 100644 index 0000000..3f208fc --- /dev/null +++ b/ysy-auth/src/main/java/com/ysy/auth/service/impl/AuthServiceImpl.java @@ -0,0 +1,8 @@ +package com.ysy.auth.service.impl; + +import com.ysy.auth.service.AuthService; +import org.springframework.stereotype.Service; + +@Service +public class AuthServiceImpl implements AuthService { +} diff --git a/ysy-auth/src/main/resources/banner.txt b/ysy-auth/src/main/resources/banner.txt new file mode 100644 index 0000000..9a830ab --- /dev/null +++ b/ysy-auth/src/main/resources/banner.txt @@ -0,0 +1,9 @@ + ██ ██ ████████ ██ ██ +░░██ ██ ██░░░░░░ ░░██ ██ + ░░████ ░██ ░░████ + ░░██ ░█████████ ░░██ + ░██ ░░░░░░░░██ ░██ + ░██ ░██ ░██ + ░██ ████████ ░██ + ░░ ░░░░░░░░ ░░ +:: ysy boot :: version 1.0 \ No newline at end of file diff --git a/ysy-auth/src/main/resources/bootstrap.yml b/ysy-auth/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..20dd61d --- /dev/null +++ b/ysy-auth/src/main/resources/bootstrap.yml @@ -0,0 +1,39 @@ +server: + #服务端口 + port: 10001 + +spring: + application: + #服务名称 + name: authService + profiles: + #配置文件生效环境 + active: dev + + cloud: + nacos: + #nacos服务器地址 + server-addr: http://124.70.132.13:8848 + #nacos服务发现配置 + discovery: + #nacos服务注册命名空间ID,默认为public + namespace: 2e6a4975-46b3-4035-b77d-92b05ae48a66 + #cluster-name: SH #服务集群设置 + config: + #指定读取命名空间配置,默认为public + namespace: ${spring.cloud.nacos.discovery.namespace} + #nacos配置文件类型 + file-extension: yaml + #共享配置 + shared-configs: + - data-id: common.yaml + group: SPRING_CLOUD_COMMONS + refresh: true + + - data-id: common-mysql.yaml + group: SPRING_CLOUD_COMMONS + refresh: true + + - data-id: common-redis.yaml + group: SPRING_CLOUD_COMMONS + refresh: true \ No newline at end of file diff --git a/ysy-common/pom.xml b/ysy-common/pom.xml new file mode 100644 index 0000000..2b4edeb --- /dev/null +++ b/ysy-common/pom.xml @@ -0,0 +1,202 @@ + + + 4.0.0 + + com.ysy + ysy-springcloud-module + 3.0.2 + + + ysy-common + + + 17 + 17 + UTF-8 + + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + + io.github.openfeign + feign-httpclient + + + + + org.springframework.boot + spring-boot-starter-amqp + + + + + javax.annotation + javax.annotation-api + 1.3.2 + + + + + co.elastic.clients + elasticsearch-java + 8.11.1 + + + + + com.fasterxml.jackson.core + jackson-databind + 2.12.3 + + + + + com.auth0 + java-jwt + 4.4.0 + + + + + org.springframework.boot + spring-boot-starter-validation + + + + + com.mysql + mysql-connector-j + runtime + + + + + com.alibaba + druid-spring-boot-starter + 1.2.20 + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 3.0.2 + + + + + com.baomidou + mybatis-plus-boot-starter + 3.5.4.1 + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + org.apache.commons + commons-pool2 + + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.42 + + + + + org.apache.commons + commons-lang3 + + + + + cn.hutool + hutool-all + 5.8.20 + + + + + com.aliyun.oss + aliyun-sdk-oss + 3.15.1 + + + + + javax.xml.bind + jaxb-api + 2.3.1 + + + javax.activation + activation + 1.1.1 + + + + org.glassfish.jaxb + jaxb-runtime + 2.3.3 + + + + + org.projectlombok + lombok + true + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + \ No newline at end of file diff --git a/ysy-common/src/main/java/com/ysy/common/config/es/ElasticClient.java b/ysy-common/src/main/java/com/ysy/common/config/es/ElasticClient.java new file mode 100644 index 0000000..c5a340c --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/config/es/ElasticClient.java @@ -0,0 +1,29 @@ +package com.ysy.common.config.es; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import co.elastic.clients.json.jackson.JacksonJsonpMapper; +import co.elastic.clients.transport.rest_client.RestClientTransport; +import lombok.Data; +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "elasticsearch") +@Data +public class ElasticClient { + private String protocol; + + private String host; + + private Integer port; + + @Bean + public ElasticsearchClient getElasticsearchClient() { + RestClient restclient = RestClient.builder(new HttpHost(host, port, protocol)).build(); + RestClientTransport transport = new RestClientTransport(restclient, new JacksonJsonpMapper()); + return new ElasticsearchClient(transport); + } +} diff --git a/ysy-common/src/main/java/com/ysy/common/config/mp/MybatisPlusConfig.java b/ysy-common/src/main/java/com/ysy/common/config/mp/MybatisPlusConfig.java new file mode 100644 index 0000000..b4be6b2 --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/config/mp/MybatisPlusConfig.java @@ -0,0 +1,25 @@ +package com.ysy.common.config.mp; + +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MybatisPlusConfig { + + public MybatisPlusConfig () { + System.out.println("MybatisPlusConfig"); + } + + /** + * 添加分页插件 + */ + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor() { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); + return interceptor; + } +} diff --git a/ysy-common/src/main/java/com/ysy/common/config/rabbitmq/ErrorMessageConfig.java b/ysy-common/src/main/java/com/ysy/common/config/rabbitmq/ErrorMessageConfig.java new file mode 100644 index 0000000..3ccf72a --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/config/rabbitmq/ErrorMessageConfig.java @@ -0,0 +1,45 @@ +package com.ysy.common.config.rabbitmq; + +import org.springframework.amqp.core.Binding; +import org.springframework.amqp.core.BindingBuilder; +import org.springframework.amqp.core.DirectExchange; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.amqp.rabbit.retry.MessageRecoverer; +import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer; +import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; +import org.springframework.amqp.support.converter.MessageConverter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnProperty(name = "spring.rabbitmq.listener.simple.retry.enabled", havingValue = "true") +public class ErrorMessageConfig { + @Bean + public DirectExchange errorMessageExchange(){ + return new DirectExchange("error.direct"); + } + @Bean + public Queue errorQueue(){ + return new Queue("error.queue", true); + } + @Bean + public Binding errorBinding(Queue errorQueue, DirectExchange errorMessageExchange){ + return BindingBuilder.bind(errorQueue).to(errorMessageExchange).with("error"); + } + + @Bean + public MessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate){ + return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "error"); + } + + /** + * 替换默认JDK序列化,org.springframework.amqp.support.converter.MessageConverter + * @return 用json格式替代默认格式 + */ + @Bean + public MessageConverter jsonConverter() { + return new Jackson2JsonMessageConverter(); + } +} \ No newline at end of file diff --git a/ysy-common/src/main/java/com/ysy/common/config/rabbitmq/RabbitPublisherConfig.java b/ysy-common/src/main/java/com/ysy/common/config/rabbitmq/RabbitPublisherConfig.java new file mode 100644 index 0000000..1a9a3ee --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/config/rabbitmq/RabbitPublisherConfig.java @@ -0,0 +1,57 @@ +package com.ysy.common.config.rabbitmq; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.core.ReturnedMessage; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; +import org.springframework.amqp.support.converter.MessageConverter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.PostConstruct; + +@Slf4j +@Configuration +public class RabbitPublisherConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback { + @Autowired + private RabbitTemplate rabbitTemplate; + + @PostConstruct + public void init() { + //设置开启Mandatory,才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数 + rabbitTemplate.setMandatory(true); + rabbitTemplate.setConfirmCallback(this); + rabbitTemplate.setReturnsCallback(this); + } + + /** + * confirm回调,(前提: 确认模式开启:publisher-confirm-type: correlated) + * CorrelationData数据可以在rabbitTemplate.convertAndSend时传入,并这种CorrelationData的setId参数,回调时能取到 + * @param correlationData 相关数据,可以在发送消息时,进行设置该参数 + * @param ack 结果 + * @param cause 原因 + */ + @Override + public void confirm(CorrelationData correlationData, boolean ack, String cause) { + if (ack) { + log.info("【ConfirmCallback】消息已经送达Exchange,ack已发"); + } else { + log.warn("【ConfirmCallback】消息没有送达Exchange"); + // todo 做一些处理,让消息再次发送。 消息缓存或入库,邮件提醒运维 + } + } + + /** + * return函数,(前提:退回模式开启:yml中publisher-returns: true) + * @param returned 返回的信息 + */ + @Override + public void returnedMessage(ReturnedMessage returned) { + log.debug("收到消息的return callback,exchange:{}, key:{}, msg:{}, code:{}, text:{}", + returned.getExchange(), returned.getRoutingKey(), returned.getMessage(), + returned.getReplyCode(), returned.getReplyText()); + // todo 处理 邮件发送,缓存或存到数据库 + } +} diff --git a/ysy-common/src/main/java/com/ysy/common/contant/ResultConstants.java b/ysy-common/src/main/java/com/ysy/common/contant/ResultConstants.java new file mode 100644 index 0000000..3c23aa6 --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/contant/ResultConstants.java @@ -0,0 +1,10 @@ +package com.ysy.common.contant; + +public class ResultConstants { + public final static int SUCCESS_CODE = 200; + public final static int FAIL_CODE = 300; + public final static int ERROR_CODE = 500; + public final static String SUCCESS_MSG = "success"; + public final static String FAIL_MSG = "fail"; + public final static String ERROR_MSG = "error"; +} diff --git a/ysy-common/src/main/java/com/ysy/common/contant/TokenConstants.java b/ysy-common/src/main/java/com/ysy/common/contant/TokenConstants.java new file mode 100644 index 0000000..eb50791 --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/contant/TokenConstants.java @@ -0,0 +1,9 @@ +package com.ysy.common.contant; + +public class TokenConstants { + public final static String TOKEN = "token"; + public final static String TOKEN_LOGIN = "tokenLogin"; + public final static String TOKEN_KEY = "tokenKey"; + public final static String USER_ID = "userId"; + public final static String USER_NAME = "username"; +} diff --git a/ysy-common/src/main/java/com/ysy/common/domain/result/PageResult.java b/ysy-common/src/main/java/com/ysy/common/domain/result/PageResult.java new file mode 100644 index 0000000..2c961ad --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/domain/result/PageResult.java @@ -0,0 +1,30 @@ +package com.ysy.common.domain.result; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +@Data +public class PageResult implements Serializable { + + private long total; + + private List list; + + public PageResult() { + } + + public PageResult(long total, List list) { + this.total = total; + this.list = list; + } + + public static PageResult toPageResult(long total, List list){ + return new PageResult(total , list); + } + + public static Result> toResult(long total, List list){ + return Result.success(PageResult.toPageResult(total, list)); + } +} diff --git a/ysy-common/src/main/java/com/ysy/common/domain/result/Result.java b/ysy-common/src/main/java/com/ysy/common/domain/result/Result.java new file mode 100644 index 0000000..27a8048 --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/domain/result/Result.java @@ -0,0 +1,83 @@ +package com.ysy.common.domain.result; + +import com.ysy.common.contant.ResultConstants; +import lombok.Data; +import java.io.Serializable; + +@Data +public class Result implements Serializable { + private static final long serialVersionUID = 1L; + + public static final int SUCCESS = ResultConstants.SUCCESS_CODE; + + public static final int FAIL = ResultConstants.FAIL_CODE; + + public static final int ERROR = ResultConstants.ERROR_CODE; + + public static final String SUCCESS_MSG = ResultConstants.SUCCESS_MSG; + + public static final String FAIL_MSG = ResultConstants.FAIL_MSG; + + public static final String ERROR_MSG = ResultConstants.ERROR_MSG; + + //field + private int code; + private String msg; + private T data; + + public static Result success() { + return restResult(SUCCESS, SUCCESS_MSG, null); + } + + public static Result success(String msg) { + return restResult(SUCCESS, msg, null); + } + + public static Result success(T data) { + return restResult(SUCCESS, SUCCESS_MSG, data); + } + + public static Result success(String msg, T data) { + return restResult(SUCCESS, msg, data); + } + + public static Result fail() { + return restResult(FAIL, FAIL_MSG, null); + } + + public static Result fail(String msg) { + return restResult(FAIL, msg, null); + } + + public static Result error() { + return restResult(ERROR, ERROR_MSG, null); + } + + public static Result error(String msg) { + return restResult(ERROR, msg, null); + } + + private static Result restResult(int code, String msg, T data) { + Result apiResult = new Result<>(); + apiResult.setCode(code); + apiResult.setMsg(msg); + apiResult.setData(data); + return apiResult; + } + + /** + * 成功 + * @return 成功:true + */ + public boolean isSuccess(){ + return this.code == SUCCESS; + } + + /** + * 失败 + * @return 失败:false + */ + public boolean isError(){ + return !isSuccess(); + } +} diff --git a/ysy-common/src/main/java/com/ysy/common/exception/GlobalExceptionHandler.java b/ysy-common/src/main/java/com/ysy/common/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..40aa988 --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/exception/GlobalExceptionHandler.java @@ -0,0 +1,28 @@ +package com.ysy.common.exception; + +import com.alibaba.fastjson2.JSON; +import com.ysy.common.domain.result.Result; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +@Configuration +@Slf4j +public class GlobalExceptionHandler { + @ExceptionHandler(IllegalArgumentException.class) + public Result illegalArgumentExceptionHandler(IllegalArgumentException e) { + log.error("服务端报错:[{}]", e.getMessage(), e.getLocalizedMessage(), e); + return Result.fail( +// JSON.toJSONString() + ); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + public Result methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) { + log.error("服务端报错:[{}]", e.getMessage(), e.getLocalizedMessage(), e); + return Result.fail(JSON.toJSONString(e.getBindingResult())); + } +} diff --git a/ysy-common/src/main/java/com/ysy/common/utils/gateway/GatewayUtils.java b/ysy-common/src/main/java/com/ysy/common/utils/gateway/GatewayUtils.java new file mode 100644 index 0000000..7a446db --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/utils/gateway/GatewayUtils.java @@ -0,0 +1,70 @@ +package com.ysy.common.utils.gateway; + +import com.alibaba.fastjson2.JSONObject; +import com.ysy.common.domain.result.Result; +import com.ysy.common.utils.tools.StringUtils; +import lombok.extern.log4j.Log4j2; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +@Log4j2 +public class GatewayUtils { + /** + * 添加请求头参数 + * @param mutate 修改对象 + * @param key 键 + * @param value 值 + */ + public static void addHeader(ServerHttpRequest.Builder mutate, String key, Object value) { + if (StringUtils.isEmpty(key)){ + log.warn("添加请求头参数键不可以为空"); + return; + } + if (value == null) { + log.warn("添加请求头参数:[{}]值为空",key); + return; + } + String valueStr = value.toString(); + mutate.header(key, valueStr); + log.info("添加请求头参数成功 - 键:[{}] , 值:[{}]", key , value); + } + + /** + * 删除请求头参数 + * @param mutate 修改对象 + * @param key 键 + */ + public static void removeHeader(ServerHttpRequest.Builder mutate, String key) { + if (StringUtils.isEmpty(key)){ + log.warn("删除请求头参数键不可以为空"); + return; + } + mutate.headers(httpHeaders -> httpHeaders.remove(key)).build(); + log.info("删除请求头参数 - 键:[{}]",key); + } + /** + * 错误结果响应 + * @param exchange 响应上下文 + * @param msg 响应消息 + * @return + */ + public static Mono errorResponse(ServerWebExchange exchange, String msg) { + ServerHttpResponse response = exchange.getResponse(); + //设置HTTP响应头状态 + response.setStatusCode(HttpStatus.OK); + //设置HTTP响应头文本格式 + response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json"); + //定义响应内容 + Result result = Result.error(msg); + String resultJson = JSONObject.toJSONString(result); + log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson); + DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes()); + //进行响应 + return response.writeWith(Mono.just(dataBuffer)); + } + } diff --git a/ysy-common/src/main/java/com/ysy/common/utils/jwt/JwtUtil.java b/ysy-common/src/main/java/com/ysy/common/utils/jwt/JwtUtil.java new file mode 100644 index 0000000..05fa5d1 --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/utils/jwt/JwtUtil.java @@ -0,0 +1,37 @@ +package com.ysy.common.utils.jwt; + +import com.auth0.jwt.JWT; +import com.auth0.jwt.JWTVerifier; +import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.interfaces.Claim; +import com.auth0.jwt.interfaces.DecodedJWT; + +import java.util.Date; +import java.util.Map; + +public class JwtUtil { + private final static Date EXPIRE_TIME = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24L); + private final static String SECRET = "qwertyuiopasdfghjkl"; + private final static String CLAIM_NAME = "claim_name"; + + public static String createToken(Map claim) { + String token = JWT.create() + .withClaim(CLAIM_NAME, claim) + //.withExpiresAt(EXPIRE_TIME) + .sign(Algorithm.HMAC256(SECRET)); + return token; + } + + public static DecodedJWT decodedJWT(String token) { + JWTVerifier build = JWT.require(Algorithm.HMAC256(SECRET)) + .build(); + DecodedJWT decodedJWT = build.verify(token); + return decodedJWT; + } + + public static Claim getClaim(String token) { + DecodedJWT decodedJWT = decodedJWT(token); + Map claims = decodedJWT.getClaims(); + return claims.get(CLAIM_NAME); + } +} diff --git a/ysy-common/src/main/java/com/ysy/common/utils/jwt/SecurityUtils.java b/ysy-common/src/main/java/com/ysy/common/utils/jwt/SecurityUtils.java new file mode 100644 index 0000000..b82aebc --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/utils/jwt/SecurityUtils.java @@ -0,0 +1,53 @@ +package com.ysy.common.utils.jwt; + + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; + +/** + * 安全服务工具类 + * + * @author ruoyi + */ +public class SecurityUtils { + /** + * 生成BCryptPasswordEncoder密码 + * + * @param password 密码 + * + * @return 加密字符串 + */ + public static String encryptPassword (String password, String salt) { + return encryptMD5(password, salt); + } + + /** + * 判断密码是否相同 + * + * @param password 真实密码 + * @param encodedPassword 加密后字符 + * + * @return 结果 + */ + public static boolean matchesPassword (String password, String salt, String encodedPassword) { + return encryptMD5(password, salt).equals(encodedPassword); + } + + /** + * 计算字符串的MD5加密值,并返回Base64编码的字符串。 + * @param password 要加密的字符串 + * @return 加密后的Base64编码字符串 + */ + public static String encryptMD5(String password, String salt) { + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update((password + salt).getBytes()); // 加盐处理 + byte[] digest = md.digest(); + String encodeToString = Base64.getEncoder().encodeToString(digest); + return encodeToString; + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } +} diff --git a/ysy-common/src/main/java/com/ysy/common/utils/oss/AliOssUtil.java b/ysy-common/src/main/java/com/ysy/common/utils/oss/AliOssUtil.java new file mode 100644 index 0000000..478a42c --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/utils/oss/AliOssUtil.java @@ -0,0 +1,66 @@ +package com.ysy.common.utils.oss; + +import com.aliyun.oss.ClientException; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.OSSException; +import com.aliyun.oss.model.PutObjectRequest; +import com.aliyun.oss.model.PutObjectResult; + +import java.io.File; +import java.io.InputStream; + +public class AliOssUtil { + private final static String ENDPOINT = "https://oss-cn-beijing.aliyuncs.com"; + // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 + // EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); + // 填写Bucket名称,例如examplebucket。 + private final static String ACCESS_KEY_ID = "LTAI5tLBQ6W5bZKvw34YX2i8"; + private final static String ACCESS_KEY_SECRET = "mHnzyqJv6CL3p0uy4xyhOvvA73sYOO"; + private final static String BUCKET_NAME = "ysy-bucket01"; + + public static String fileUpload(String objectName, InputStream inputStream) throws Exception { + // 创建OSSClient实例。 + OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET); + + // 返回url地址 + String url = ""; + + try { + // 填写字符串。 + String content = "Hello OSS,你好世界"; + + // 创建PutObjectRequest对象。 + PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName, inputStream); + + // 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。 + // ObjectMetadata metadata = new ObjectMetadata(); + // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString()); + // metadata.setObjectAcl(CannedAccessControlList.Private); + // putObjectRequest.setMetadata(metadata); + + // 上传字符串。 + PutObjectResult result = ossClient.putObject(putObjectRequest); + + // 返回url地址 + url = "https://" + BUCKET_NAME + "." + ENDPOINT.substring(ENDPOINT.lastIndexOf("/") + 1) + "/" + objectName; + } catch (OSSException oe) { + System.out.println("Caught an OSSException, which means your request made it to OSS, " + + "but was rejected with an error response for some reason."); + System.out.println("Error Message:" + oe.getErrorMessage()); + System.out.println("Error Code:" + oe.getErrorCode()); + System.out.println("Request ID:" + oe.getRequestId()); + System.out.println("Host ID:" + oe.getHostId()); + } catch (ClientException ce) { + System.out.println("Caught an ClientException, which means the client encountered " + + "a serious internal problem while trying to communicate with OSS, " + + "such as not being able to access the network."); + System.out.println("Error Message:" + ce.getMessage()); + } finally { + if (ossClient != null) { + ossClient.shutdown(); + } + } + return url; + } +} diff --git a/ysy-common/src/main/java/com/ysy/common/utils/tools/StringUtils.java b/ysy-common/src/main/java/com/ysy/common/utils/tools/StringUtils.java new file mode 100644 index 0000000..8191f3a --- /dev/null +++ b/ysy-common/src/main/java/com/ysy/common/utils/tools/StringUtils.java @@ -0,0 +1,68 @@ +package com.ysy.common.utils.tools; + +import org.springframework.util.AntPathMatcher; + +import java.util.Collection; +import java.util.List; + +/** + * @author DongZl + * @description: 字符串处理工具类 + */ +public class StringUtils extends org.apache.commons.lang3.StringUtils { + + /** + * * 判断一个对象是否为空 + * + * @param object Object + * @return true:为空 false:非空 + */ + public static boolean isNull(Object object) { + return object == null; + } + + /** + * * 判断一个Collection是否为空, 包含List,Set,Queue + * + * @param coll 要判断的Collection + * @return true:为空 false:非空 + */ + public static boolean isEmpty(Collection coll) { + return isNull(coll) || coll.isEmpty(); + } + + /** + * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串 + * + * @param str 指定字符串 + * @param strs 需要检查的字符串数组 + * @return 是否匹配 + */ + public static boolean matches(String str, List strs) { + if (isEmpty(str) || isEmpty(strs)) { + return false; + } + for (String pattern : strs) { + if (isMatch(pattern, str)) + { + return true; + } + } + return false; + } + + /** + * 判断url是否与规则配置: + * ? 表示单个字符; + * * 表示一层路径内的任意字符串,不可跨层级; + * ** 表示任意层路径; + * + * @param pattern 匹配规则 + * @param url 需要匹配的url + * @return + */ + public static boolean isMatch(String pattern, String url) { + AntPathMatcher matcher = new AntPathMatcher(); + return matcher.match(pattern, url); + } +} diff --git a/ysy-gateway/pom.xml b/ysy-gateway/pom.xml new file mode 100644 index 0000000..3c4a1c4 --- /dev/null +++ b/ysy-gateway/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.ysy + ysy-springcloud-module + 3.0.2 + + + ysy-gateway + + + 17 + 17 + UTF-8 + + + + + com.ysy + ysy-common + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + + org.projectlombok + lombok + true + + + \ No newline at end of file diff --git a/ysy-gateway/src/main/java/com/ysy/gateway/GatewayApplication.java b/ysy-gateway/src/main/java/com/ysy/gateway/GatewayApplication.java new file mode 100644 index 0000000..68099d2 --- /dev/null +++ b/ysy-gateway/src/main/java/com/ysy/gateway/GatewayApplication.java @@ -0,0 +1,11 @@ +package com.ysy.gateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class GatewayApplication { + public static void main(String[] args) { + SpringApplication.run(GatewayApplication.class, args); + } +} \ No newline at end of file diff --git a/ysy-gateway/src/main/java/com/ysy/gateway/white/AuthProperties.java b/ysy-gateway/src/main/java/com/ysy/gateway/white/AuthProperties.java new file mode 100644 index 0000000..4e9a849 --- /dev/null +++ b/ysy-gateway/src/main/java/com/ysy/gateway/white/AuthProperties.java @@ -0,0 +1,17 @@ +package com.ysy.gateway.white; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Data +@ConfigurationProperties(prefix = "auth") +@Component +@RefreshScope +public class AuthProperties { + private List excludePaths; + private List includePaths; +} diff --git a/ysy-gateway/src/main/resources/banner.txt b/ysy-gateway/src/main/resources/banner.txt new file mode 100644 index 0000000..9a830ab --- /dev/null +++ b/ysy-gateway/src/main/resources/banner.txt @@ -0,0 +1,9 @@ + ██ ██ ████████ ██ ██ +░░██ ██ ██░░░░░░ ░░██ ██ + ░░████ ░██ ░░████ + ░░██ ░█████████ ░░██ + ░██ ░░░░░░░░██ ░██ + ░██ ░██ ░██ + ░██ ████████ ░██ + ░░ ░░░░░░░░ ░░ +:: ysy boot :: version 1.0 \ No newline at end of file diff --git a/ysy-gateway/src/main/resources/bootstrap.yml b/ysy-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..3345573 --- /dev/null +++ b/ysy-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,33 @@ +server: + port: 10010 +spring: + application: + #服务名称 + name: gateway + profiles: + #配置文件生效环境 + active: dev + + cloud: + nacos: + #nacos服务器地址 + server-addr: http://124.70.132.13:8848 + #nacos服务发现配置 + discovery: + #nacos服务注册命名空间ID,默认为public + namespace: 2e6a4975-46b3-4035-b77d-92b05ae48a66 + #nacos配置文件配置 + config: + #指定读取命名空间配置,默认为public + namespace: ${spring.cloud.nacos.discovery.namespace} + #nacos配置文件类型 + file-extension: yaml + #共享配置 + shared-configs: + - data-id: common.yaml + group: SPRING_CLOUD_COMMONS + refresh: true + + - data-id: common-redis.yaml + group: SPRING_CLOUD_COMMONS + refresh: true \ No newline at end of file diff --git a/ysy-modules/pom.xml b/ysy-modules/pom.xml new file mode 100644 index 0000000..d6a17dd --- /dev/null +++ b/ysy-modules/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + com.ysy + ysy-springcloud-module + 3.0.2 + + + ysy-modules + + + 17 + 17 + UTF-8 + + + \ No newline at end of file diff --git a/ysy-modules/ysy-system/pom.xml b/ysy-modules/ysy-system/pom.xml new file mode 100644 index 0000000..a3ed350 --- /dev/null +++ b/ysy-modules/ysy-system/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + com.ysy + ysy-springcloud-module + 3.0.2 + ../../pom.xml + + + ysy-system + + + 17 + 17 + UTF-8 + + + + + + com.ysy + ysy-common + + + + + org.projectlombok + lombok + true + + + + + org.springframework.boot + spring-boot-starter-test + test + + + \ No newline at end of file diff --git a/ysy-modules/ysy-system/src/main/java/com/ysy/system/SystemApplication.java b/ysy-modules/ysy-system/src/main/java/com/ysy/system/SystemApplication.java new file mode 100644 index 0000000..7a9df4b --- /dev/null +++ b/ysy-modules/ysy-system/src/main/java/com/ysy/system/SystemApplication.java @@ -0,0 +1,13 @@ +package com.ysy.system; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableFeignClients(basePackages = "com.ysy.common.remote") +public class SystemApplication { + public static void main(String[] args) { + SpringApplication.run(SystemApplication.class, args); + } +} \ No newline at end of file diff --git a/ysy-modules/ysy-system/src/main/java/com/ysy/system/controller/UserController.java b/ysy-modules/ysy-system/src/main/java/com/ysy/system/controller/UserController.java new file mode 100644 index 0000000..c9c9d79 --- /dev/null +++ b/ysy-modules/ysy-system/src/main/java/com/ysy/system/controller/UserController.java @@ -0,0 +1,9 @@ +package com.ysy.system.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/user") +public class UserController { +} diff --git a/ysy-modules/ysy-system/src/main/resources/banner.txt b/ysy-modules/ysy-system/src/main/resources/banner.txt new file mode 100644 index 0000000..9a830ab --- /dev/null +++ b/ysy-modules/ysy-system/src/main/resources/banner.txt @@ -0,0 +1,9 @@ + ██ ██ ████████ ██ ██ +░░██ ██ ██░░░░░░ ░░██ ██ + ░░████ ░██ ░░████ + ░░██ ░█████████ ░░██ + ░██ ░░░░░░░░██ ░██ + ░██ ░██ ░██ + ░██ ████████ ░██ + ░░ ░░░░░░░░ ░░ +:: ysy boot :: version 1.0 \ No newline at end of file diff --git a/ysy-modules/ysy-system/src/main/resources/bootstrap.yml b/ysy-modules/ysy-system/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..e077b3a --- /dev/null +++ b/ysy-modules/ysy-system/src/main/resources/bootstrap.yml @@ -0,0 +1,39 @@ +server: + #服务端口 + port: 10002 + +spring: + application: + #服务名称 + name: systemService + profiles: + #配置文件生效环境 + active: dev + + cloud: + nacos: + #nacos服务器地址 + server-addr: http://124.70.132.13:8848 + #nacos服务发现配置 + discovery: + #nacos服务注册命名空间ID,默认为public + namespace: 2e6a4975-46b3-4035-b77d-92b05ae48a66 + #cluster-name: SH #服务集群设置 + config: + #指定读取命名空间配置,默认为public + namespace: ${spring.cloud.nacos.discovery.namespace} + #nacos配置文件类型 + file-extension: yaml + #共享配置 + shared-configs: + - data-id: common.yaml + group: SPRING_CLOUD_COMMONS + refresh: true + + - data-id: common-mysql.yaml + group: SPRING_CLOUD_COMMONS + refresh: true + + - data-id: common-redis.yaml + group: SPRING_CLOUD_COMMONS + refresh: true \ No newline at end of file