自定义 spring mvc 拦截器(近期项目需求实现)
需求背景:特定文件夹下任何文件不经过登录,全部拦截强制跳转登录,并客户端禁止下载服务器定制文件夹文件
经过1天多时间的各种尝试,自定义式的强大拦截器实现了,废话不说了,直接贴代码啦。
demo:
1> 根目录下 index.html 内容:
<a href="html/index.html">index</a><br/>
<a href="html/login3.html">login3.html---</a><br/>
<a href="html/xxx.xx">xxx.xx---</a><br/>
<a href="html/1.jpg">1.jpg---</a><br/>
<a href="html/1.src">1.src---</a><br/>
<a href="html/1.zip">1.zip---</a><br/>
。。。。。任何写啦
2> dispatcher-servlet.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
使Spring支持自动检测组件,如注解的Controller
-->
<context:component-scan base-package="com.yjde.web.controller" /><!--
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/html/" p:suffix=".html" />
--><!--<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
-->
<mvc:interceptors>
<mvc:interceptor>
<!--设置拦截的路径:具体路径的拦截-->
<!--<mvc:mapping path="/*.*" />
<mvc:mapping path="/login1.do" />
<mvc:mapping path="/login2.do" />-->
<mvc:mapping path="/*.*" />
<bean class="com.yjde.web.interceptor.TimeInterceptor">
<!--openingTime 属性指定上班时间-->
<property name="openingTime">
<value>12</value>
</property>
<!--closingTime属性指定下班时间-->
<property name="closingTime">
<value>14</value>
</property>
<!--outsideOfficeHoursPage属性指定提示页面的URL-->
<property name="outsideOfficeHoursPage">
<value>http://localhost:8080/SpringMVCInterceptor/login/login.html
</value>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors><!--
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="message" />
--></beans>
3> web.xml
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/html/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--处理从页面传递中文到后台而出现的中文乱码问题-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4> java 源码:
package com.yjde.web.interceptor;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class TimeInterceptor extends HandlerInterceptorAdapter {
// 继承HandlerInterceptorAdapter类
private int openingTime; // openingTime 属性指定上班时间
private int closingTime; // closingTime属性指定下班时间
private String outsideOfficeHoursPage;// outsideOfficeHoursPage属性指定错误提示页面的URL
// 重写 preHandle()方法,在业务处理器处理请求之前对该请求进行拦截处理
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
Calendar cal = Calendar.getInstance();
System.out.println(">>"+request.getRequestURL());
System.out.println(request.getRequestURI());
int hour = cal.get(Calendar.HOUR_OF_DAY); // 获取当前时间
if (openingTime <= hour && hour < closingTime) { // 判断当前是否处于工作时间段内
return true;
} else {
response.sendRedirect(outsideOfficeHoursPage); // 返回提示页面
return false;
}
}
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object o, ModelAndView mav)
throws Exception {
System.out.println("postHandle");
}
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object o, Exception excptn)
throws Exception {
System.out.println("afterCompletion");
}
public int getOpeningTime() {
return openingTime;
}
public void setOpeningTime(int openingTime) {
this.openingTime = openingTime;
}
public int getClosingTime() {
return closingTime;
}
public void setClosingTime(int closingTime) {
this.closingTime = closingTime;
}
public String getOutsideOfficeHoursPage() {
return outsideOfficeHoursPage;
}
public void setOutsideOfficeHoursPage(String outsideOfficeHoursPage) {
this.outsideOfficeHoursPage = outsideOfficeHoursPage;
}
}
// 可以看出,上面的代码重载了preHandle()方法,该方法在业务处理器处理请求之前被调用。在该方法中,首先获得当前的时间,判断其是否在
// openingTime和closingTime之间,如果在,返回true,这样才会调用业务控制器去处理该请求;否则直接转向一个静态页面,返回
// false,这样该请求就不会被处理。
5>java源码:
package com.yjde.web.controller;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class LoginController {
@RequestMapping(value="/{username}", method = RequestMethod.GET)
public String html(@PathVariable("username") String username,HttpServletRequest request) {
System.out.println("拦截成功:"+username);
return username;
}
}