<-! Html login code ->
<div class="box">
<div class="title">登录</div>
<div class="input">
<label for="name">用户名</label>
<input type="text" name="name" id="name">
<span class="spin"></span>
</div>
<div class="input">
<label for="pass">密码</label>
<input type="password" name="pass" id="pass">
<span class="spin"></span>
</div>
<div class="button login">
<button>
<span>登录</span>
<i class="fa fa-check"></i>
</button>
</div>
<a href="javascript:" class="pass-forgot">忘记密码?</a>
</div>
<!-- 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";
}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 ( "successful operation" );
return RES;
}
public static JsonMsg fail(){
JsonMsg res = new JsonMsg();
res.setCode ( 200 );
res.setMsg ( "operation failed" );
return RES;
}
public JsonMsg addInfo(String key,Object object){
this.extendInfo.put(key,object);
return this;
}
}
/ **
* the Controller
* determine the user name and password are correct
* /
@ 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", "to true" ); // if correct, add attributes to determine seesion, the interceptor is available to determine whether logged
return JsonMsg.success ();
}
// custom interceptor
// then jump back to the login page if the unregistered
public class SysInterceptor the extends HandlerInterceptorAdapter {
/ **
* After entering into the first interceptor method
* Returns false then no longer continue
* Returns true proceed
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println ( "I am the interceptor: I came" );
HttpSession session = request.getSession();
String islogin = (String)session.getAttribute("islogin");
if(islogin==null){
System.out.println ( "user is not logged" );
response.sendRedirect("/cch/login");
return false;
}
System.out.println ( "User logged in" );
return to 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 {
}
}
// add the interceptor to springboot
@Configuration
public class SessionConfig the implements WebMvcConfigurer {
@Override
public void addInterceptors (InterceptorRegistry Registry) {
// knockdown have "/ cch" prefix path, in addition to "/ cch / login", " / cch / dologin"
registry.addInterceptor(new SysInterceptor()).excludePathPatterns("/cch/login","/cch/dologin").addPathPatterns("/cch/**");
}
}