您好,登錄后才能下訂單哦!
這篇文章主要介紹了SpringBoot登錄判斷代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
<!-- html登錄代碼 --> <div class="box"> <div class="title">登錄</div> <div class="input"> <label for="name">用戶名</label> <input type="text" name="name" id="name"> <span class="spin"></span> </div> <div class="input"> <label for="pass">密碼</label> <input type="password" name="pass" id="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(); }
//自定義攔截器 //如果未登錄則跳回登錄頁 public class SysInterceptor extends HandlerInterceptorAdapter { /** * 進入攔截器后首先進入的方法 * 返回false則不再繼續執行 * 返回true則繼續執行 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("我是攔截器:我進來了"); HttpSession session = request.getSession(); String islogin = (String)session.getAttribute("islogin"); if(islogin==null){ System.out.println("用戶沒有登錄"); response.sendRedirect("/cch/login"); return false; } System.out.println("用戶已登錄"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
//把攔截器添加到springboot中 @Configuration public class SessionConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //攔截有 "/cch" 前綴的路徑,除了 "/cch/login","/cch/dologin" registry.addInterceptor(new SysInterceptor()).excludePathPatterns("/cch/login","/cch/dologin").addPathPatterns("/cch/**"); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。