JAVA秒杀系统--登录模块

一:登录接口

 @RequestMapping("/to_login")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/do_login")
    @ResponseBody
    public Result<Boolean> doLogin(HttpServletResponse response, @Valid LoginVo loginVo) {
        log.info(loginVo.toString());
        //登录
//        miaoshaUserService.login(response, loginVo);
        return Result.success(true);
    }

用户通过localhost:8080/login/to_login来访问登录页面,这个Controller给用户返回一个login的前端页面。

二:前端像后端传输数据

<script>
    function login(){
        $("#loginForm").validate({
            submitHandler:function(form){
                console.log("login");

                doLogin();
            }
        });
    }
    function doLogin(){
        g_showLoading();   // 提示框
        console.log("dologin");

        var inputPass = $("#password").val();
        var salt = g_passsword_salt;         // 1a2b3c4d
        var str = ""+salt.charAt(0)+salt.charAt(2) + inputPass +salt.charAt(5) + salt.charAt(4);                                                 
        var password = md5(str);

        $.ajax({
            url: "/login/do_login",
            type: "POST",
            data:{
                mobile:$("#mobile").val(),
                password: password
            },
            success:function(data){
                layer.closeAll();
                console.log(data)

                if(data.code == 0){
                    layer.msg("成功");
                    // window.location.href="/goods/to_list";
                }else{
                    layer.msg(data.msg);
                }
            },
            error:function(){
                layer.closeAll();
                console.log(data)

            }
        });
    }
</script>

前端将password进行第一次MD5加密后将数据传给后端

三:检查数据格式

public class LoginVo {

    @NotNull        // 不能为空
    @IsMobile		// 检查是否是手机号格式
    private String mobile;

    @NotNull        // 不能为空
    @Length(min=32)  // 限定密码长度
    private String password;

    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "LoginVo [mobile=" + mobile + ", password=" + password + "]";
    }
}

后端通过LoginVo来检查数据格式

四:与数据库进行比较

public boolean login(HttpServletResponse response, LoginVo loginVo) {
        if(loginVo == null) {
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        String mobile = loginVo.getMobile();
        System.out.println("start get");
        String formPass = loginVo.getPassword();
        //判断手机号是否存在
//        System.out.println("miaoshamapper: "+miaoshaUserMapper);
        MiaoshaUser user = getById(Long.parseLong(mobile));
        System.out.println("end get");

//        System.out.println("user: "+user);
        if(user == null) {
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        //验证密码
        String dbPass = user.getPassword();
        String saltDB = user.getSalt();
        String calcPass = MD5Util.formPassToDBPass(formPass, saltDB);
//        System.out.println("md5密码:"+calcPass);
//        System.out.println("数据库密码:"+dbPass);
        if(!calcPass.equals(dbPass)) {
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }

        return true;
    }

首先判断数据库中有无这个id,在比较密码是否正确

五:对象缓存

        //生成cookie,来标识用户 ,将用户写道cookie中,传递给客户端,客户端再给服务端
        String token	 = UUIDUtil.uuid();
        addCookie(response, token, user);   //把个人信息放到redis缓存上去

这个还没怎么整好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java实现秒杀系统@Controller @RequestMapping("seckill")//url:/模块/资源/{id}/细分 /seckill/list public class SeckillController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private SeckillService seckillService; @RequestMapping(value="/list",method = RequestMethod.GET) public String list(Model model){ //获取列表页 List list=seckillService.getSeckillList(); model.addAttribute("list",list); //list.jsp+model = ModelAndView return "list";//WEB-INF/jsp/"list".jsp } @RequestMapping(value = "/{seckillId}/detail",method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long seckillId, Model model){ if (seckillId == null){ return "redirect:/seckill/list"; } Seckill seckill = seckillService.getById(seckillId); if (seckill == null){ return "forward:/seckill/list"; } model.addAttribute("seckill",seckill); return "detail"; } //ajax json @RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) @ResponseBody public SeckillResult exposer(@PathVariable("seckillId") Long seckillId){ SeckillResult result; try { Exposer exposer =seckillService.exportSeckillUrl(seckillId); result = new SeckillResult(true,exposer); } catch (Exception e) { logger.error(e.getMessage(),e); result = new SeckillResult(false,e.getMessage()); } return result; } @RequestMapping(value = "/{seckillId}/{md5}/execution", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"} ) @ResponseBody public SeckillResult execute(@PathVariable("seckillId")Long seckillId,

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值