当前位置 主页 > 网站技术 > 代码类 >

    SpringBoot登录判断过程代码实例

    栏目:代码类 时间:2019-12-27 12:07

    这篇文章主要介绍了SpringBoot登录判断代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    <!--  html登录代码  -->
    
        <div class="box">
          <div class="title">登录</div>
          <div class="input">
            <label for="name">用户名</label>
            <input type="text" name="name" >
            <span class="spin"></span>
          </div>
          <div class="input">
            <label for="pass">密码</label>
            <input type="password" name="pass" >
            <span class="spin"></span>
          </div>
          <div class="button login">
            <button>
              <span>登录</span>
              <i class="fa fa-check"></i>
            </button>
          </div>
          <a href="javascript:" rel="external nofollow" class="pass-forgot">忘记密码?</a>
        </div>
    <!-- javasript 登录代码 -->
    $(".button").click(function(e) {
       $("button", this).addClass('active');
       if ($(".button").hasClass("login")){
         $.ajax({
          url:"/cch/dologin",
          type:"POST",
          data:{
            name:$("#name").serialize(),
            pass:$("#pass").serialize(),
          },
          success:function (result) {
            if(result.code==100){
             window.location.href="/cch/main" rel="external nofollow" ;
            }else {
             alert(result.extendInfo.login_error);
            }
          }
         })
       }
      })
    //JsonMsg工具类
    public class JsonMsg {
      private int code;
      private String msg;
      private Map<String,Object> extendInfo = new HashMap<String, Object>();
    
      public int getCode() {
        return code;
      }
    
      public void setCode(int code) {
        this.code = code;
      }
    
      public String getMsg() {
        return msg;
      }
    
      public void setMsg(String msg) {
        this.msg = msg;
      }
    
      public Map<String, Object> getExtendInfo() {
        return extendInfo;
      }
    
      public void setExtendInfo(Map<String, Object> extendInfo) {
        this.extendInfo = extendInfo;
      }
    
      public static JsonMsg success(){
        JsonMsg res = new JsonMsg();
        res.setCode(100);
        res.setMsg("操作成功");
        return res;
      }
    
      public static JsonMsg fail(){
        JsonMsg res = new JsonMsg();
        res.setCode(200);
        res.setMsg("操作失败");
        return res;
      }
    
      public JsonMsg addInfo(String key,Object object){
        this.extendInfo.put(key,object);
        return this;
      }
    }
    /**
    * controller
    * 判断用户名和密码是否正确
    */
      @RequestMapping(value = "/cch/dologin",method = RequestMethod.POST)
      @ResponseBody
      public JsonMsg dologin(HttpServletRequest request){
        String username = request.getParameter("name");
        String password = request.getParameter("pass");
        System.out.println(username+" "+password);
        if (!"name=admin".equals(username) || !"pass=123".equals(password)){
          return JsonMsg.fail().addInfo("login_error","用户名或密码错误");
        }
        request.getSession().setAttribute("islogin","true");//如果正确,则在seesion里添加判断属性,可供拦截器判断是否登录过
        return JsonMsg.success();
      }