一 : 配置文件中设置拦截
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.zhiyou100"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="17367648787"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.zhiyou100.inteceptor.Lanjie" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
二 : 设置拦截类
@Component
public class Lanjie extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest req
, HttpServletResponse resp
, Object o
)
throws Exception
{
if (req
.getRequestURL().indexOf("/login.action")!=-1&&
req
.getMethod().toUpperCase().equals("POST")){
return true;
}
Worker worker
=(Worker
) req
.getSession().getAttribute("user");
System
.out
.println(worker
);
if (worker
!=null
&&worker
.getWid() != null
){
return true;
}
req
.setAttribute("login_message", "请先登录在进行操作!");
System
.out
.println(req
.getRequestURL());
resp
.sendRedirect(req
.getContextPath()+"/login.jsp");
return false;
}
}
三 : 登录
@Controller
public class LoginController {
@Autowired
private WorkerLoginService service
;
@Autowired
HttpSession session
;
@RequestMapping(value
= "/login.action", produces
= "text/json;charset=utf-8")
public String
getOneByWnameAndWpwd(String wname
, String wpwd
, HttpServletRequest req
) throws Exception
{
if (wname
== null
|| wname
.equals("")) {
session
.setAttribute("login_message", "用户名不能为空!");
return "redirect:/login.jsp";
} else if (wpwd
== null
|| wpwd
.equals("")) {
session
.setAttribute("login_message", "密码不能为空!");
return "redirect:/login.jsp";
}
Worker worker
= service
.getOneByWname(wname
);
System
.out
.println(wname
);
System
.out
.println("pwd=" + worker
.getWpwd());
if (Md5Util
.verify(wpwd
, Tool
.KEY
, worker
.getWpwd())) {
session
.setAttribute("user", worker
);
return "redirect:/admin.jsp";
}
if (!worker
.getWpwd().equals(wpwd
)) {
session
.setAttribute("login_message", "密码错误!");
return "redirect:/login.jsp";
}
String md5Pwd
= Md5Util
.md5(worker
.getWpwd(), Tool
.KEY
);
worker
.setWpwd(md5Pwd
);
service
.changePwd(worker
);
System
.out
.println("xxxxx" + worker
.getWpwd());
session
.setAttribute("user", worker
);
session
.setAttribute("login_message", "登录成功!");
return "redirect:/admin.jsp";
}
@RequestMapping("/exit.action")
public String
exit(HttpServletRequest req
) {
req
.getSession().invalidate();
return "redirect:/login.action";
}
}
退出之后,想要禁止回退的话,请参考我的另外一篇博客 禁止回退的解决方法
如果出现回退之后陷入登录页面的死循环的话,可以参考以下这篇博客 回退登陆死循环的解决方法