您好,登錄后才能下訂單哦!
本篇文章為大家展示了SpringBoot中怎么判斷登錄過程,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
<!-- 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","用戶名或密碼錯(cuò)誤"); } request.getSession().setAttribute("islogin","true");//如果正確,則在seesion里添加判斷屬性,可供攔截器判斷是否登錄過 return JsonMsg.success(); }
//自定義攔截器//如果未登錄則跳回登錄頁public class SysInterceptor extends HandlerInterceptorAdapter { /** * 進(jìn)入攔截器后首先進(jìn)入的方法 * 返回false則不再繼續(xù)執(zhí)行 * 返回true則繼續(xù)執(zhí)行 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("我是攔截器:我進(jìn)來了"); 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/**"); }}
上述內(nèi)容就是SpringBoot中怎么判斷登錄過程,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。