一、拦截jsp
创建InterceptorJspFilter类实现Filter接口,重写doFilter
@WebFilter("*.jsp")
public class InterceptorJspFilter implements Filter{
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest httpRequest = (HttpServletRequest)arg0;
HttpServletResponse httptResponse = (HttpServletResponse) arg1;
String uri = httpRequest.getRequestURI();
UserEntity userEntity = (UserEntity) httpRequest.getSession().getAttribute("user");
if(userEntity!=null){
chain.doFilter(httpRequest, httptResponse);
}
//包含登录界面就不拦截
else if (uri.contains("login.jsp") {
chain.doFilter(httpRequest, httptResponse);
}
else {
try {
ThisAssection.getThisRuntimeException("用户非法登录已打回登录界面");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
httptResponse.sendRedirect("troIndex.jsp");
}
}
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
拦截controller
springmvc配置拦截器
public class InterceptHandlerController implements HandlerInterceptor{
public List<String> excludeUrls;
public List<String> getExcludeUrls() {
return excludeUrls;
}
public void setExcludeUrls(List<String> excludeUrls) {
this.excludeUrls = excludeUrls;
}
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
}
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
// TODO Auto-generated method stub
arg0.setCharacterEncoding("utf-8");
arg1.setCharacterEncoding("utf-8");
UserEntity userEntity = (UserEntity) arg0.getSession().getAttribute("user");
String uri = arg0.getRequestURI();
for(String str:excludeUrls){
if(uri.contains(str))
return true;
}
if(userEntity == null){
try {
ThisAssection.getThisRuntimeException("用户非法登录已打回界面");
} catch (Exception e) {
//在命令行打印异常信息在程序中出错的位置及原因
e.printStackTrace();
// TODO: handle exception
arg1.sendRedirect("index.jsp");
}
}
return true;
}
}
ThisAssection.java
public class ThisAssection {
/**
* 自定义异常
* @param message
*/
public static void getThisRuntimeException(final String message)throws ThisRuntimeException{
throw new ThisRuntimeException(message);
}
/**
* 断言一个对象为空
* @param message 提示用户的信息
* @param obj 需要断言的对象
* @return Object 返回类型
* @throws ThisRuntimeException
*/
public static Object isObjectNull(final String message,Object obj) throws ThisRuntimeException{
if(obj!=null){
getThisRuntimeException(message);
}
return obj;
}
}
ThisRuntimeException.java
public class ThisRuntimeException extends RuntimeException{
public ThisRuntimeException() {
super();
// TODO Auto-generated constructor stub
}
public ThisRuntimeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public ThisRuntimeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
springmvc.xml配置controller拦截器
<!-- 配置controller拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截地址,拦截所有 -->
<mvc:mapping path="/**"/>
<!-- 排除不需要拦截的地址 -->
<mvc:exclude-mapping path="/register"/>
<mvc:exclude-mapping path="/login"/>
<mvc:exclude-mapping path="/findArticleListView"/>
<mvc:exclude-mapping path="/findFileListView"/>
<mvc:exclude-mapping path="/addAdView"/>
<mvc:exclude-mapping path="/findChartView"/>
<!-- <mvc:exclude-mapping path="/accountActivationCod"/> -->
<!-- 指向自定义的拦截器 -->
<bean class="com.xxx.utils.InterceptHandlerController">
<property name="excludeUrls">
<list>
<value>register.jsp</value>
<value>index.jsp</value>
<value>/view/static</value>
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>