第三修改
parent
989f61ffdd
commit
8178dc9cae
|
@ -15,4 +15,6 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||
public interface RemoteUserService {
|
||||
@PostMapping("/findName/{username}")
|
||||
public Result<User> findName(@PathVariable("username") String username);
|
||||
@PostMapping("/findTel/{tel}")
|
||||
public Result<User> findTel(@PathVariable String tel);
|
||||
}
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
package com.bwie.controller;
|
||||
|
||||
import com.bwie.common.domain.request.TelLogin;
|
||||
import com.bwie.common.domain.request.UserRequest;
|
||||
import com.bwie.common.result.Result;
|
||||
import com.bwie.service.impl.AuthService;
|
||||
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;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @author gxb
|
||||
|
@ -18,10 +17,37 @@ public class AuthController {
|
|||
@Autowired
|
||||
private AuthService authService;
|
||||
/**
|
||||
* 登录
|
||||
* 用户名 + 密码 = 登录
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public Result login(@RequestBody UserRequest userRequest){
|
||||
return authService.login(userRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
* @param tel
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/sendCode/{tel}")
|
||||
public Result sendCode(@PathVariable String tel){
|
||||
return authService.sendCode(tel);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号 + 验证码 = 登录
|
||||
* @param telLogin
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/TelLogin")
|
||||
public Result TelLogin(@RequestBody TelLogin telLogin){
|
||||
return authService.TelLogin(telLogin);
|
||||
}
|
||||
/**
|
||||
* 获取当前登录人
|
||||
*/
|
||||
@GetMapping("/info")
|
||||
public Result info(){
|
||||
return authService.info();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.bwie.service.impl;
|
||||
|
||||
import com.bwie.common.domain.request.TelLogin;
|
||||
import com.bwie.common.domain.request.UserRequest;
|
||||
import com.bwie.common.result.Result;
|
||||
|
||||
|
@ -11,4 +12,10 @@ import com.bwie.common.result.Result;
|
|||
public interface AuthService {
|
||||
Result login(UserRequest userRequest);
|
||||
|
||||
Result sendCode(String tel);
|
||||
|
||||
Result TelLogin(TelLogin telLogin);
|
||||
|
||||
Result info();
|
||||
|
||||
}
|
||||
|
|
|
@ -5,9 +5,12 @@ import com.bwie.common.constants.JwtConstants;
|
|||
import com.bwie.common.constants.TokenConstants;
|
||||
import com.bwie.common.domain.User;
|
||||
import com.bwie.common.domain.request.JwtRequest;
|
||||
import com.bwie.common.domain.request.TelLogin;
|
||||
import com.bwie.common.domain.request.UserRequest;
|
||||
import com.bwie.common.result.Result;
|
||||
import com.bwie.common.utils.JwtUtils;
|
||||
import com.bwie.common.utils.OptionalUtils;
|
||||
import com.bwie.common.utils.StringUtils;
|
||||
import com.bwie.user.RemoteUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
@ -16,6 +19,8 @@ import org.springframework.stereotype.Service;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.sql.Time;
|
||||
import java.util.HashMap;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
|
@ -31,9 +36,25 @@ public class AuthServicelmpl implements AuthService{
|
|||
private HttpServletRequest request;
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
@Autowired
|
||||
private OptionalUtils optionalUtils;
|
||||
|
||||
/**
|
||||
* 用户名 + 密码 = 登录
|
||||
* @param userRequest
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result login(UserRequest userRequest) {
|
||||
//判断姓名是否为空
|
||||
Optional<String> optional = Optional.ofNullable(userRequest.getUsername());
|
||||
|
||||
//判断
|
||||
if (!optional.isPresent()){
|
||||
//为null定义一个默认值
|
||||
String defaultName = optional.orElse("张三");
|
||||
userRequest.setUsername(defaultName);
|
||||
}
|
||||
User user = remoteUserService.findName(userRequest.getUsername()).getData();
|
||||
if (null==user){
|
||||
return Result.error("用户名不存在!!!!!!!");
|
||||
|
@ -44,6 +65,76 @@ public class AuthServicelmpl implements AuthService{
|
|||
JwtRequest token = this.getToken(user);
|
||||
return Result.success(token,"登录成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
* @param tel
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result sendCode(String tel) {
|
||||
//判断手机号是否为空
|
||||
Optional<String> optional = Optional.ofNullable(tel);
|
||||
if (!optional.isPresent()){
|
||||
//默认值
|
||||
String defaultPhone = optional.orElse("18098734205");
|
||||
tel = defaultPhone;
|
||||
}
|
||||
//格式验证
|
||||
if (!tel.matches("^1[3-9]\\d{9}$")){
|
||||
return Result.error("手机号格式有误");
|
||||
}
|
||||
User user = remoteUserService.findTel(tel).getData();
|
||||
if (null==user){
|
||||
return Result.error("手机号不存在");
|
||||
}
|
||||
String code = "";
|
||||
for (int i = 0; i < 4; i++) {
|
||||
code += new Random().nextInt(10);
|
||||
}
|
||||
redisTemplate.opsForValue().set(tel,code,10,TimeUnit.MINUTES);
|
||||
return Result.success("验证码:"+code+",十分钟内有效期,谨防假冒,未泄露与他人");
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号 + 验证码 = 登录
|
||||
* @param telLogin
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result TelLogin(TelLogin telLogin) {
|
||||
//判断手机号是否为空
|
||||
Optional<String> optional = Optional.ofNullable(telLogin.getTel());
|
||||
if (!optional.isPresent()){
|
||||
//默认值
|
||||
String defaultPhone = optional.orElse("18098734205");
|
||||
telLogin.setTel(defaultPhone);
|
||||
}
|
||||
//格式验证
|
||||
if (!telLogin.getTel().matches("^1[3-9]\\d{9}$")){
|
||||
return Result.error("手机号格式有误");
|
||||
}
|
||||
User user = remoteUserService.findTel(telLogin.getTel()).getData();
|
||||
if (null==user){
|
||||
return Result.error("手机号不存在");
|
||||
}
|
||||
//验证码验证码
|
||||
if (!redisTemplate.hasKey(telLogin.getTel())){
|
||||
return Result.error("验证码过期了");
|
||||
}
|
||||
String getCode = redisTemplate.opsForValue().get(telLogin.getTel());
|
||||
if (!getCode.equals(telLogin.getCode())){
|
||||
return Result.error("验证码错误");
|
||||
}
|
||||
JwtRequest token = this.getToken(user);
|
||||
return Result.success(token,"登录成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result info() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成token
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package com.bwie.common.domain.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author gxb
|
||||
* @description TODO
|
||||
* @date 2024-01-08 8:51
|
||||
*/
|
||||
@Data
|
||||
public class TelLogin {
|
||||
private String tel;
|
||||
private String code;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.bwie.common.utils;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author gxb
|
||||
* @description TODO
|
||||
* @date 2024-01-07 9:29
|
||||
*/
|
||||
@Component
|
||||
public class OptionalUtils {
|
||||
// 私有构造函数,避免被实例化
|
||||
private OptionalUtils() {}
|
||||
|
||||
/**
|
||||
* 获取 Optional 对象中的值,如果为空,则返回默认值
|
||||
*
|
||||
* @param optional Optional 对象
|
||||
* @param defaultValue 默认值
|
||||
* @param <T> 值的类型
|
||||
* @return Optional 对象中的值或默认值
|
||||
*/
|
||||
public static <T> T getOrDefault(Optional<T> optional, T defaultValue) {
|
||||
return optional.orElse(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果 Optional 对象中的值存在,则执行给定的操作
|
||||
*
|
||||
* @param optional Optional 对象
|
||||
* @param action 要执行的操作
|
||||
* @param <T> 值的类型
|
||||
*/
|
||||
public static <T> void ifPresent(Optional<T> optional, Consumer<? super T> action) {
|
||||
optional.ifPresent(action);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Optional 对象中的值转换为另一种类型
|
||||
*
|
||||
* @param optional Optional 对象
|
||||
* @param mapper 转换函数
|
||||
* @param <T> 原始值的类型
|
||||
* @param <U> 转换后的值的类型
|
||||
* @return 转换后的 Optional 对象
|
||||
*/
|
||||
public static <T, U> Optional<U> map(Optional<T> optional, Function<? super T, ? extends U> mapper) {
|
||||
return optional.map(mapper);
|
||||
}
|
||||
|
||||
}
|
|
@ -17,8 +17,24 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
public class UserController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 用户名查询
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/findName/{username}")
|
||||
public Result<User> findName(@PathVariable("username") String username){
|
||||
return userService.findName(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号查询
|
||||
* @param tel
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/findTel/{tel}")
|
||||
public Result<User> findTel(@PathVariable String tel){
|
||||
return userService.findTel(tel);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,5 +12,17 @@ import org.springframework.stereotype.Component;
|
|||
@Component
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
/**
|
||||
* 用户名查询
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
User findName(String username);
|
||||
|
||||
/**
|
||||
* 手机号查询
|
||||
* @param tel
|
||||
* @return
|
||||
*/
|
||||
User findTel(String tel);
|
||||
}
|
||||
|
|
|
@ -11,4 +11,5 @@ import com.bwie.common.result.Result;
|
|||
public interface UserService {
|
||||
Result<User> findName(String username);
|
||||
|
||||
Result<User> findTel(String tel);
|
||||
}
|
||||
|
|
|
@ -15,9 +15,26 @@ import org.springframework.stereotype.Service;
|
|||
public class UserServicelmpl implements UserService{
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
/**
|
||||
* 用户名查询
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<User> findName(String username) {
|
||||
User user = userMapper.findName(username);
|
||||
return Result.success(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号查询
|
||||
* @param tel
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<User> findTel(String tel) {
|
||||
User user = userMapper.findTel(tel);
|
||||
return Result.success(user);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,16 @@
|
|||
<?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">
|
||||
|
||||
<sql id="user">
|
||||
user_id,username,password,tel,role
|
||||
</sql>
|
||||
<select id="findName" resultType="com.bwie.common.domain.User">
|
||||
select *
|
||||
select <include refid="user"></include>
|
||||
from user
|
||||
where username = #{username};
|
||||
</select>
|
||||
<select id="findTel" resultType="com.bwie.common.domain.User">
|
||||
select <include refid="user"></include>
|
||||
from user where tel = #{tel}
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
Loading…
Reference in New Issue