網站首頁 編程語言 正文
一、基于Session實現登錄
1.1 業務流程圖
二、發送短信驗證碼
2.1 發送短信請求方式及參數說明
這個地方為什么需要session?? 因為我們需要把驗證碼保存在session當中
/**
* 發送手機驗證碼
*/
@PostMapping("code")
public Result sendCode(@RequestParam("phone") String phone, HttpSession session) {
// TODO 發送短信驗證碼并保存驗證碼
// return Result.fail("功能未完成");
return userService.sendCode(phone,session);
}
2.2 業務層代碼模擬發送短信
@Override
public Result sendCode(String phone, HttpSession session) {
// 1.校驗手機號
if(RegexUtils.isPhoneInvalid(phone)){
// 說明:RegexUtils使我們封裝的一個類 isCodeInvalid是里面的靜態方法,在這個靜態方法里面又調用了另外一個靜態方法得以實現
// 2.如果不符合,返回錯誤信息
return Result.fail("手機號格式錯誤");
}
// 3.符合,生成驗證碼 6代表生成的驗證碼的長度 RandomUtil使用這個工具類生成
String code = RandomUtil.randomNumbers(6);
// 4.保存驗證碼到session key必須是一個字符串,value是一個對象
session.setAttribute("code",code);
// 5.發送驗證碼
// 實現起來比較麻煩 我們使用日志假裝發送
log.debug("發送短信驗證碼成功,驗證碼:"+code);
return Result.ok();
}
}
三、登錄功能 ?
3.1? 短信驗證的請求方式及路徑
/**
* 登錄功能
* @param loginForm 登錄參數,包含手機號、驗證碼;或者手機號、密碼
*/
@PostMapping("/login")
public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session){
// TODO 實現登錄功能
return userService.login(loginForm,session);
}
3.2? 業務層代碼實現用戶登錄
流程圖:
代碼:
/**
* 實現用戶登錄
* @param loginForm 登錄的參數
* @param session
* @return
*/
@Override
public Result login(LoginFormDTO loginForm, HttpSession session) {
// 1.校驗手機號
if(RegexUtils.isPhoneInvalid(loginForm.getPhone())){
// 說明:RegexUtils使我們封裝的一個類 isCodeInvalid是里面的靜態方法,在這個靜態方法里面又調用了另外一個靜態方法得以實現
// 1.2.如果不符合,返回錯誤信息
return Result.fail("手機號格式錯誤");
}
// 2.校驗驗證碼
// 2.1 得到code 這個值是真實的code
Object cacheCode = session.getAttribute("code");
// 2.2 獲取用戶輸入的code
String code = loginForm.getCode();
if(cacheCode ==null || !cacheCode.toString().equals(code)){
// 3.不一致,報錯
return Result.fail("驗證碼錯誤");
}
// 4.一致,根據手機號查詢用戶 .one()代表查詢一個 list()代表著查詢多個
User user =query().eq("phone",loginForm.getPhone()).one();
// 5.判斷用戶是否存在
if(user ==null){
// 6.不存在,創建新用戶并保存
user = createUserWithPhone(loginForm.getPhone());
}
// 7.保存用戶信息到session中
session.setAttribute("user",user);
return Result.ok();
}
private User createUserWithPhone(String phone) {
// 1.創建用戶
User user = new User();
user.setPhone(phone);
// USER_NICK_NAME_PREFIX其實就是 "user_",這樣寫更有逼格
user.setNickName(USER_NICK_NAME_PREFIX+RandomUtil.randomString(10));
// 保存用戶
save(user);
return user;
}
3.3 攔截器——登錄驗證功能
// HandlerInterceptor 這是一個攔截器
public class LoginInterceptor implements HandlerInterceptor {
// 前置攔截 在進入controller之前我們進行登錄校驗
@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); //返回401狀態碼
return false;
}
// 5.存在,保存用戶信息到ThreadLocal 保存在當前線程里面的
UserHolder.saveUser((User)user);
// 6.放行
return true;
}
// 在controller執行之后攔截 這個我們在這里不需要
// @Override
// public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
// }
// 渲染之后,返回給用戶之前 用戶業務執行完畢我們要銷毀維護信息,避免泄露
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 移除用戶
UserHolder.removeUser();
}
}
public class UserHolder {
private static final ThreadLocal<User> tl = new ThreadLocal<>();
public static void saveUser(User user){
tl.set(user);
}
public static User getUser(){
return tl.get();
}
public static void removeUser(){
tl.remove();
}
}
@Configuration
public class MvcConfig implements WebMvcConfigurer {
// 攔截器的注冊器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.excludePathPatterns(
"/user/code",
"/user/login",
"/shop/**",
"/blog/hot",
"/shop-type/**",
"upload/**",
"voucher/**"
);
}
}
@GetMapping("/me")
public Result me(){
// TODO 獲取當前登錄的用戶并返回
// 直接取就可以了
User user= UserHolder.getUser();
return Result.ok(user);
}
三、隱藏用戶敏感信息
如下圖所示,服務器返回的信息有點多,我們為了保護用戶的信息,我們需要隱藏部分的內容
所以一開始我們存入session的信息就不應該是完整的信息,這樣才能降低服務器的壓力
UserServiceImpl中的login方法
// 7.保存用戶信息到session中 \
// BeanUtil.copyProperties(user, UserDTO.class)) 會自動的將user中的屬性拷貝到UserDTO當中而且也創建出一個UserDTO對象
session.setAttribute("user", BeanUtil.copyProperties(user, UserDTO.class));
取的時候我們也應該做出變化
LoginInterceptor類
// 5.存在,保存用戶信息到ThreadLocal 保存在當前線程里面的
UserHolder.saveUser((UserDTO)user);
此時我們再登錄查詢信息,就還剩下三個字段了
四、session共享問題
多臺Tomcat并不共享session存儲空間,當請求切換到不同的Tomcat服務導致數據丟失的問題
所以這個方案就被pass了
session的替代方案應該滿足:
數據共享內存存儲key、value結構
所以我們選擇Redis
任何一臺Tomcat都能訪問到Redis,這樣就能實現數據共享
總結
原文鏈接:https://blog.csdn.net/weixin_51351637/article/details/127519489
相關推薦
- 2022-11-26 React常見跨窗口通信方式實例詳解_React
- 2022-06-30 C++四種case的詳細介紹小結_C 語言
- 2022-09-15 git驗證線上的版本是否符合預期_相關技巧
- 2023-04-24 Python?argparse中的action=store_true用法小結_python
- 2022-08-25 C語言詳細分析宏定義與預處理命令的應用_C 語言
- 2024-03-07 做springboot-分模塊技術時新建立Maven模塊時報錯
- 2022-03-27 mongodb啟動方法小結_MongoDB
- 2022-04-21 使用mongoshake實現mongodb數據同步的操作方法_MongoDB
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支