From 32da4d4805b5a0ee3e19eed816e1c9d8438f3a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AE=87=E6=81=92?= <13581426+zhou030824@user.noreply.gitee.com> Date: Thu, 25 Jan 2024 10:29:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BE=E5=8C=BA=E5=AE=89=E9=98=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 38 ++++++ .idea/.gitignore | 8 ++ .idea/checkstyle-idea.xml | 16 +++ .idea/encodings.xml | 22 ++++ .idea/misc.xml | 19 +++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ community-security-auth/pom.xml | 34 +++++ .../java/com/zyh/auth/AuthApplication.java | 14 ++ .../auth/config/ConfirmCallbackConfig.java | 48 +++++++ .../zyh/auth/config/ReturnCallbackConfig.java | 41 ++++++ .../zyh/auth/controller/AuthController.java | 29 ++++ .../com/zyh/auth/feign/AuthFeignService.java | 14 ++ .../zyh/auth/feign/AuthFeignServicempl.java | 13 ++ .../zyh/auth/log/GlobalExceptionHandler.java | 14 ++ .../java/com/zyh/auth/log/LoggingAspect.java | 24 ++++ .../com/zyh/auth/service/AuthService.java | 14 ++ .../zyh/auth/service/impl/AuthServicempl.java | 72 ++++++++++ .../src/main/resources/bootstrap.yml | 31 +++++ community-security-common/pom.xml | 103 +++++++++++++++ .../com/zyh/common/constants/Constants.java | 18 +++ .../zyh/common/constants/JwtConstants.java | 29 ++++ .../constants/RabbitMQQueueNameConstants.java | 33 +++++ .../zyh/common/constants/TokenConstants.java | 24 ++++ .../main/java/com/zyh/common/domain/User.java | 19 +++ .../common/domain/request/UserRequest.java | 9 ++ .../common/domain/response/UserResponse.java | 11 ++ .../com/zyh/common/result/PageResult.java | 34 +++++ .../java/com/zyh/common/result/Result.java | 76 +++++++++++ .../java/com/zyh/common/utils/JwtUtils.java | 109 +++++++++++++++ .../com/zyh/common/utils/StringUtils.java | 68 ++++++++++ .../com/zyh/common/utils/TelSmsUtils.java | 87 ++++++++++++ community-security-gateway/pom.xml | 46 +++++++ .../com/zyh/gateway/GateWayApplication.java | 12 ++ .../zyh/gateway/config/IgnoreWhiteConfig.java | 32 +++++ .../com/zyh/gateway/filters/AuthFilters.java | 66 ++++++++++ .../com/zyh/gateway/utils/GatewayUtils.java | 98 ++++++++++++++ .../src/main/resources/bootstrap.yml | 31 +++++ .../community-security-modules-shop/pom.xml | 69 ++++++++++ .../java/com/zyh/system/ShopApplication.java | 16 +++ .../src/main/resources/bootstrap.yml | 32 +++++ .../community-security-modules-system/pom.xml | 68 ++++++++++ .../com/zyh/system/SystemApplication.java | 13 ++ .../system/controller/LoginController.java | 27 ++++ .../com/zyh/system/mapper/LoginMapper.java | 10 ++ .../com/zyh/system/service/LoginService.java | 9 ++ .../system/service/impl/LoginServicempl.java | 17 +++ .../src/main/resources/bootstrap.yml | 32 +++++ .../main/resources/mapper/SysUserMapper.xml | 9 ++ community-security-modules/pom.xml | 25 ++++ pom.xml | 66 ++++++++++ 50 files changed, 1873 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/checkstyle-idea.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 community-security-auth/pom.xml create mode 100644 community-security-auth/src/main/java/com/zyh/auth/AuthApplication.java create mode 100644 community-security-auth/src/main/java/com/zyh/auth/config/ConfirmCallbackConfig.java create mode 100644 community-security-auth/src/main/java/com/zyh/auth/config/ReturnCallbackConfig.java create mode 100644 community-security-auth/src/main/java/com/zyh/auth/controller/AuthController.java create mode 100644 community-security-auth/src/main/java/com/zyh/auth/feign/AuthFeignService.java create mode 100644 community-security-auth/src/main/java/com/zyh/auth/feign/AuthFeignServicempl.java create mode 100644 community-security-auth/src/main/java/com/zyh/auth/log/GlobalExceptionHandler.java create mode 100644 community-security-auth/src/main/java/com/zyh/auth/log/LoggingAspect.java create mode 100644 community-security-auth/src/main/java/com/zyh/auth/service/AuthService.java create mode 100644 community-security-auth/src/main/java/com/zyh/auth/service/impl/AuthServicempl.java create mode 100644 community-security-auth/src/main/resources/bootstrap.yml create mode 100644 community-security-common/pom.xml create mode 100644 community-security-common/src/main/java/com/zyh/common/constants/Constants.java create mode 100644 community-security-common/src/main/java/com/zyh/common/constants/JwtConstants.java create mode 100644 community-security-common/src/main/java/com/zyh/common/constants/RabbitMQQueueNameConstants.java create mode 100644 community-security-common/src/main/java/com/zyh/common/constants/TokenConstants.java create mode 100644 community-security-common/src/main/java/com/zyh/common/domain/User.java create mode 100644 community-security-common/src/main/java/com/zyh/common/domain/request/UserRequest.java create mode 100644 community-security-common/src/main/java/com/zyh/common/domain/response/UserResponse.java create mode 100644 community-security-common/src/main/java/com/zyh/common/result/PageResult.java create mode 100644 community-security-common/src/main/java/com/zyh/common/result/Result.java create mode 100644 community-security-common/src/main/java/com/zyh/common/utils/JwtUtils.java create mode 100644 community-security-common/src/main/java/com/zyh/common/utils/StringUtils.java create mode 100644 community-security-common/src/main/java/com/zyh/common/utils/TelSmsUtils.java create mode 100644 community-security-gateway/pom.xml create mode 100644 community-security-gateway/src/main/java/com/zyh/gateway/GateWayApplication.java create mode 100644 community-security-gateway/src/main/java/com/zyh/gateway/config/IgnoreWhiteConfig.java create mode 100644 community-security-gateway/src/main/java/com/zyh/gateway/filters/AuthFilters.java create mode 100644 community-security-gateway/src/main/java/com/zyh/gateway/utils/GatewayUtils.java create mode 100644 community-security-gateway/src/main/resources/bootstrap.yml create mode 100644 community-security-modules/community-security-modules-shop/pom.xml create mode 100644 community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/ShopApplication.java create mode 100644 community-security-modules/community-security-modules-shop/src/main/resources/bootstrap.yml create mode 100644 community-security-modules/community-security-modules-system/pom.xml create mode 100644 community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/SystemApplication.java create mode 100644 community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/controller/LoginController.java create mode 100644 community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/mapper/LoginMapper.java create mode 100644 community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/service/LoginService.java create mode 100644 community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/service/impl/LoginServicempl.java create mode 100644 community-security-modules/community-security-modules-system/src/main/resources/bootstrap.yml create mode 100644 community-security-modules/community-security-modules-system/src/main/resources/mapper/SysUserMapper.xml create mode 100644 community-security-modules/pom.xml create mode 100644 pom.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ 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/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 0000000..b7c47dd --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,16 @@ + + + + 10.12.4 + JavaOnly + true + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..cb8d5e4 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..19f49b3 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/community-security-auth/pom.xml b/community-security-auth/pom.xml new file mode 100644 index 0000000..e98b43c --- /dev/null +++ b/community-security-auth/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + com.zyh + community-security + 1.0-SNAPSHOT + + + community-security-auth + + + 17 + 17 + UTF-8 + + + + + + + com.zyh + community-security-common + + + + org.springframework.boot + spring-boot-starter-web + + + + diff --git a/community-security-auth/src/main/java/com/zyh/auth/AuthApplication.java b/community-security-auth/src/main/java/com/zyh/auth/AuthApplication.java new file mode 100644 index 0000000..be4287a --- /dev/null +++ b/community-security-auth/src/main/java/com/zyh/auth/AuthApplication.java @@ -0,0 +1,14 @@ +package com.zyh.auth; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableFeignClients +public class AuthApplication { + public static void main(String[] args) { + SpringApplication.run(AuthApplication.class, args); + System.out.println("Auth启动成功"); + } +} diff --git a/community-security-auth/src/main/java/com/zyh/auth/config/ConfirmCallbackConfig.java b/community-security-auth/src/main/java/com/zyh/auth/config/ConfirmCallbackConfig.java new file mode 100644 index 0000000..a0e6c74 --- /dev/null +++ b/community-security-auth/src/main/java/com/zyh/auth/config/ConfirmCallbackConfig.java @@ -0,0 +1,48 @@ +package com.zyh.auth.config; + +import lombok.extern.log4j.Log4j2; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +/** + * 消息发送确认配置 + * 消息发送到交换机的回调 + */ +@Component +@Log4j2 +public class ConfirmCallbackConfig implements RabbitTemplate.ConfirmCallback { + + @Autowired + private RabbitTemplate rabbitTemplate; + + /** + * @PostContruct是spring框架的注解,在⽅法上加该注解会在项⽬启动的时候执⾏该⽅法,也可以理解为在spring容器初始化的时候执 + */ + @PostConstruct + public void init() { + rabbitTemplate.setConfirmCallback(this); + } + + /** + * 交换机不管是否收到消息的一个回调方法 + * + * @param correlationData 消息相关数据 + * @param ack 交换机是否收到消息 + * @param cause 失败原因 + */ + @Override + public void confirm(CorrelationData correlationData, boolean ack, String cause) { + if (!ack) { + String exchange = correlationData.getReturned().getExchange(); + String message = correlationData.getReturned().getMessage().getBody().toString(); + // 发送异常 + log.error("消息:{},发送到交换机:{}失败,原因是:{}", message, exchange, cause); + // TODO 可以把异常信息 以及 消息的内容直接添加到 MYSQL + } + } + +} diff --git a/community-security-auth/src/main/java/com/zyh/auth/config/ReturnCallbackConfig.java b/community-security-auth/src/main/java/com/zyh/auth/config/ReturnCallbackConfig.java new file mode 100644 index 0000000..ad91da3 --- /dev/null +++ b/community-security-auth/src/main/java/com/zyh/auth/config/ReturnCallbackConfig.java @@ -0,0 +1,41 @@ +package com.zyh.auth.config; + +import lombok.extern.log4j.Log4j2; +import org.springframework.amqp.core.ReturnedMessage; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +/** + * 消息发送到队列的确认 + */ +@Component +@Log4j2 +public class ReturnCallbackConfig implements RabbitTemplate.ReturnsCallback { + + @Autowired + private RabbitTemplate rabbitTemplate; + + /** + * @PostContruct是spring框架的注解,在⽅法上加该注解会在项⽬启动的时候执⾏该⽅法,也可以理解为在spring容器初始化的时候执 + */ + @PostConstruct + public void init() { + rabbitTemplate.setReturnsCallback(this); + } + + /** + * 消息发送失败 则会执行这个方法 + * + * @param returnedMessage the returned message and metadata. + */ + @Override + public void returnedMessage(ReturnedMessage returnedMessage) { + log.error("消息:{},被交换机:{} 回退!退回原因为:{}", + returnedMessage.getMessage().toString(), returnedMessage.getExchange(), returnedMessage.getReplyText()); + // TODO 回退了所有的信息,可做补偿机制 + } + +} diff --git a/community-security-auth/src/main/java/com/zyh/auth/controller/AuthController.java b/community-security-auth/src/main/java/com/zyh/auth/controller/AuthController.java new file mode 100644 index 0000000..d9790a3 --- /dev/null +++ b/community-security-auth/src/main/java/com/zyh/auth/controller/AuthController.java @@ -0,0 +1,29 @@ +package com.zyh.auth.controller; +import com.zyh.auth.service.AuthService; +import com.zyh.common.domain.User; +import com.zyh.common.domain.request.UserRequest; +import com.zyh.common.domain.response.UserResponse; +import com.zyh.common.result.Result; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class AuthController { + @Autowired + AuthService authService; + + @PostMapping("login") + public Result login(@RequestBody UserRequest userRequest) { + Result login = authService.login(userRequest); + return login; + } + + @PostMapping("user/info") + public Result userInfo() { + User user = authService.userInfo(); + Result result = Result.success(user); + return result; + } +} diff --git a/community-security-auth/src/main/java/com/zyh/auth/feign/AuthFeignService.java b/community-security-auth/src/main/java/com/zyh/auth/feign/AuthFeignService.java new file mode 100644 index 0000000..0104697 --- /dev/null +++ b/community-security-auth/src/main/java/com/zyh/auth/feign/AuthFeignService.java @@ -0,0 +1,14 @@ +package com.zyh.auth.feign; + +import com.zyh.common.domain.User; +import com.zyh.common.result.Result; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +//@FeignClient(value = "community-security-modules-system", fallback = AuthFeignServicempl.class) +@FeignClient(value = "community-security-modules-system") +public interface AuthFeignService { + @PostMapping("/user/findPhone/{phone}") + public Result findPhone(@PathVariable String phone); +} diff --git a/community-security-auth/src/main/java/com/zyh/auth/feign/AuthFeignServicempl.java b/community-security-auth/src/main/java/com/zyh/auth/feign/AuthFeignServicempl.java new file mode 100644 index 0000000..a8cd280 --- /dev/null +++ b/community-security-auth/src/main/java/com/zyh/auth/feign/AuthFeignServicempl.java @@ -0,0 +1,13 @@ +//package com.zyh.auth.feign; +// +// +//import com.zyh.common.domain.User; +//import com.zyh.common.result.Result; +// +//public class AuthFeignServicempl implements AuthFeignService{ +// +// @Override +// public Result findPhone(String phone) { +// return null; +// } +//} diff --git a/community-security-auth/src/main/java/com/zyh/auth/log/GlobalExceptionHandler.java b/community-security-auth/src/main/java/com/zyh/auth/log/GlobalExceptionHandler.java new file mode 100644 index 0000000..b22d8c1 --- /dev/null +++ b/community-security-auth/src/main/java/com/zyh/auth/log/GlobalExceptionHandler.java @@ -0,0 +1,14 @@ +package com.zyh.auth.log; + +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +@ControllerAdvice +public class GlobalExceptionHandler { + @ResponseBody + @ExceptionHandler(Exception.class) + public String handle(Exception e) { + return "发生错误: " + e.getMessage(); + } +} diff --git a/community-security-auth/src/main/java/com/zyh/auth/log/LoggingAspect.java b/community-security-auth/src/main/java/com/zyh/auth/log/LoggingAspect.java new file mode 100644 index 0000000..6a1dd3a --- /dev/null +++ b/community-security-auth/src/main/java/com/zyh/auth/log/LoggingAspect.java @@ -0,0 +1,24 @@ +package com.zyh.auth.log; + +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Aspect +@Component +public class LoggingAspect { + private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class); + + @Before("execution(* com.zyh..*(..))") + public void logBefore() { + logger.info("接口被调用"); + } + + @AfterReturning(pointcut = "execution(* com.zyh..*(..))", returning = "result") + public void logAfterReturning(Object result) { + logger.info("接口返回: " + result); + } +} diff --git a/community-security-auth/src/main/java/com/zyh/auth/service/AuthService.java b/community-security-auth/src/main/java/com/zyh/auth/service/AuthService.java new file mode 100644 index 0000000..f68b4c5 --- /dev/null +++ b/community-security-auth/src/main/java/com/zyh/auth/service/AuthService.java @@ -0,0 +1,14 @@ +package com.zyh.auth.service; + + +import com.zyh.common.domain.User; +import com.zyh.common.domain.request.UserRequest; +import com.zyh.common.domain.response.UserResponse; +import com.zyh.common.result.Result; + +public interface AuthService { + Result login(UserRequest userRequest); + + User userInfo(); + +} diff --git a/community-security-auth/src/main/java/com/zyh/auth/service/impl/AuthServicempl.java b/community-security-auth/src/main/java/com/zyh/auth/service/impl/AuthServicempl.java new file mode 100644 index 0000000..fc7dd51 --- /dev/null +++ b/community-security-auth/src/main/java/com/zyh/auth/service/impl/AuthServicempl.java @@ -0,0 +1,72 @@ +package com.zyh.auth.service.impl; + +import com.alibaba.fastjson.JSONObject; +import com.zyh.auth.feign.AuthFeignService; +import com.zyh.auth.service.AuthService; +import com.zyh.common.constants.JwtConstants; +import com.zyh.common.constants.TokenConstants; +import com.zyh.common.domain.User; +import com.zyh.common.domain.request.UserRequest; +import com.zyh.common.domain.response.UserResponse; +import com.zyh.common.result.Result; +import com.zyh.common.utils.JwtUtils; +import com.zyh.common.utils.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; + +@Service +public class AuthServicempl implements AuthService { + @Autowired + RedisTemplate redisTemplate; + @Autowired + HttpServletRequest request; + @Autowired + AuthFeignService authFeignService; + @Override + public Result login(UserRequest userRequest) { + if (StringUtils.isAnyBlank(userRequest.getPhone(), userRequest.getPwd())) { + return Result.error("手机号或密码不能为空"); + } + if (!rule(userRequest.getPhone())) { + return Result.error("手机号格式错误"); + } + Result result = authFeignService.findPhone(userRequest.getPhone()); + User user = result.getData(); + if (user == null) { + return Result.error("手机号未注册"); + } + if (!userRequest.getPwd().equals(user.getPwd())) { + return Result.error("密码错误"); + } + String userkey = UUID.randomUUID().toString().replaceAll("-", ""); + HashMap map = new HashMap<>(); + map.put(JwtConstants.USER_KEY, userkey); + String token = JwtUtils.createToken(map); + redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY + userkey, JSONObject.toJSONString(user), 30, TimeUnit.MINUTES); + UserResponse userResponse = new UserResponse(); + userResponse.setToken(token); + userResponse.setExpirTime("30MIN"); + return Result.success(userResponse); + } + + @Override + public User userInfo() { + String token = request.getHeader(TokenConstants.TOKEN); + String userKey = JwtUtils.getUserKey(token); + String s = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey); + User user = JSONObject.parseObject(s, User.class); + return user; + } + + private boolean rule(String userPhone) { + Pattern compile = Pattern.compile("^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[189]))\\d{8}$"); + return compile.matcher(userPhone).matches(); + } +} diff --git a/community-security-auth/src/main/resources/bootstrap.yml b/community-security-auth/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..c91114a --- /dev/null +++ b/community-security-auth/src/main/resources/bootstrap.yml @@ -0,0 +1,31 @@ +# Tomcat +server: + port: 9001 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: community-security-auth + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.221.193.62:8848 + namespace: e3afd3fd-73b0-4ac1-9f9e-aad1a8aa9f46 + config: + # 配置中心地址 + server-addr: 124.221.193.62:8848 + namespace: e3afd3fd-73b0-4ac1-9f9e-aad1a8aa9f46 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/community-security-common/pom.xml b/community-security-common/pom.xml new file mode 100644 index 0000000..4ea9af6 --- /dev/null +++ b/community-security-common/pom.xml @@ -0,0 +1,103 @@ + + + 4.0.0 + + com.zyh + community-security + 1.0-SNAPSHOT + + + community-security-common + + + 17 + 17 + UTF-8 + + + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + io.jsonwebtoken + jjwt + 0.9.1 + + + + com.alibaba + fastjson + 1.2.80 + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.springframework.boot + spring-boot-starter-validation + + + + org.apache.commons + commons-lang3 + + + + org.projectlombok + lombok + + + + cn.hutool + hutool-all + 5.8.3 + + + + com.aliyun + dysmsapi20170525 + 2.0.1 + + + + org.springframework.boot + spring-boot-starter-amqp + + + + + diff --git a/community-security-common/src/main/java/com/zyh/common/constants/Constants.java b/community-security-common/src/main/java/com/zyh/common/constants/Constants.java new file mode 100644 index 0000000..939f64c --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/constants/Constants.java @@ -0,0 +1,18 @@ +package com.zyh.common.constants; + +/** + * @description: 系统常量 + * @author DongZl + */ +public class Constants { + /** + * 成功标记 + */ + public static final Integer SUCCESS = 200; + public static final String SUCCESS_MSG = "操作成功"; + /** + * 失败标记 + */ + public static final Integer ERROR = 500; + public static final String ERROR_MSG = "操作异常"; +} diff --git a/community-security-common/src/main/java/com/zyh/common/constants/JwtConstants.java b/community-security-common/src/main/java/com/zyh/common/constants/JwtConstants.java new file mode 100644 index 0000000..4d55c2a --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/constants/JwtConstants.java @@ -0,0 +1,29 @@ +package com.zyh.common.constants; + +/** + * @author DongZl + * @description: Jwt常量 + */ +public class JwtConstants { + + /** + * 用户ID字段 + */ + public static final String DETAILS_USER_ID = "user_id"; + + /** + * 用户名字段 + */ + public static final String DETAILS_USERNAME = "username"; + + /** + * 用户标识 + */ + public static final String USER_KEY = "user_key"; + + /** + * 令牌秘钥 + */ + public final static String SECRET = "abcdefghijklmnopqrstuvwxyz"; + +} diff --git a/community-security-common/src/main/java/com/zyh/common/constants/RabbitMQQueueNameConstants.java b/community-security-common/src/main/java/com/zyh/common/constants/RabbitMQQueueNameConstants.java new file mode 100644 index 0000000..933f4dd --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/constants/RabbitMQQueueNameConstants.java @@ -0,0 +1,33 @@ +package com.zyh.common.constants; + +/** + * @ClassName: + * @Description: + * @Author: zhuwenqiang + * @Date: 2023/8/23 + */ +public class RabbitMQQueueNameConstants { + + /** + * 短信队列的名称 + */ + public static final String SEND_SMS_QUEUE_NAME = "send_sms_queue"; + + /** + * 批量添加入库单队列名称 + */ + public static final String BATCH_ADD_STORAGE_QUEUE_NAME = "batch_add_storage_queue"; + + /** + * 登录日志队列名称 + */ + public static final String LOGIN_LOG_QUEUE_NAME = "login_log_queue"; + + /** + * 释放用户锁定状态队列 + */ + public static final String RELEASE_USER_LOCK_QUEUE_NAME = "release_user_lock_queue"; + + + +} diff --git a/community-security-common/src/main/java/com/zyh/common/constants/TokenConstants.java b/community-security-common/src/main/java/com/zyh/common/constants/TokenConstants.java new file mode 100644 index 0000000..2df8cd5 --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/constants/TokenConstants.java @@ -0,0 +1,24 @@ +package com.zyh.common.constants; + +/** + * @author DongZl + * @description: 令牌常量 + */ +public class TokenConstants { + /** + * 缓存有效期,默认720(分钟) + */ + public final static long EXPIRATION = 720; + /** + * 缓存刷新时间,默认120(分钟) + */ + public final static long REFRESH_TIME = 120; + /** + * 权限缓存前缀 + */ + public final static String LOGIN_TOKEN_KEY = "login_tokens:"; + /** + * token标识 + */ + public static final String TOKEN = "token"; +} diff --git a/community-security-common/src/main/java/com/zyh/common/domain/User.java b/community-security-common/src/main/java/com/zyh/common/domain/User.java new file mode 100644 index 0000000..414b5a6 --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/domain/User.java @@ -0,0 +1,19 @@ +package com.zyh.common.domain; + +import lombok.Data; + +import java.util.Date; + +@Data +public class User { + private Integer id; + private String name; + private String phone; + private String pwd; + private Date birth; + + + + + +} diff --git a/community-security-common/src/main/java/com/zyh/common/domain/request/UserRequest.java b/community-security-common/src/main/java/com/zyh/common/domain/request/UserRequest.java new file mode 100644 index 0000000..59ff141 --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/domain/request/UserRequest.java @@ -0,0 +1,9 @@ +package com.zyh.common.domain.request; + +import lombok.Data; + +@Data +public class UserRequest { + private String phone; + private String pwd; +} diff --git a/community-security-common/src/main/java/com/zyh/common/domain/response/UserResponse.java b/community-security-common/src/main/java/com/zyh/common/domain/response/UserResponse.java new file mode 100644 index 0000000..0b0491a --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/domain/response/UserResponse.java @@ -0,0 +1,11 @@ +package com.zyh.common.domain.response; + +import lombok.Data; + +@Data + +public class UserResponse { + private String token; + private String expirTime; + +} diff --git a/community-security-common/src/main/java/com/zyh/common/result/PageResult.java b/community-security-common/src/main/java/com/zyh/common/result/PageResult.java new file mode 100644 index 0000000..90ffac2 --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/result/PageResult.java @@ -0,0 +1,34 @@ +package com.zyh.common.result; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * @author DongZl + * @description: 列表返回结果集 + */ +@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/community-security-common/src/main/java/com/zyh/common/result/Result.java b/community-security-common/src/main/java/com/zyh/common/result/Result.java new file mode 100644 index 0000000..6f77b77 --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/result/Result.java @@ -0,0 +1,76 @@ +package com.zyh.common.result; + +import com.zyh.common.constants.Constants; +import lombok.Data; + +import java.io.Serializable; + +/** + * @author DongZl + * @description: 响应信息主体 + */ +@Data +public class Result implements Serializable { + + private static final long serialVersionUID = 1L; + /** + * 成功 + */ + public static final int SUCCESS = Constants.SUCCESS; + /** + * 失败 + */ + public static final int FAIL = Constants.ERROR; + /** + * 返回状态码 + */ + private int code; + /** + * 响应信息 + */ + private String msg; + /** + * 响应数据 + */ + private T data; + + public static Result success() { + return restResult(null, SUCCESS, Constants.SUCCESS_MSG); + } + + public static Result success(T data) { + return restResult(data, SUCCESS, Constants.SUCCESS_MSG); + } + + public static Result success(T data, String msg) { + return restResult(data, SUCCESS, msg); + } + + public static Result error() { + return restResult(null, FAIL, Constants.ERROR_MSG); + } + + public static Result error(String msg) { + return restResult(null, FAIL, msg); + } + + public static Result error(T data) { + return restResult(data, FAIL, Constants.ERROR_MSG); + } + + public static Result error(T data, String msg) { + return restResult(data, FAIL, msg); + } + + public static Result error(int code, String msg) { + return restResult(null, code, msg); + } + + private static Result restResult(T data, int code, String msg) { + Result apiResult = new Result<>(); + apiResult.setCode(code); + apiResult.setData(data); + apiResult.setMsg(msg); + return apiResult; + } +} diff --git a/community-security-common/src/main/java/com/zyh/common/utils/JwtUtils.java b/community-security-common/src/main/java/com/zyh/common/utils/JwtUtils.java new file mode 100644 index 0000000..9cb68fe --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/utils/JwtUtils.java @@ -0,0 +1,109 @@ +package com.zyh.common.utils; + +import com.zyh.common.constants.JwtConstants; +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; + +import java.util.Map; + +/** + * @description: Jwt工具类 + * @author DongZl + */ +public class JwtUtils { + + /** + * 秘钥 + */ + public static String secret = JwtConstants.SECRET; + + /** + * 从数据声明生成令牌 + * + * @param claims 数据声明 + * @return 令牌 + */ + public static String createToken(Map claims){ + String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact(); + return token; + } + + /** + * 从令牌中获取数据声明 + * + * @param token 令牌 + * @return 数据声明 + */ + public static Claims parseToken(String token){ + return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody(); + } + /** + * 根据令牌获取用户标识 + * + * @param token 令牌 + * @return 用户ID + */ + public static String getUserKey(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.USER_KEY); + } + /** + * 根据令牌获取用户标识 + * + * @param claims 身份信息 + * @return 用户ID + */ + public static String getUserKey(Claims claims){ + return getValue(claims, JwtConstants.USER_KEY); + } + /** + * 根据令牌获取用户ID + * + * @param token 令牌 + * @return 用户ID + */ + public static String getUserId(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.DETAILS_USER_ID); + } + /** + * 根据身份信息获取用户ID + * + * @param claims 身份信息 + * @return 用户ID + */ + public static String getUserId(Claims claims){ + return getValue(claims, JwtConstants.DETAILS_USER_ID); + } + /** + * 根据令牌获取用户名 + * + * @param token 令牌 + * @return 用户名 + */ + public static String getUserName(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.DETAILS_USERNAME); + } + /** + * 根据身份信息获取用户名 + * + * @param claims 身份信息 + * @return 用户名 + */ + public static String getUserName(Claims claims){ + return getValue(claims, JwtConstants.DETAILS_USERNAME); + } + /** + * 根据身份信息获取键值 + * + * @param claims 身份信息 + * @param key 键 + * @return 值 + */ + public static String getValue(Claims claims, String key){ + Object obj = claims.get(key); + return obj == null ? "" : obj.toString(); + } +} diff --git a/community-security-common/src/main/java/com/zyh/common/utils/StringUtils.java b/community-security-common/src/main/java/com/zyh/common/utils/StringUtils.java new file mode 100644 index 0000000..dca4412 --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/utils/StringUtils.java @@ -0,0 +1,68 @@ +package com.zyh.common.utils; + +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/community-security-common/src/main/java/com/zyh/common/utils/TelSmsUtils.java b/community-security-common/src/main/java/com/zyh/common/utils/TelSmsUtils.java new file mode 100644 index 0000000..79ba3fb --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/utils/TelSmsUtils.java @@ -0,0 +1,87 @@ +package com.zyh.common.utils; + +import com.alibaba.fastjson.JSONObject; +import com.aliyun.dysmsapi20170525.Client; +import com.aliyun.dysmsapi20170525.models.SendSmsRequest; +import com.aliyun.dysmsapi20170525.models.SendSmsResponse; +import com.aliyun.teaopenapi.models.Config; +import lombok.extern.log4j.Log4j2; + +import java.util.Map; + +/** + * 短信工具类 + */ +@Log4j2 +public class TelSmsUtils { + + /** + * 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 + */ + private static String accessKeyId = "LTAI5tMRrNoxsBPgb6bYZWTW"; + private static String accessKeySecret = "r9LXZtt3ewEG2DHQ6DbAc65F0AFRA7"; + private static String templateCode = "SMS10001"; + + /** + * 短信访问域名 + */ + private static String endpoint = "dysmsapi.aliyuncs.com"; + /** + * 短信签名 + */ + private static String signName = "登录验证"; + + /** + * 实例化短信对象 + */ + private static Client client; + + static { + log.info("初始化短信服务开始"); + long startTime = System.currentTimeMillis(); + try { + client = initClient(); + log.info("初始化短信成功:{}",signName); + } catch (Exception e) { + e.printStackTrace(); + } + log.info("初始化短信服务结束:耗时:{}MS",(System.currentTimeMillis()-startTime)); + } + /** + * 初始化短信对象 + * @return + * @throws Exception + */ + private static Client initClient() throws Exception{ + Config config = new Config() + // 您的AccessKey ID + .setAccessKeyId(accessKeyId) + // 您的AccessKey Secret + .setAccessKeySecret(accessKeySecret); + // 访问的域名 + config.endpoint = endpoint; + return new Client(config); + } + + /** + * 发送单条短信 + * @param tel + * @param sendDataMap + */ + public static String sendSms(String tel , Map sendDataMap){ + SendSmsRequest sendSmsRequest = new SendSmsRequest() + .setPhoneNumbers(tel) + .setSignName(signName) + .setTemplateCode(templateCode) + .setTemplateParam(JSONObject.toJSONString(sendDataMap)); + SendSmsResponse sendSmsResponse = null; + try { + log.info("发送短信验证码:消息内容是:【{}】", JSONObject.toJSONString(sendDataMap)); + sendSmsResponse = client.sendSms(sendSmsRequest); + } catch (Exception e) { + log.error("短信发送异常,手机号:【{}】,短信内容:【{}】,异常信息:【{}】", tel, sendDataMap, e); + } + return JSONObject.toJSONString(sendSmsResponse.getBody()); + } + +} diff --git a/community-security-gateway/pom.xml b/community-security-gateway/pom.xml new file mode 100644 index 0000000..bfb0c01 --- /dev/null +++ b/community-security-gateway/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + com.zyh + community-security + 1.0-SNAPSHOT + + + community-security-gateway + + + 17 + 17 + UTF-8 + + + + + + + com.zyh + community-security-common + + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + com.alibaba.cloud + spring-cloud-alibaba-sentinel-gateway + + + + com.alibaba.csp + sentinel-spring-cloud-gateway-adapter + + + + + diff --git a/community-security-gateway/src/main/java/com/zyh/gateway/GateWayApplication.java b/community-security-gateway/src/main/java/com/zyh/gateway/GateWayApplication.java new file mode 100644 index 0000000..5954e1e --- /dev/null +++ b/community-security-gateway/src/main/java/com/zyh/gateway/GateWayApplication.java @@ -0,0 +1,12 @@ +package com.zyh.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); + System.out.println("GateWay启动成功"); + } +} diff --git a/community-security-gateway/src/main/java/com/zyh/gateway/config/IgnoreWhiteConfig.java b/community-security-gateway/src/main/java/com/zyh/gateway/config/IgnoreWhiteConfig.java new file mode 100644 index 0000000..7785cdb --- /dev/null +++ b/community-security-gateway/src/main/java/com/zyh/gateway/config/IgnoreWhiteConfig.java @@ -0,0 +1,32 @@ +package com.zyh.gateway.config; + +import com.alibaba.fastjson.JSONObject; +import lombok.Data; +import lombok.extern.log4j.Log4j2; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +/** + * @description: 放行白名单配置 + * @author DongZl + */ +@Configuration +@RefreshScope +@ConfigurationProperties(prefix = "ignore") +@Data +@Log4j2 +public class IgnoreWhiteConfig { + /** + * 放行白名单配置,网关不校验此处的白名单 + */ + private List whites = new ArrayList<>(); + + public void setWhites(List whites) { + log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites)); + this.whites = whites; + } +} diff --git a/community-security-gateway/src/main/java/com/zyh/gateway/filters/AuthFilters.java b/community-security-gateway/src/main/java/com/zyh/gateway/filters/AuthFilters.java new file mode 100644 index 0000000..d549571 --- /dev/null +++ b/community-security-gateway/src/main/java/com/zyh/gateway/filters/AuthFilters.java @@ -0,0 +1,66 @@ + package com.zyh.gateway.filters; + +import com.zyh.common.constants.JwtConstants; +import com.zyh.common.constants.TokenConstants; +import com.zyh.common.utils.JwtUtils; +import com.zyh.common.utils.StringUtils; +import com.zyh.gateway.config.IgnoreWhiteConfig; +import com.zyh.gateway.utils.GatewayUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.gateway.filter.GatewayFilter; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.http.HttpHeaders; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +import java.util.List; + +@Configuration +public class AuthFilters implements GatewayFilter, Ordered { + + @Autowired + private IgnoreWhiteConfig ignoreWhiteConfig; + + @Autowired + private StringRedisTemplate stringRedisTemplate; + + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + //白名单放行 + List whites = ignoreWhiteConfig.getWhites(); + ServerHttpRequest request = exchange.getRequest(); + String path = request.getURI().getPath(); + if (StringUtils.matches(path,whites)){ + return chain.filter(exchange); + } + //判断token是否为空 + HttpHeaders headers = request.getHeaders(); + String token = headers.getFirst(TokenConstants.TOKEN); + if (StringUtils.isEmpty(token)){ + return GatewayUtils.errorResponse(exchange,"token为空111"); + } + + //token合法性 + try { + JwtUtils.parseToken(token); + } catch (Exception e) { + return GatewayUtils.errorResponse(exchange,"token不合法"); + } + //token时效性 + String userKey = JwtUtils.getUserKey(token); + Boolean hasKey = stringRedisTemplate.hasKey(JwtConstants.USER_KEY + userKey); + if (!hasKey){ + return GatewayUtils.errorResponse(exchange,"token失效"); + } + return chain.filter(exchange); + } + + @Override + public int getOrder() { + return 0; + } +} diff --git a/community-security-gateway/src/main/java/com/zyh/gateway/utils/GatewayUtils.java b/community-security-gateway/src/main/java/com/zyh/gateway/utils/GatewayUtils.java new file mode 100644 index 0000000..de7a063 --- /dev/null +++ b/community-security-gateway/src/main/java/com/zyh/gateway/utils/GatewayUtils.java @@ -0,0 +1,98 @@ +package com.zyh.gateway.utils; + +import com.alibaba.fastjson.JSONObject; +import com.zyh.common.result.Result; +import com.zyh.common.utils.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; + +/** + * @author DongZl + * @description: 网关处理工具类 + */ +@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, HttpStatus httpStatus) { + ServerHttpResponse response = exchange.getResponse(); + //设置HTTP响应头状态 + response.setStatusCode(httpStatus); + //设置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)); + } + + /** + * 错误结果响应 + * @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/community-security-gateway/src/main/resources/bootstrap.yml b/community-security-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..c8dc69b --- /dev/null +++ b/community-security-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,31 @@ +# Tomcat +server: + port: 18080 +# Spring +spring: + application: + # 应用名称 + name: community-security-gateway + profiles: + # 环境配置 + active: dev + main: + # 允许使用循环引用 + allow-circular-references: true + # 允许定义相同的bean对象 去覆盖原有的 + allow-bean-definition-overriding: true + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.221.193.62:8848 + namespace: e3afd3fd-73b0-4ac1-9f9e-aad1a8aa9f46 + config: + # 配置中心地址 + server-addr: 124.221.193.62:8848 + namespace: e3afd3fd-73b0-4ac1-9f9e-aad1a8aa9f46 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/community-security-modules/community-security-modules-shop/pom.xml b/community-security-modules/community-security-modules-shop/pom.xml new file mode 100644 index 0000000..23dfb51 --- /dev/null +++ b/community-security-modules/community-security-modules-shop/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + com.zyh + community-security-modules + 1.0-SNAPSHOT + + + community-security-modules-shop + + + 17 + 17 + UTF-8 + + + + + + + + com.zyh + community-security-common + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + mysql + mysql-connector-java + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + diff --git a/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/ShopApplication.java b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/ShopApplication.java new file mode 100644 index 0000000..454f2e6 --- /dev/null +++ b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/ShopApplication.java @@ -0,0 +1,16 @@ +package com.zyh.system; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; + +@SpringBootApplication +public class ShopApplication { + public static void main(String[] args) { + + SpringApplication.run(ShopApplication.class, args); + System.out.println("Shop启动成功"); + } +} + diff --git a/community-security-modules/community-security-modules-shop/src/main/resources/bootstrap.yml b/community-security-modules/community-security-modules-shop/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..a9e2748 --- /dev/null +++ b/community-security-modules/community-security-modules-shop/src/main/resources/bootstrap.yml @@ -0,0 +1,32 @@ +# Tomcat +server: + port: 9003 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: community-security-modules-shop + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.221.193.62:8848 + namespace: e3afd3fd-73b0-4ac1-9f9e-aad1a8aa9f46 + config: + # 配置中心地址 + server-addr: 124.221.193.62:8848 + namespace: e3afd3fd-73b0-4ac1-9f9e-aad1a8aa9f46 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + diff --git a/community-security-modules/community-security-modules-system/pom.xml b/community-security-modules/community-security-modules-system/pom.xml new file mode 100644 index 0000000..40b5337 --- /dev/null +++ b/community-security-modules/community-security-modules-system/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + com.zyh + community-security-modules + 1.0-SNAPSHOT + + + community-security-modules-system + + + 17 + 17 + UTF-8 + + + + + + + + com.zyh + community-security-common + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + mysql + mysql-connector-java + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + diff --git a/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/SystemApplication.java b/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/SystemApplication.java new file mode 100644 index 0000000..bc7062a --- /dev/null +++ b/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/SystemApplication.java @@ -0,0 +1,13 @@ +package com.zyh.system; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SystemApplication { + public static void main(String[] args) { + + SpringApplication.run(SystemApplication.class, args); + System.out.println("System启动成功"); + } +} diff --git a/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/controller/LoginController.java b/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/controller/LoginController.java new file mode 100644 index 0000000..65fe6b3 --- /dev/null +++ b/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/controller/LoginController.java @@ -0,0 +1,27 @@ +package com.zyh.system.controller; + +import com.zyh.common.domain.User; +import com.zyh.common.result.Result; +import com.zyh.system.service.LoginService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/user") +public class LoginController { + @Autowired + LoginService loginService; + + + + @PostMapping("findPhone/{phone}") + public Result findPhone(@PathVariable String phone) { + User user = loginService.findPhone(phone); + Result result = Result.success(user); + return result; + } + +} diff --git a/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/mapper/LoginMapper.java b/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/mapper/LoginMapper.java new file mode 100644 index 0000000..795801f --- /dev/null +++ b/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/mapper/LoginMapper.java @@ -0,0 +1,10 @@ +package com.zyh.system.mapper; + +import com.zyh.common.domain.User; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +@Mapper +public interface LoginMapper { + User findPhone(@Param("phone") String phone); +} diff --git a/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/service/LoginService.java b/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/service/LoginService.java new file mode 100644 index 0000000..cdc65ef --- /dev/null +++ b/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/service/LoginService.java @@ -0,0 +1,9 @@ +package com.zyh.system.service; + + +import com.zyh.common.domain.User; + +public interface LoginService { + + User findPhone(String phone); +} diff --git a/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/service/impl/LoginServicempl.java b/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/service/impl/LoginServicempl.java new file mode 100644 index 0000000..3af786a --- /dev/null +++ b/community-security-modules/community-security-modules-system/src/main/java/com/zyh/system/service/impl/LoginServicempl.java @@ -0,0 +1,17 @@ +package com.zyh.system.service.impl; + +import com.zyh.common.domain.User; +import com.zyh.system.mapper.LoginMapper; +import com.zyh.system.service.LoginService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class LoginServicempl implements LoginService { + @Autowired + LoginMapper loginMapper; + @Override + public User findPhone(String phone) { + return loginMapper.findPhone(phone); + } +} diff --git a/community-security-modules/community-security-modules-system/src/main/resources/bootstrap.yml b/community-security-modules/community-security-modules-system/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..d385c93 --- /dev/null +++ b/community-security-modules/community-security-modules-system/src/main/resources/bootstrap.yml @@ -0,0 +1,32 @@ +# Tomcat +server: + port: 9004 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: community-security-modules-system + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.221.193.62:8848 + namespace: e3afd3fd-73b0-4ac1-9f9e-aad1a8aa9f46 + config: + # 配置中心地址 + server-addr: 124.221.193.62:8848 + namespace: e3afd3fd-73b0-4ac1-9f9e-aad1a8aa9f46 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + diff --git a/community-security-modules/community-security-modules-system/src/main/resources/mapper/SysUserMapper.xml b/community-security-modules/community-security-modules-system/src/main/resources/mapper/SysUserMapper.xml new file mode 100644 index 0000000..9f676b8 --- /dev/null +++ b/community-security-modules/community-security-modules-system/src/main/resources/mapper/SysUserMapper.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/community-security-modules/pom.xml b/community-security-modules/pom.xml new file mode 100644 index 0000000..33e394e --- /dev/null +++ b/community-security-modules/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + com.zyh + community-security + 1.0-SNAPSHOT + + + community-security-modules + pom + + community-security-modules-shop + community-security-modules-system + + + + 17 + 17 + UTF-8 + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e1a256e --- /dev/null +++ b/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + com.zyh + community-security + 1.0-SNAPSHOT + pom + + community-security-auth + community-security-common + community-security-gateway + community-security-modules + + + + 17 + 17 + UTF-8 + + + + + spring-boot-starter-parent + org.springframework.boot + 2.6.2 + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + 2021.0.0 + pom + import + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + 2021.1 + pom + import + + + + com.alibaba.nacos + nacos-client + 2.0.4 + + + + com.zyh + community-security-common + 1.0-SNAPSHOT + + + + + +