session短信登录机制

master
barney 2 years ago
parent c3f5041264
commit 41852cade2
  1. 18
      hm-dianping/src/main/java/com/hmdp/config/MvcConfig.java
  2. 18
      hm-dianping/src/main/java/com/hmdp/controller/UserController.java
  3. 7
      hm-dianping/src/main/java/com/hmdp/service/IUserService.java
  4. 70
      hm-dianping/src/main/java/com/hmdp/service/impl/UserServiceImpl.java
  5. 34
      hm-dianping/src/main/java/com/hmdp/utils/LoginInterceptor.java

@ -0,0 +1,18 @@
package com.hmdp.config;
import com.hmdp.utils.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 添加登录拦截器
registry.addInterceptor(new LoginInterceptor()).excludePathPatterns(
"/user/login",
"/user/code"
);
}
}

@ -3,9 +3,12 @@ package com.hmdp.controller;
import com.hmdp.dto.LoginFormDTO; import com.hmdp.dto.LoginFormDTO;
import com.hmdp.dto.Result; import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.User;
import com.hmdp.entity.UserInfo; import com.hmdp.entity.UserInfo;
import com.hmdp.service.IUserInfoService; import com.hmdp.service.IUserInfoService;
import com.hmdp.service.IUserService; import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -34,10 +37,10 @@ public class UserController {
/** /**
* 发送手机验证码 * 发送手机验证码
*/ */
@PostMapping("code") @PostMapping("/code")
public Result sendCode(@RequestParam("phone") String phone, HttpSession session) { public Result sendCode(@RequestParam("phone") String phone, HttpSession session) {
// TODO 发送短信验证码并保存验证码 // 发送短信验证码并保存验证码
return Result.fail("功能未完成"); return userService.sendPhone(phone,session);
} }
/** /**
@ -46,8 +49,8 @@ public class UserController {
*/ */
@PostMapping("/login") @PostMapping("/login")
public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session){ public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session){
// TODO 实现登录功能 // 实现登录功能
return Result.fail("功能未完成"); return userService.login(loginForm,session);
} }
/** /**
@ -62,8 +65,9 @@ public class UserController {
@GetMapping("/me") @GetMapping("/me")
public Result me(){ public Result me(){
// TODO 获取当前登录的用户并返回 // 获取当前登录的用户并返回
return Result.fail("功能未完成"); UserDTO user = UserHolder.getUser();
return Result.ok(user);
} }
@GetMapping("/info/{id}") @GetMapping("/info/{id}")

@ -1,8 +1,12 @@
package com.hmdp.service; package com.hmdp.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.hmdp.dto.LoginFormDTO;
import com.hmdp.dto.Result;
import com.hmdp.entity.User; import com.hmdp.entity.User;
import javax.servlet.http.HttpSession;
/** /**
* <p> * <p>
* 服务类 * 服务类
@ -13,4 +17,7 @@ import com.hmdp.entity.User;
*/ */
public interface IUserService extends IService<User> { public interface IUserService extends IService<User> {
Result sendPhone(String phone, HttpSession session);
Result login(LoginFormDTO loginForm, HttpSession session);
} }

@ -1,11 +1,22 @@
package com.hmdp.service.impl; package com.hmdp.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.LoginFormDTO;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.User; import com.hmdp.entity.User;
import com.hmdp.mapper.UserMapper; import com.hmdp.mapper.UserMapper;
import com.hmdp.service.IUserService; import com.hmdp.service.IUserService;
import com.hmdp.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import static com.hmdp.utils.SystemConstants.USER_NICK_NAME_PREFIX;
/** /**
* <p> * <p>
* 服务实现类 * 服务实现类
@ -15,6 +26,65 @@ import org.springframework.stereotype.Service;
* @since 2021-12-22 * @since 2021-12-22
*/ */
@Service @Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Override
public Result sendPhone(String phone, HttpSession session) {
System.out.println("phone: " + phone);
// 1. 检验验证码
if (RegexUtils.isPhoneInvalid(phone)) {
// 2.不符合,返回错误信息
return Result.fail("手机号格式错误");
}
// 3.符合,生成验证码
String code = RandomUtil.randomNumbers(6);
// 4.保存验证码到session中
session.setAttribute("code",code);
// 5.发送验证码
log.debug("发送验证码成功,验证码: {}",code);
// 返回ok
return Result.ok();
}
@Override
public Result login(LoginFormDTO loginForm, HttpSession session) {
String phone = loginForm.getPhone();
// 1.检验手机号
if (RegexUtils.isPhoneInvalid(phone)) {
// 手机号不合法,直接返回错误
Result.fail("手机号格式错误");
}
// 2.校验验证码
// 从session中取出验证码
Object cacheCode = session.getAttribute("code");
String code = loginForm.getCode();
if (code == null || !cacheCode.toString().equals(code)) {
// 3.不一致或者验证码为空
return Result.fail("验证码错误");
}
// 4.一致,查询手机号对应的用户是否存在
User user = query().eq("phone",phone).one();
// 5.用户不存在,则创建新用户
if (user == null) {
// 6.不存在则根据手机号新建用户
user = createUserWithPhone(phone);
}
// 7.将用户信息存入session
session.setAttribute("user", BeanUtil.copyProperties(user, UserDTO.class));
return Result.ok();
}
private User createUserWithPhone(String phone) {
// 创建用户
User user = new User();
user.setPhone(phone);
user.setNickName(USER_NICK_NAME_PREFIX + RandomUtil.randomNumbers(10));
// 保存用户
save(user);
return user;
}
} }

@ -0,0 +1,34 @@
package com.hmdp.utils;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 1.获取session
HttpSession session = request.getSession();
// 2.获取session中的用户
Object user = session.getAttribute("user");
// 3.判断用户是否存在
if (user == null) {
// 4.不存在,将请求拦截
response.setStatus(401);
return false;
}
// 5.存在则保存用户信息到ThreadLocal
UserHolder.saveUser((UserDTO) user);
// 6.放行
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
UserHolder.removeUser();
}
}
Loading…
Cancel
Save