SpringMVC使用自定义注解的方式实现session检查
来源:互联网 发布:python 迭代 编辑:程序博客网 时间:2024/06/07 00:26
使用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错误~那么你就改成我这种配置把~我就是这么改过来的。
0 0
- SpringMVC使用自定义注解的方式实现session检查
- Springmvc AOP拦截Controller获取session(自定义注解的方式)
- SpringMVC自定义注解的方式AOP实现 日志管理
- springmvc自定义注解拦截器方式实现注解功能拦截器的配置
- java SpringmvcAOP拦截Controller,获取session(自定义注解的方式)
- 用注解方式实现springMVC
- SpringMVC基于session存储注解的值 (全局使用)
- 使用注解方式开发SpringMVC
- J2EE项目使用自定义注解实现基于SpringMVC + Mybatis + Mysql的读写分离
- Spring AOP + 自定义注解实现Session的验证
- springmvc Aspect 实现自定义注解的日志记录
- 一篇非常经典的springMVC注解实现方式详解
- SpringMVC的服务器验证二(注解方式实现)
- SpringMVC的注解命名方式
- 注解的方式配置springMVC
- springMVC注解方式的校验
- SpringMVC中使用注解方式的对象验证
- 使用Struts2的拦截器和自定义注解方式实现权限控制
- Linux VS Windows
- RaspberryPi+SAE+Wechat打造智能家居篇(三)
- Linux学习笔记(06-11)UNIX环境高级编程 apue.h头文件的配置
- 中断下半部分析之Tasklet
- xUtils框架的介绍
- SpringMVC使用自定义注解的方式实现session检查
- eclipse:错误: 找不到或无法加载主类
- 黑马程序员---java单例设计模式
- android权限大全
- Java 中的静态内部类
- C primer plus(第五版)编程练习第十四章
- 为什么基类指针和引用可以指向派生类对象,但是反过来不行?
- 错误日志解决办法:statement aborts at 6: [INSERT INTO MenuTbl(_id) VALUES (?)] datatype mismatch
- 模式匹配算法KMP