您好,登录后才能下订单哦!
本篇文章为大家展示了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。