美文网首页
2018-12-08 登录功能(上)

2018-12-08 登录功能(上)

作者: 培根好吃 | 来源:发表于2018-12-08 23:15 被阅读0次

    1.手机号校验

    利用

    手机号校验

    public class ValidatorUtil {
    
        private static final  Pattern mobile_pattern=Pattern.compile("1\\d{10}");
        public static boolean isMobile(String src) {
            if(StringUtils.isEmpty(src)) {
                return false;
            }
        Matcher m=mobile_pattern.matcher(src);
            return m.matches();         
            
        }
    
        public static void main(String[] args) {
            System.out.println(isMobile("15757855000"));
            System.out.println(isMobile("157578550sfsg"));
        }
    }
    

    2.在templates里面建login.html页面

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>登录</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        
        <!-- jquery -->
        <script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
        <!-- bootstrap -->
        <link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/bootstrap.min.css}" />
        <script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>
        <!-- jquery-validator -->
        <script type="text/javascript" th:src="@{/jquery-validation/jquery.validate.min.js}"></script>
        <script type="text/javascript" th:src="@{/jquery-validation/localization/messages_zh.min.js}"></script>
        <!-- layer    弹框-->
        <script type="text/javascript" th:src="@{/layer/layer.js}"></script>
        <!-- md5.js -->
        <script type="text/javascript" th:src="@{/js/md5.min.js}"></script>
        <!-- common.js    展示loading框, 固定salt-->
        <script type="text/javascript" th:src="@{/js/common.js}"></script>
        
    </head>
    <body>
    
    <form name="loginForm" id="loginForm" method="post"  style="width:50%; margin:0 auto">
    
        <h2 style="text-align:center; margin-bottom: 20px">用户登录</h2>
        
        <div class="form-group">
            <div class="row">
                <label class="form-label col-md-4">请输入手机号码</label>
                <div class="col-md-5">
                    <input id="mobile" name = "mobile" class="form-control" type="text" placeholder="手机号码" required="true"  minlength="11" maxlength="11" />
                </div>
                <div class="col-md-1">
                </div>
            </div>
        </div>
        
        <div class="form-group">
                <div class="row">
                    <label class="form-label col-md-4">请输入密码</label>
                    <div class="col-md-5">
                        <input id="password" name="password" class="form-control" type="password"  placeholder="密码" required="true" minlength="6" maxlength="16" />
                    </div>
                </div>
        </div>
        
        <div class="row">
                    <div class="col-md-5">
                        <button class="btn btn-primary btn-block" type="reset" onclick="reset()">重置</button>
                    </div>
                    <div class="col-md-5">
                        <button class="btn btn-primary btn-block" type="submit" onclick="login()">登录</button>
                    </div>
         </div>
         
    </form>
    </body>
    <script>
    
    function login(){
        $("#loginForm").validate({
            submitHandler:function(form){
                 doLogin();
            }    
        });
    }
    
    
     function doLogin(){
        g_showLoading();
        var pass=$("#password").val();
        var salt_fixed=g_passsword_salt;
        var str=""+salt_fixed.charAt(0)+salt_fixed.charAt(2)+pass+salt_fixed.charAt(5)+salt_fixed.charAt(4);
        var password=md5(str);
        $ajax({
            url:"/login/do_login",
            type:"POST",
            data:{
                mobile:$("#mobile").val(),
                password:password
            },
            success:function(data){
                layer.closeAll();
                if(data.code == 0){
                    layer.msg("成功");
                    window.location.href="/goods/to_list";
                }else{
                    layer.msg(data.msg);
                }
                console.log(data);
            },
            error:function(){
            
                    }
        });
    } 
     
    </script>
    </html>
    

    3.将数据库的秒杀对象引入,作对比

    MiaoshaUser

    package com.ryan.miaosha.domain;
    
    import java.util.Date;
    
    public class MiaoshaUser {
        private Long id;
        private String nickname;
        private String password;
        private String salt;
        private String head;
        private Date registerDate;
        private Date lastLoginDate;
        private Integer loginCount;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getNickname() {
            return nickname;
        }
        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getSalt() {
            return salt;
        }
        public void setSalt(String salt) {
            this.salt = salt;
        }
        public String getHead() {
            return head;
        }
        public void setHead(String head) {
            this.head = head;
        }
        public Date getRegisterDate() {
            return registerDate;
        }
        public void setRegisterDate(Date registerDate) {
            this.registerDate = registerDate;
        }
        public Date getLastLoginDate() {
            return lastLoginDate;
        }
        public void setLastLoginDate(Date lastLoginDate) {
            this.lastLoginDate = lastLoginDate;
        }
        public Integer getLoginCount() {
            return loginCount;
        }
        public void setLoginCount(Integer loginCount) {
            this.loginCount = loginCount;
        }
    }
    

    MiaoshaUserDao

    package com.ryan.miaosha.dao;
    
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    
    import com.ryan.miaosha.domain.MiaoshaUser;
    
    @Mapper
    public interface MiaoshaUserDao {
        
        @Select("select * from miaosha_user where id = #{id}")
        public MiaoshaUser getById(@Param("id")long id);
    }
    
    

    MiaoshaUserService

    @Service
    public class MiaoshaUserService {
        
        
        //public static final String COOKI_NAME_TOKEN = "token";
        
        @Autowired
        MiaoshaUserDao miaoshaUserDao;
        
        @Autowired
        RedisService redisService;
        
        public MiaoshaUser getById(long id) {
            return miaoshaUserDao.getById(id);
        }
    }
        public CodeMsg login(LoginVo loginVo) {
            String mobile = loginVo.getMobile();
            long id=Long.parseLong(mobile);
            MiaoshaUser userById = getById(id);
            if(userById==null) {
                return CodeMsg.USER_NOT_EXIST;
            }
            String saltDB = userById.getSalt();
            String passWEB=loginVo.getPassword();
            String passValidated=MD5Util.WebPassToDB(passWEB, saltDB);
            String passDB=userById.getPassword();
            if(!passDB.equals(passValidated)) {
                return CodeMsg.PASSWORD_ERROR;
            }
            return CodeMsg.SUCCESS; 
        }
    
    

    LoginController

    @Controller
    @RequestMapping("/login")
    public class LoginController {
    
        private static Logger log=LoggerFactory.getLogger(LoginController.class);
        @Autowired
        UserService userService;
        @Autowired
        RedisService redisService;
        @Autowired
        MiaoshaUserService miaoshaUserService;
        
        @RequestMapping("/to_login")
        String tologin() {
            return "login";
        } 
        //请求成功    为什么传入loginVo  因为要接收网页表单传来的参数  将手机号和密码传入一个对象中 
        @RequestMapping("/do_login")
        @ResponseBody
        Result<Boolean> dologin(LoginVo loginVo) {
            String mobile = loginVo.getMobile();
            String passInput=loginVo.getPassword();
            if(StringUtils.isEmpty(mobile)) {
                return Result.error(CodeMsg.MOBILE_EMPTY);
            }
            if(!ValidatorUtil.isMobile(mobile)) {
                return Result.error(CodeMsg.MOBILE_ERROR);
            }
            if(StringUtils.isEmpty(passInput)) {
                return Result.error(CodeMsg.PASSWORD_EMPTY);
            }
             log.info(loginVo.toString());
             //登录
             CodeMsg cm = miaoshaUserService.login(loginVo);
             if(cm.getCode()==0) {
                 return Result.success(true);
             }else {
                 return Result.error(cm);
             }
            
        }
        
    }
    

    CodeMsg

    package com.ryan.miaosha.domain;
    
    public class CodeMsg {
    
        private int code;
        private String msg;
        /*
         * 按照模块定义CodeMsg
         * */
        public static CodeMsg SUCCESS=new CodeMsg(0,"SUCCESS");
        public static CodeMsg SERVER_ERROR=new CodeMsg(500100,"服务器异常");
        public static CodeMsg PASSWORD_EMPTY=new CodeMsg(500211,"密码为空");
        public static CodeMsg MOBILE_EMPTY=new CodeMsg(500212,"手机号为空");
        public static CodeMsg MOBILE_ERROR=new CodeMsg(500213,"手机号格式错误");
        public static CodeMsg USER_NOT_EXIST=new CodeMsg(500214,"用户bububu不存在");
        public static CodeMsg PASSWORD_ERROR=new CodeMsg(500215,"密码错误");
        
        public CodeMsg() {
            
        }
        private CodeMsg(int code, String msg) {
            this.code=code;
            this.msg=msg;
        }
        public int getCode() {
            return code;
        }
        public String getMsg() {
            return msg;
        }
        
        
    }
    
    

    相关文章

      网友评论

          本文标题:2018-12-08 登录功能(上)

          本文链接:https://www.haomeiwen.com/subject/vegchqtx.html