洞察探讨小游戏SDK接入的最佳实践以及对企业跨平台开发的优势
594
2023-07-03
SpringBoot登录判断过程代码实例
这篇文章主要介绍了SpringBoot登录判断代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
<div class="box">
登录
$(".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
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
return extendInfo;
}
public void setExtendInfo(Map
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.prinOJdXzaSUXFtln(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, HttpServletResOJdXzaSUXFponse 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/**");
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~