From 969ac09b43e17b27c6aaf69c8d5f6760380a32ac Mon Sep 17 00:00:00 2001 From: wxy Date: Mon, 15 Apr 2024 11:27:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/bwie/auth/AuthApplication.java | 19 ++++ .../bwie/auth/controller/AuthController.java | 62 +++++++++++++ .../com/bwie/auth/feign/UserFeignService.java | 18 ++++ .../com/bwie/auth/service/AuthService.java | 20 ++++ .../auth/service/impl/AuthServiceImpl.java | 93 +++++++++++++++++++ .../com/bwie/common/domain/pojo/User.java | 2 - .../common/domain/request/UserRequest.java | 15 +++ .../common/domain/response/JwtResponse.java | 15 +++ .../com/bwie/gateway/GateWayApplication.java | 15 +++ .../com/bwie/gateway/filters/AuthFilters.java | 25 +++++ .../java/com/bwie/user/UserApplication.java | 17 ++++ .../bwie/user/controller/UserController.java | 39 ++++++++ .../java/com/bwie/user/mapper/UserMapper.java | 13 +++ .../com/bwie/user/service/UserService.java | 12 +++ .../user/service/impl/UserServiceImpl.java | 23 +++++ .../src/main/resources/mapper/UserMapper.xml | 5 +- 16 files changed, 390 insertions(+), 3 deletions(-) create mode 100644 boot-auth/src/main/java/com/bwie/auth/AuthApplication.java create mode 100644 boot-auth/src/main/java/com/bwie/auth/controller/AuthController.java create mode 100644 boot-auth/src/main/java/com/bwie/auth/feign/UserFeignService.java create mode 100644 boot-auth/src/main/java/com/bwie/auth/service/AuthService.java create mode 100644 boot-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java create mode 100644 boot-common/src/main/java/com/bwie/common/domain/request/UserRequest.java create mode 100644 boot-common/src/main/java/com/bwie/common/domain/response/JwtResponse.java create mode 100644 boot-gateway/src/main/java/com/bwie/gateway/GateWayApplication.java create mode 100644 boot-gateway/src/main/java/com/bwie/gateway/filters/AuthFilters.java create mode 100644 moudles/boot-user/src/main/java/com/bwie/user/UserApplication.java create mode 100644 moudles/boot-user/src/main/java/com/bwie/user/controller/UserController.java create mode 100644 moudles/boot-user/src/main/java/com/bwie/user/mapper/UserMapper.java create mode 100644 moudles/boot-user/src/main/java/com/bwie/user/service/UserService.java create mode 100644 moudles/boot-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java diff --git a/boot-auth/src/main/java/com/bwie/auth/AuthApplication.java b/boot-auth/src/main/java/com/bwie/auth/AuthApplication.java new file mode 100644 index 0000000..7bb2f0d --- /dev/null +++ b/boot-auth/src/main/java/com/bwie/auth/AuthApplication.java @@ -0,0 +1,19 @@ +package com.bwie.auth; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午11:20 + */ +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients +public class AuthApplication { + public static void main(String[] args) { + SpringApplication.run(AuthApplication.class, args); + } +} diff --git a/boot-auth/src/main/java/com/bwie/auth/controller/AuthController.java b/boot-auth/src/main/java/com/bwie/auth/controller/AuthController.java new file mode 100644 index 0000000..2a16bba --- /dev/null +++ b/boot-auth/src/main/java/com/bwie/auth/controller/AuthController.java @@ -0,0 +1,62 @@ +package com.bwie.auth.controller; + +import com.alibaba.fastjson.JSON; +import com.bwie.auth.service.AuthService; +import com.bwie.common.domain.pojo.User; +import com.bwie.common.domain.request.UserRequest; +import com.bwie.common.domain.response.JwtResponse; +import com.bwie.common.result.Result; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:50 + */ +@RestController +@RequestMapping("/auth") +@Log4j2 +public class AuthController { + + + @Autowired + private HttpServletRequest request; + + @Autowired + private AuthService authService; + + + @PostMapping("/login") + public Resultlogin(@RequestBody UserRequest userRequest){ + log.info("功能描述: xx,请求URI:{},请求方式: {},请求参数: {}", + request.getRequestURI(),request.getMethod(),userRequest); + Resultresult=authService.login(userRequest); + log.info("功能描述: xx,请求URI:{},请求方式: {},响应结果: {}", + request.getRequestURI(),request.getMethod(), JSON.toJSONString(result)); + return result; + } + + @PostMapping("/sendCode/{phone}") + public Result sendCode(@PathVariable("phone")String phone){ + log.info("功能描述: xx,请求URI:{},请求方式: {},请求参数: {}", + request.getRequestURI(),request.getMethod(),phone); + Result result=authService.sendCode(phone); + log.info("功能描述: xx,请求URI:{},请求方式: {},响应结果: {}", + request.getRequestURI(),request.getMethod(), JSON.toJSONString(result)); + return result; + } + + @GetMapping("/info") + public Resultinfo(){ + log.info("功能描述: xx,请求URI:{},请求方式: {}", + request.getRequestURI(),request.getMethod()); + User user=authService.info(); + Result result=Result.success(user); + log.info("功能描述: xx,请求URI:{},请求方式: {},响应结果: {}", + request.getRequestURI(),request.getMethod(), JSON.toJSONString(result)); + return result; + } +} diff --git a/boot-auth/src/main/java/com/bwie/auth/feign/UserFeignService.java b/boot-auth/src/main/java/com/bwie/auth/feign/UserFeignService.java new file mode 100644 index 0000000..89d9991 --- /dev/null +++ b/boot-auth/src/main/java/com/bwie/auth/feign/UserFeignService.java @@ -0,0 +1,18 @@ +package com.bwie.auth.feign; + +import com.bwie.common.domain.pojo.User; +import com.bwie.common.result.Result; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:49 + */ +@FeignClient(name = "boot-user") +public interface UserFeignService { + + @GetMapping("/user/findByPhone/{phone}") + public Result findByPhone(@PathVariable("phone") String phone); +} diff --git a/boot-auth/src/main/java/com/bwie/auth/service/AuthService.java b/boot-auth/src/main/java/com/bwie/auth/service/AuthService.java new file mode 100644 index 0000000..ffb0750 --- /dev/null +++ b/boot-auth/src/main/java/com/bwie/auth/service/AuthService.java @@ -0,0 +1,20 @@ +package com.bwie.auth.service; + +import com.bwie.common.domain.pojo.User; +import com.bwie.common.domain.request.UserRequest; +import com.bwie.common.domain.response.JwtResponse; +import com.bwie.common.result.Result; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:50 + */ +public interface AuthService { + + Result login(UserRequest userRequest); + + Result sendCode(String phone); + + User info(); + +} diff --git a/boot-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java b/boot-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java new file mode 100644 index 0000000..9008cce --- /dev/null +++ b/boot-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java @@ -0,0 +1,93 @@ +package com.bwie.auth.service.impl; + +import cn.hutool.core.util.RandomUtil; +import com.alibaba.fastjson.JSON; +import com.bwie.auth.feign.UserFeignService; +import com.bwie.auth.service.AuthService; +import com.bwie.common.constants.JwtConstants; +import com.bwie.common.constants.TokenConstants; +import com.bwie.common.domain.pojo.User; +import com.bwie.common.domain.request.UserRequest; +import com.bwie.common.domain.response.JwtResponse; +import com.bwie.common.result.Result; +import com.bwie.common.utils.JwtUtils; +import com.bwie.common.utils.StringUtils; +import com.bwie.common.utils.TelSmsUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestBody; + +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:51 + */ +@Service +public class AuthServiceImpl implements AuthService { + + @Autowired + private HttpServletRequest request; + + @Autowired + private RedisTemplateredisTemplate; + + @Autowired + private UserFeignService userFeignService; + + @Override + public Result login(@RequestBody UserRequest userRequest) { + if(StringUtils.isEmpty(userRequest.getPhone())||StringUtils.isEmpty(userRequest.getCode())){ + return Result.error("手机号或验证码不能为空!"); + } + Result byPhone = userFeignService.findByPhone(userRequest.getPhone()); + User userLogin = byPhone.getData(); + if(userLogin==null){ + return Result.error("请注册!"); + } + String code = redisTemplate.opsForValue().get(userRequest.getPhone()); + if(StringUtils.isBlank(code)){ + return Result.error("验证码过期!"); + } + if(!code.equals(userRequest.getCode())){ + return Result.error("验证码错误!"); + } + Map jwtMap = new HashMap<>(); + String userKey = UUID.randomUUID().toString().replaceAll("-", ""); + jwtMap.put(JwtConstants.USER_KEY,userKey); + String token = JwtUtils.createToken(jwtMap); + redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey, JSON.toJSONString(userLogin),2, TimeUnit.MINUTES); + JwtResponse jwtResponse = new JwtResponse(); + jwtResponse.setToken(token); + jwtResponse.setExpireTime("20MIN"); + return Result.success(jwtResponse); + } + @Override + public Result sendCode(String phone) { + Result byPhone = userFeignService.findByPhone(phone); + User loginUser = byPhone.getData(); + if(loginUser==null){ + return Result.error("请你注册!"); + } + String code = RandomUtil.randomNumbers(4); + redisTemplate.opsForValue().set(phone,code,2,TimeUnit.MINUTES); + TelSmsUtils.sendSms(phone,new HashMap(){{ + put("code",code); + }}); + return Result.success("发送成功"); + } + + @Override + public User info() { + String token = request.getHeader(TokenConstants.TOKEN); + String userKey = JwtUtils.getUserKey(token); + String userJson = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey); + return JSON.parseObject(userJson,User.class); + } +} diff --git a/boot-common/src/main/java/com/bwie/common/domain/pojo/User.java b/boot-common/src/main/java/com/bwie/common/domain/pojo/User.java index d6c9a7d..8bc9e37 100644 --- a/boot-common/src/main/java/com/bwie/common/domain/pojo/User.java +++ b/boot-common/src/main/java/com/bwie/common/domain/pojo/User.java @@ -15,9 +15,7 @@ public class User { private Integer userId; private String userName; - private String email; private String address; - private String phone; } diff --git a/boot-common/src/main/java/com/bwie/common/domain/request/UserRequest.java b/boot-common/src/main/java/com/bwie/common/domain/request/UserRequest.java new file mode 100644 index 0000000..c024722 --- /dev/null +++ b/boot-common/src/main/java/com/bwie/common/domain/request/UserRequest.java @@ -0,0 +1,15 @@ +package com.bwie.common.domain.request; + +import lombok.Data; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:31 + */ +@Data +public class UserRequest { + + private String phone; + + private String code; +} diff --git a/boot-common/src/main/java/com/bwie/common/domain/response/JwtResponse.java b/boot-common/src/main/java/com/bwie/common/domain/response/JwtResponse.java new file mode 100644 index 0000000..68cbb4c --- /dev/null +++ b/boot-common/src/main/java/com/bwie/common/domain/response/JwtResponse.java @@ -0,0 +1,15 @@ +package com.bwie.common.domain.response; + +import lombok.Data; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:31 + */ +@Data +public class JwtResponse { + + private String token; + + private String expireTime; +} diff --git a/boot-gateway/src/main/java/com/bwie/gateway/GateWayApplication.java b/boot-gateway/src/main/java/com/bwie/gateway/GateWayApplication.java new file mode 100644 index 0000000..ecac70e --- /dev/null +++ b/boot-gateway/src/main/java/com/bwie/gateway/GateWayApplication.java @@ -0,0 +1,15 @@ +package com.bwie.gateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:49 + */ +@SpringBootApplication +public class GateWayApplication { + public static void main(String[] args) { + SpringApplication.run(GateWayApplication.class, args); + } +} diff --git a/boot-gateway/src/main/java/com/bwie/gateway/filters/AuthFilters.java b/boot-gateway/src/main/java/com/bwie/gateway/filters/AuthFilters.java new file mode 100644 index 0000000..12d083d --- /dev/null +++ b/boot-gateway/src/main/java/com/bwie/gateway/filters/AuthFilters.java @@ -0,0 +1,25 @@ +package com.bwie.gateway.filters; + +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:48 + */ +@Component +public class AuthFilters implements GlobalFilter, Ordered { + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + return chain.filter(exchange); + } + + @Override + public int getOrder() { + return 0; + } +} diff --git a/moudles/boot-user/src/main/java/com/bwie/user/UserApplication.java b/moudles/boot-user/src/main/java/com/bwie/user/UserApplication.java new file mode 100644 index 0000000..1bdb850 --- /dev/null +++ b/moudles/boot-user/src/main/java/com/bwie/user/UserApplication.java @@ -0,0 +1,17 @@ +package com.bwie.user; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:36 + */ +@SpringBootApplication +@EnableDiscoveryClient +public class UserApplication { + public static void main(String[] args) { + SpringApplication.run(UserApplication.class, args); + } +} diff --git a/moudles/boot-user/src/main/java/com/bwie/user/controller/UserController.java b/moudles/boot-user/src/main/java/com/bwie/user/controller/UserController.java new file mode 100644 index 0000000..53873e9 --- /dev/null +++ b/moudles/boot-user/src/main/java/com/bwie/user/controller/UserController.java @@ -0,0 +1,39 @@ +package com.bwie.user.controller; + +import com.alibaba.fastjson.JSON; +import com.bwie.common.domain.pojo.User; +import com.bwie.common.result.Result; +import com.bwie.user.service.UserService; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:32 + */ +@RestController +@RequestMapping("/user") +@Log4j2 +public class UserController { + + @Autowired + private UserService userService; + + + @Autowired + private HttpServletRequest request; + + @GetMapping("/findByPhone/{phone}") + public ResultfindByPhone(@PathVariable("phone")String phone){ + log.info("功能描述: xx,请求URI:{},请求方式: {},请求参数: {}", + request.getRequestURI(),request.getMethod(),phone); + User user=userService.findByPhone(phone); + Result result=Result.success(user); + log.info("功能描述: xx,请求URI:{},请求方式: {},响应结果: {}", + request.getRequestURI(),request.getMethod(), JSON.toJSONString(result)); + return result; + } +} diff --git a/moudles/boot-user/src/main/java/com/bwie/user/mapper/UserMapper.java b/moudles/boot-user/src/main/java/com/bwie/user/mapper/UserMapper.java new file mode 100644 index 0000000..33cc6ea --- /dev/null +++ b/moudles/boot-user/src/main/java/com/bwie/user/mapper/UserMapper.java @@ -0,0 +1,13 @@ +package com.bwie.user.mapper; + +import com.bwie.common.domain.pojo.User; +import org.apache.ibatis.annotations.Mapper; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:32 + */ +@Mapper +public interface UserMapper { + User findByPhone(String phone); +} diff --git a/moudles/boot-user/src/main/java/com/bwie/user/service/UserService.java b/moudles/boot-user/src/main/java/com/bwie/user/service/UserService.java new file mode 100644 index 0000000..ebd8dfb --- /dev/null +++ b/moudles/boot-user/src/main/java/com/bwie/user/service/UserService.java @@ -0,0 +1,12 @@ +package com.bwie.user.service; + +import com.bwie.common.domain.pojo.User; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:32 + */ +public interface UserService { + + User findByPhone(String phone); +} diff --git a/moudles/boot-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java b/moudles/boot-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..a6bc166 --- /dev/null +++ b/moudles/boot-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java @@ -0,0 +1,23 @@ +package com.bwie.user.service.impl; + +import com.bwie.common.domain.pojo.User; +import com.bwie.user.mapper.UserMapper; +import com.bwie.user.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @Author: wangxinyuan + * @Date: 2024/4/15 上午10:32 + */ +@Service +public class UserServiceImpl implements UserService { + + @Autowired + private UserMapper userMapper; + + @Override + public User findByPhone(String phone) { + return userMapper.findByPhone(phone); + } +} diff --git a/moudles/boot-user/src/main/resources/mapper/UserMapper.xml b/moudles/boot-user/src/main/resources/mapper/UserMapper.xml index a31c922..b208f8c 100644 --- a/moudles/boot-user/src/main/resources/mapper/UserMapper.xml +++ b/moudles/boot-user/src/main/resources/mapper/UserMapper.xml @@ -1,5 +1,8 @@ - + +