目录
js
$(function () {
alert(233);
// 给登录按钮添加事件
$("#login").click(function () {
let mobile = $("#mobile").val();
let password = $("#password").val();
console.log("mobile=%s,password=%s",mobile,password);
//1.密码加密
//1) 定义固定盐
let salt='f1g2h3j4';
//2) 固定盐混淆
let temp=salt.charAt(1)+""+salt.charAt(5)+password+salt.charAt(0)+""+salt.charAt(3);
//3) 使用MD5完成前端第一次加密
let pwd=md5(temp);
//2.向后台发起登录ajax请求
$.post('/user/toLogin',{
mobile:mobile,
password:pwd
},function(rs){
if(rs.code!=200){//登录失败
alert(rs.msg);
}else
//alert(rs.msg);
location.href="/";//登录成功跳转index.html
},'json');
});
});
user.java
- package com.zking.spbootpro.model;
-
- import com.baomidou.mybatisplus.annotation.TableName;
- import java.time.LocalDateTime;
- import java.io.Serializable;
- import java.util.Date;
-
- import lombok.Data;
- import lombok.EqualsAndHashCode;
-
- /**
- *
- * 用户信息表
- *
- *
- * @author zking
- * @since 2022-11-05
- */
- @Data
- @EqualsAndHashCode(callSuper = false)
- @TableName("t_user")
- public class User implements Serializable {
-
- private long id;
-
- /**
- * 昵称
- */
- private String nickname;
-
- /**
- * MD5(MD5(pass明文+固定salt)+salt)
- */
- private String password;
-
- /**
- * 随机salt
- */
- private String salt;
-
- /**
- * 注册时间
- */
- private Date registerDate;
-
- /**
- * 最后一次登录时间
- */
- private LocalDateTime lastLoginDate;
-
- /**
- * 登录次数
- */
- private Integer loginCount;
-
-
- }
package com.zking.spbootpro.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zking.spbootpro.exception.BusinessException;
import com.zking.spbootpro.mapper.UserMapper;
import com.zking.spbootpro.model.User;
import com.zking.spbootpro.model.dto.UserDto;
import com.zking.spbootpro.service.IRedisService;
import com.zking.spbootpro.service.IUserService;
import com.zking.spbootpro.utils.CookieUtils;
import com.zking.spbootpro.utils.JsonResponseBody;
import com.zking.spbootpro.utils.JsonResponseStatus;
import com.zking.spbootpro.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;/**
*
* 用户信息表 服务实现类
*
*
* @author zking
* @since 2022-11-04
*/
@Service
public class UserServiceImpl extends ServiceImplimplements IUserService {
@Autowired
private UserMapper userMapper;
@Autowired
private IRedisService redisService;@Override
public JsonResponseBody toLogin(UserDto userDto, HttpServletRequest req, HttpServletResponse resp) {
// 1.5.1)判断mobile和password是否为空
// 1.5.2)判断mobile格式是否正确
// 1.5.3)根据用户手机号码查询用户是否存在
User user = userMapper.selectOne(new QueryWrapper()
.eq("id", userDto.getMobile()));// 1.5.4)校验账号
if (user == null)
throw new BusinessException(JsonResponseStatus.USER_USERNAME_ERROR);// 前台传递到后台的密码,要进过工具类md5加密一次,才有可能跟数据库密码匹配上
String pwd = MD5Utils.formPassToDbPass(userDto.getPassword(), user.getSalt());
// 1.5.5)校验密码
if(!pwd.equals(user.getPassword()))
throw new BusinessException(JsonResponseStatus.USER_PASSWORD_ERROR);//6.将登陆用户对象与token令牌进行绑定保存到cookie和redis
//创建登陆令牌token
String token= UUID.randomUUID().toString().replace("-","");
//将token令牌保存到cookie中
CookieUtils.setCookie(req,resp,"token",token,7200);
//将登陆token令牌与用户对象user绑定到redis中
redisService.setUserToRedis(token,user);
//将用户登陆的昵称设置到cookie中
CookieUtils.setCookie(req,resp,"nickname",user.getNickname());
return new JsonResponseBody<>();
}
}
代码
UserArgumentResovler 参数解析器
- package com.zking.spbootpro.config;
-
- import com.zking.spbootpro.exception.BusinessException;
- import com.zking.spbootpro.model.User;
- import com.zking.spbootpro.service.IredisService;
- import com.zking.spbootpro.utils.CookieUtils;
- import com.zking.spbootpro.utils.JsonResponseStatus;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.MethodParameter;
- import org.springframework.stereotype.Component;
- import org.springframework.web.bind.support.WebDataBinderFactory;
- import org.springframework.web.context.request.NativeWebRequest;
- import org.springframework.web.method.support.HandlerMethodArgumentResolver;
- import org.springframework.web.method.support.ModelAndViewContainer;
- import org.springframework.web.servlet.HandlerExceptionResolver;
-
- import javax.servlet.http.HttpServletRequest;
-
- /**
- * 参数解析器
- * @author 锦鲤
- * @site www.lucy.com
- * @company xxx公司
- * @create 2022-11-07 18:25
- */
- @Component
- public class UserArgumentResovler implements HandlerMethodArgumentResolver {
-
- @Autowired
- private IredisService iredisService;
-
- /**
- * supportsParameter的方法返回值
- * true:会使用下面resolveArgument
- * false:则不调用
- * @param methodParameter
- * @return
- */
- @Override
- public boolean supportsParameter(MethodParameter methodParameter) {
- return methodParameter.getParameterType()== User.class;
- }
-
- /**
- * resolveArgument;
- * 具体的业务代码处理
- * @param methodParameter
- * @param modelAndViewContainer
- * @param nativeWebRequest
- * @param webDataBinderFactory
- * @return
- * @throws Exception
- */
- @Override
- public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
- HttpServletRequest request =(HttpServletRequest) nativeWebRequest.getNativeRequest();
- String token = CookieUtils.getCookieValue(request,"token");
- if(token==null){
- throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
- }
- User user= iredisService.getUserToRedis(token);
- if(user ==null){
- throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
- }
-
- return user;
- }
- }
WebConfig
- package com.zking.spbootpro.config;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.method.support.HandlerMethodArgumentResolver;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
- import java.util.List;
-
- /**
- * @author 锦鲤
- * @site www.lucy.com
- * @company xxx公司
- * @create 2022-11-07 18:42
- *
- */
- @Configuration
- public class WebConfig implements WebMvcConfigurer {
-
- @Autowired
- private UserArgumentResovler userArgumentResovler;
-
- /**
- * 配置静态资源访问映射,使用了WebMvcConfigurer会覆盖原有的application.yml文件中的静态资源配置
- * @param registry
- */
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/static/**")
- .addResourceLocations("classpath:/static/");
- }
-
- /**
- * 添加自定义的参数解析器
- * @param resolvers
- */
- @Override
- public void addArgumentResolvers(List
resolvers) { - resolvers.add(userArgumentResovler);
- }
- }
购物车后台搭建
实体类ShopCar 、ShopCarItem
- package com.zking.spbootpro.model.vo;
-
- import java.util.*