您好,登錄后才能下訂單哦!
本篇文章為大家展示了SpringBoot中怎么判斷登錄過程,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
<!-- html登錄代碼 --> <p class="box"> <p class="title">登錄</p> <p class="input"> <label for="name">用戶名</label> <input type="text" name="name" id="name"> <span class="spin"></span> </p> <p class="input"> <label for="pass">密碼</label> <input type="password" name="pass" id="pass"> <span class="spin"></span> </p> <p class="button login"> <button> <span>登錄</span> <i class="fa fa-check"></i> </button> </p> <a href="javascript:" rel="external nofollow" class="pass-forgot">忘記密碼?</a> </p>
<!-- 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中@Configurationpublic 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/**"); }}
上述內容就是SpringBoot中怎么判斷登錄過程,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。