master
wxy 2024-04-15 11:27:32 +08:00
parent 0bd8c4ca79
commit 969ac09b43
16 changed files with 390 additions and 3 deletions

View File

@ -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);
}
}

View File

@ -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 Result<JwtResponse>login(@RequestBody UserRequest userRequest){
log.info("功能描述: xx,请求URI{},请求方式: {},请求参数: {}",
request.getRequestURI(),request.getMethod(),userRequest);
Result<JwtResponse>result=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 Result<User>info(){
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;
}
}

View File

@ -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<User> findByPhone(@PathVariable("phone") String phone);
}

View File

@ -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<JwtResponse> login(UserRequest userRequest);
Result sendCode(String phone);
User info();
}

View File

@ -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 RedisTemplate<String,String>redisTemplate;
@Autowired
private UserFeignService userFeignService;
@Override
public Result<JwtResponse> login(@RequestBody UserRequest userRequest) {
if(StringUtils.isEmpty(userRequest.getPhone())||StringUtils.isEmpty(userRequest.getCode())){
return Result.error("手机号或验证码不能为空!");
}
Result<User> 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<String, Object> 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<User> 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<String,String>(){{
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);
}
}

View File

@ -15,9 +15,7 @@ public class User {
private Integer userId;
private String userName;
private String email;
private String address;
private String phone;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}

View File

@ -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);
}
}

View File

@ -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 Result<User>findByPhone(@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;
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.bwie.user.mapper.UserMapper">
<select id="findByPhone" resultType="com.bwie.common.domain.pojo.User">
select * from user where phone = #{phone}
</select>
</mapper>