资讯网站,wordpress源代码如何在本地编辑,做设计都有什么网站,做简单的html网站目录
一、User类
二、ThreadLocal类
三、用户业务逻辑接口
四、用户业务逻辑接口实现类
五、用户控制层
六、用户登录拦截器
七、拦截器配置类
八、隐藏敏感信息的代码调整 完整的项目资源共享地址#xff0c;当中包含了代码、资源文件以及Nginx#xff08;Wi…目录
一、User类
二、ThreadLocal类
三、用户业务逻辑接口
四、用户业务逻辑接口实现类
五、用户控制层
六、用户登录拦截器
七、拦截器配置类
八、隐藏敏感信息的代码调整 完整的项目资源共享地址当中包含了代码、资源文件以及NginxWindows版和完整的配置 链接https://pan.quark.cn/s/5c28484d7882 提取码cJxQ 其中对于短信登录这块代码已经做了优化通过Redis实现短信验证码登录想要按照Session方式进行登录来学习Session的短信验证码登录流程的可以将工程相关代码按下述的代码进行修改调整。 一、User类
package com.hmdp.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;Data
EqualsAndHashCode(callSuper false)
Accessors(chain true)
TableName(tb_user)
public class User implements Serializable {private static final long serialVersionUID 1L;/*** 主键*/TableId(value id, type IdType.AUTO)private Long id;/*** 手机号码*/private String phone;/*** 密码加密存储*/private String password;/*** 昵称默认是随机字符*/private String nickName;/*** 用户头像*/private String icon ;/*** 创建时间*/private LocalDateTime createTime;/*** 更新时间*/private LocalDateTime updateTime;}二、ThreadLocal类
package com.hmdp.utils;import com.hmdp.entity.User;public class UserHolder {private static final ThreadLocalUser 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();}
}三、用户业务逻辑接口
package com.hmdp.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.hmdp.dto.LoginFormDTO;
import com.hmdp.dto.Result;
import com.hmdp.entity.User;import javax.servlet.http.HttpSession;public interface IUserService extends IServiceUser {Result sendCode(String phone, HttpSession session);Result login(LoginFormDTO loginForm, HttpSession session);
}四、用户业务逻辑接口实现类
package com.hmdp.service.impl;import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.LoginFormDTO;
import com.hmdp.dto.Result;
import com.hmdp.entity.User;
import com.hmdp.mapper.UserMapper;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import static com.hmdp.utils.SystemConstants.USER_NICK_NAME_PREFIX;Slf4j
Service
public class UserServiceImpl extends ServiceImplUserMapper, User implements IUserService {Resourceprivate StringRedisTemplate stringRedisTemplate;Overridepublic Result sendCode(String phone, HttpSession session) {// 1.校验手机号if (RegexUtils.isPhoneInvalid(phone)) {// 2.如果不符合返回错误信息return Result.fail(手机号格式错误);}// 3.符合生成验证码String code RandomUtil.randomNumbers(6);// 4.保存验证码到 sessionsession.setAttribute(code: phone, code);// 5.发送验证码log.debug(发送短信验证码成功验证码{}, code);// 返回okreturn Result.ok();}Overridepublic Result login(LoginFormDTO loginForm, HttpSession session) {// 1.校验手机号String phone loginForm.getPhone();if (RegexUtils.isPhoneInvalid(phone)) {// 2.如果不符合返回错误信息return Result.fail(手机号格式错误);}// 2. 校验验证码Object cacheCode session.getAttribute(code: loginForm.getPhone());String code loginForm.getCode();// 3. 不一致则报错if (cacheCode null || !cacheCode.toString().equals(code)) {return Result.fail(验证码错误);}// 4. 一致则根据手机号查询用户User user query().eq(phone, phone).one();// 5. 判断用户是否存在if (user null) {// 6. 不存在则创建用户并保存createUserWithPhone(phone);}// 7. 保存用户信息到session中session.setAttribute(user, user);// 8.返回tokenreturn Result.ok();}private User createUserWithPhone(String phone) {// 1.创建用户User user new User();user.setPhone(phone);user.setNickName(USER_NICK_NAME_PREFIX RandomUtil.randomString(10));// 2.保存用户save(user);return user;}
}五、用户控制层
package com.hmdp.controller;import com.hmdp.dto.Result;
import com.hmdp.service.IUserInfoService;
import com.hmdp.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;Slf4j
RestController
RequestMapping(/user)
public class UserController {Resourceprivate IUserService userService;/*** 发送手机验证码*/PostMapping(code)public Result sendCode(RequestParam(phone) String phone, HttpSession session) {// 发送短信验证码并保存验证码return userService.sendCode(phone, session);}/*** 登录功能* param loginForm 登录参数包含手机号、验证码或者手机号、密码*/PostMapping(/login)public Result login(RequestBody LoginFormDTO loginForm, HttpSession session){// 实现登录功能return userService.login(loginForm, session);}GetMapping(/me)public Result me(){// 获取当前登录的用户并返回User user UserHolder.getUser();return Result.ok(user);}
}
六、用户登录拦截器
package com.hmdp.utils;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 {Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// 1. 获取SessionHttpSession session request.getSession();// 2. 获取session中的用户Object user session.getAttribute(user);// 3. 判断用户是否存在if (user null) {// 4. 不存在则拦截并返回401状态码response.setStatus(401);return false;}// 5. 存在则保存用户信息到ThreadLocalUserHolder.saveUser((User) user);return true;}Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {// 移除用户UserHolder.removeUser();}
}七、拦截器配置类
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 {Overridepublic void addInterceptors(InterceptorRegistry registry) {// 登录拦截器registry.addInterceptor(new LoginInterceptor()).excludePathPatterns(/shop/**,/voucher/**,/shop-type/**,/upload/**,/blog/hot,/user/code,/user/login);}
}八、隐藏敏感信息的代码调整
上述代码中存入Session中的用户信息涉及到了很多的敏感字段如手机号、密码等会有安全风险所以对代码优化如下
package com.hmdp.dto;import lombok.Data;Data
public class UserDTO {private Long id;private String nickName;private String icon;
}package com.hmdp.utils;import com.hmdp.dto.UserDTO;public class UserHolder {private static final ThreadLocalUserDTO tl new ThreadLocal();public static void saveUser(UserDTO user){tl.set(user);}public static UserDTO getUser(){return tl.get();}public static void removeUser(){tl.remove();}
}package com.hmdp.controller;import com.hmdp.dto.LoginFormDTO;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.service.IUserInfoService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;Slf4j
RestController
RequestMapping(/user)
public class UserController {Resourceprivate IUserService userService;Resourceprivate IUserInfoService userInfoService;/*** 发送手机验证码*/PostMapping(code)public Result sendCode(RequestParam(phone) String phone, HttpSession session) {// 发送短信验证码并保存验证码return userService.sendCode(phone, session);}/*** 登录功能* param loginForm 登录参数包含手机号、验证码或者手机号、密码*/PostMapping(/login)public Result login(RequestBody LoginFormDTO loginForm, HttpSession session){// 实现登录功能return userService.login(loginForm, session);}GetMapping(/me)public Result me(){// 获取当前登录的用户并返回UserDTO user UserHolder.getUser();return Result.ok(user);}
}
package com.hmdp.utils;import com.hmdp.dto.UserDTO;
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 {Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// 1. 获取SessionHttpSession session request.getSession();// 2. 获取session中的用户Object user session.getAttribute(user);// 3. 判断用户是否存在if (user null) {// 4. 不存在则拦截并返回401状态码response.setStatus(401);return false;}// 5. 存在则保存用户信息到ThreadLocalUserHolder.saveUser((UserDTO) user);return true;}Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {// 移除用户UserHolder.removeUser();}
} 文章转载自: http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.jkfyt.cn.gov.cn.jkfyt.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.ndmbz.cn.gov.cn.ndmbz.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.fpqsd.cn.gov.cn.fpqsd.cn http://www.morning.jhfkr.cn.gov.cn.jhfkr.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.ykrck.cn.gov.cn.ykrck.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.dbrnl.cn.gov.cn.dbrnl.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.npqps.cn.gov.cn.npqps.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.yrlfy.cn.gov.cn.yrlfy.cn http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.fdsbs.cn.gov.cn.fdsbs.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.mrckk.cn.gov.cn.mrckk.cn http://www.morning.nyqnk.cn.gov.cn.nyqnk.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.cwrnr.cn.gov.cn.cwrnr.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.dkqyg.cn.gov.cn.dkqyg.cn http://www.morning.xhklb.cn.gov.cn.xhklb.cn http://www.morning.plxhq.cn.gov.cn.plxhq.cn http://www.morning.wjqbr.cn.gov.cn.wjqbr.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.lgkbn.cn.gov.cn.lgkbn.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.mgnrc.cn.gov.cn.mgnrc.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.ngcsh.cn.gov.cn.ngcsh.cn http://www.morning.pylpd.cn.gov.cn.pylpd.cn http://www.morning.mwkwg.cn.gov.cn.mwkwg.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.ckxd.cn.gov.cn.ckxd.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.mplb.cn.gov.cn.mplb.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.ljsxg.cn.gov.cn.ljsxg.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.cjxqx.cn.gov.cn.cjxqx.cn http://www.morning.lhxrn.cn.gov.cn.lhxrn.cn