Spring MVC基础知识整理➣拦截器和自定义注解
概述
Spring MVC中通过注解来对方法或者类进行动态的说明或者标注,类似于配置标识文件的属性信息。当标注的类或者方式被使用时候,通过提取注解信息来达到对类的动态处理。在 MVC中,我们常用的注解@Controller、 @RequestMapping等,通过设置注解里面的方法值来实现不同数据的处理方式。
如@RequestMapping(value="/City",method=RequestMethod.GET),我们标注请求映射地址和请求的方式;通过解读这些属性信息,来实现内部不通的处理方式。
自定义注解
新建Annotation(注解),实例代码如下
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)//保留时间,一般注解就是为了框架开发时代替配置文件使用,JVM运行时用反射取参数处理,所以一般都为RUNTIME类型
public @interface AuthPassport {
//是否启用验证信息
boolean validate() default true;
//权限等级编码
String PowerCode() default "admin";
}
添加方法的注释类
public class HelloWorldController
{
@AuthPassport(validate=true,PowerCode="EveryOne")
@RequestMapping(value={"/*","/say"},method=RequestMethod.GET)
public ModelAndView China()
{
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", "Hello World!");
modelAndView.setViewName("HelloWorld/CIndex");
return modelAndView;
}
}
获取注解类信息
import java.lang.reflect.Method;
public class TestThis {
public static void main(String[] args) throws Exception {
// 提取到被注解的方法Method,这里用到了反射的知识
Method method = Class.forName("HelloWorldController").getDeclaredMethod("China");
// 从Method方法中通过方法getAnnotation获得我们设置的注解
AuthPassport oneAnnotation = method.getAnnotation(AuthPassport.class);
// 得到注解的俩参数
System.out.println(oneAnnotation.validate());
System.out.println(oneAnnotation.PowerCode());
}
}
拦截器
针对MVC的数据请求,我们可以配置响应的Bean(拦截器)进行数据拦截,自定义拦截器需要继承HandlerInterceptorAdapter类,重写该类的三个方法(preHandle、postHandle、afterCompletion),其中afterCompletion响应之后触发的方法,preHandler响应之前处理方法,postHandle是标识响应中触发的方法。我们可以在preHandler里面进行数据的校验,比如权限的校验、登录信息的校验等信息的处理。HandlerInterceptorAdapter的源码:
public interface HandlerInterceptor {
boolean preHandle(
HttpServletRequest request, HttpServletResponse response,
Object handler)
throws Exception;
void postHandle(
HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView)
throws Exception;
void afterCompletion(
HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex)
throws Exception;
}
自定义拦截器如下
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import justin.com.comclass.AuthPassport; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class AuthInterceptor extends HandlerInterceptorAdapter { @Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if(handler.getClass().isAssignableFrom(HandlerMethod.class))
{
AuthPassport authpassport=((HandlerMethod) handler).getMethodAnnotation(AuthPassport.class);//使用自定义注解
if(authpassport==null || authpassport.validate()==false)
{
return true;
}
else
{
System.out.println("测试注解");
boolean IsValieData=request.isRequestedSessionIdValid();
//在这里实现自己的权限验证逻辑
if(!IsValieData)//如果验证成功返回true(这里直接写false来模拟验证失败的处理)
return true;
else//如果验证失败
{
//返回到登录界面
response.sendRedirect("../login/index");
return false;
}
}
}
else
{
return true;
}
}
}
在spring-servletconfig.xml 中配置拦截器信息
<mvc:interceptors>
<!-- 如果不定义 mvc:mapping path 将拦截所有的URL请求 -->
<bean class="justin.com.fileter.AuthInterceptor"></bean>
</mvc:interceptors>
至此MVC自定义的拦截器整理完毕!
Spring MVC基础知识整理➣拦截器和自定义注解的更多相关文章
- Spring MVC基础知识整理➣国际化和异常处理
概述 Spring框架为WEB项目提供了国际化以及异常处理机制.所谓的国际化也就是不同国籍,显示不同国籍的语言与符号.异常处理,也就是能够捕获WEB项目下的所有异常信息,并能处理记录这些异常信息机制. ...
- Spring MVC基础知识整理➣环境搭建和Hello World
概述 Spring MVC属于SpringFrameWork的产品,采用Model-View-Controller进行数据交互,已经融合在Spring Web Flow里面.Spring 框架提供了构 ...
- Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库
概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...
- Spring MVC基础知识整理➣数据校验与格式化
概述 将view中Form的数据提交到后台之后,后台如何验证数据的有效性?在这里Spring MVC提供了相应的Hibernate类包(hibernate-validator-4.3.1.Final. ...
- Spring MVC基础知识整理➣View与Controller数据交互
概述 Spring MVC是由View—Controller—Model组成,其中View和Controller的数据交互,成为了关注的核心点.MVC中,我们将View中的数据传递到Controlle ...
- Spring MVC全局异常处理与拦截器校检
在使用Spring MVC进行开发时,总是要对系统异常和用户的异常行为进行处理,以提供给用户友好的提示,也可以提高系统的安全性. 拦截系统响应错误 首先是拦截系统响应错误,这个可以在web.xml中配 ...
- Spring MVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- spring mvc 用cookie和拦截器实现自动登录(/免登录)
Cookie/Session机制详解:http://blog.csdn.net/fangaoxin/article/details/6952954 SpringMVC记住密码功能:http://blo ...
- Spring框架系列(一)--Spring MVC基础知识
Web项目开发过程中一般都是使用MVC(Model-View-Controller)模式,早先的Struts2到Spring MVC,再到现在Spring Boot,都是相似的思 路.Spring B ...
随机推荐
- python3+selenium入门08-鼠标事件
使用click()可以模拟鼠标的左键点击事件,现在的web页面中有很多其他的鼠标交互方式,比如鼠标右击.双击.悬停.鼠标拖放等功能.在WebDriver中,将这些关于鼠标操作的方法封装在ActionC ...
- 游记-NOIP2018
Day -3 受蛊惑跑到理工大去试机,意外发现home里的noilinux账户下有个压缩包,而且还试对了密码,怀着 激动 紧张的心情,打开来看,里面写着 (写出来我就会被禁赛了): asdfasdra ...
- javascript for循环 日期 select
2016年12月28日 20:01:54 星期三 html: <a href="aaaa">AAAA</a> <a href="bbbb&q ...
- Laravel资源理由器跟隐式控制的对比及是怎样的吧?- Route::resource vs Route::controller
stackoverflow找到的问题:http://stackoverflow.com/questions/23505875/laravel-routeresource-vs-routecontrol ...
- MySQL NULL处理
-- 首先在用户表中插入数据如下 TRUNCATE TABLE UserInfo ; INSERT INTO `userinfo`(`ID`,`UserName`,`UserLogin`,`User ...
- 【原创】大数据基础之Flume(2)kudu sink
kudu中的flume sink代码路径: https://github.com/apache/kudu/tree/master/java/kudu-flume-sink kudu-flume-sin ...
- Socket实现断线重连
客户端维护一个线程安全的待发送信息队列 开启死循环 判断Socket = null 调用Socket的sendUrgentData(0xFF)发送1个字节的心跳包 捕捉到连接异常后就关 ...
- MySQL跟踪SQL执行之开启慢查询日志
查询慢查询相关参数 show variables like '%quer%'; slow_query_log(是否记录慢查询) slow_query_log_file(慢日志文件路径) ...
- mysql8:caching-sha2-password问题
参考文章:https://blog.csdn.net/u010026255/article/details/80062153 问题:caching-sha2-password 处理: ALTER US ...
- 路由跟踪表满,日志报错nf_conntrack: table full, dropping packet.
“连接跟踪表已满,开始丢包”!相信不少用iptables的同学都会见过这个错误信息吧,这个问题曾经也困扰过我好长一段时间.此问题的解决办法有四种(nf_conntrack 在CentOS 5 / ke ...