使用SpringMVC后,感觉注解方式甚是好用,于是想着自定义注解来实现session检查,用到了SpringMVC的拦截器。
1.自定义注解。
package com.xxx.order;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SessionCheck {
boolean check() default true;
}
2.拦截器。
package com.raycloud.order;
import com.raycloud.order.common.Constant;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 描述:
*
* @author chenpeng
* @date 2015/6/11 15:16
*/
public class SessionInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
SessionCheck sessionCheck = handlerMethod.getMethod().getAnnotation(SessionCheck.class);
if (sessionCheck != null && sessionCheck.check()) {
System.out.println("你遇到了:@sessionCheck");
response.getOutputStream().print("{\"message\":\"session invalid\",\"result\":700}");
return false;
} else {
System.out.println("未受到拦截");
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
3.拦截器配置。
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<context:component-scan base-package="com.raycloud"/>
<mvc:annotation-driven/>
<mvc:interceptors>
<bean class="com.raycloud.order.SessionInterceptor" />
</mvc:interceptors>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
上面两个还好写,这个配置是找了不少时间,具体的可以看这里
========================》
使用方法:
这样就能实现自定义注解方式的拦截了哈,拦截成功后会打印出自定义的信息。
控制台信息输出:
总结:
难点在于SpringMVC的配置了,遇到过各种问题:
1.配置好了拦截器,但拦截器没有起到任何效果
如果你用的是DefaultAnnotationHandlerMapping,而且拦截器路径什么的都是正确的,那么你把<mvc:annotation-driven/>去掉试试
2.拦截器可以用,但是无法自定义注解
如果用的DefaultAnnotationHandlerMapping,需要在后面再引入<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
3.自定义注解ok了,拦截页面跳转也ok了,但是拦截json数据会出现406错误~那么你就改成我这种配置把~我就是这么改过来的。