這篇文章主要介紹了SpringBoot登錄判斷代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
10年積累的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有望奎免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
$(".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 MapextendInfo = new HashMap (); 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 getExtendInfo() { return extendInfo; } public void setExtendInfo(Map 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中 @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/**"); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。