自定义 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;

}

}

时间: 2024-08-04 12:10:40

自定义 spring mvc 拦截器(近期项目需求实现)的相关文章

Spring Boot 2.X(九):Spring MVC - 拦截器(Interceptor)

拦截器 1.简介 Spring MVC 中的拦截器(Interceptor)类似于 Servlet 开发中的过滤器 Filter,它主要用于拦截用户请求并作相应的处理,它也是 AOP 编程思想的体现,底层通过动态代理模式完成. 2.定义实现类 拦截器有两种实现方式: 1.实现 HandlerInterceptor 接口 2.继承 HandlerInterceptorAdapter 抽象类(看源码最底层也是通过 HandlerInterceptor 接口 实现) 3.HandlerIntercep

对于Spring MVC 拦截器的一些了解

Spring MVC 拦截器的执行顺序 应用场景 假设请求 localhost:8080/ 则要求直接重定向到 localhost:8080/login ; 定义拦截器顺序 permission login 执行顺序 pre 先执行先定义的,而 post 和 after 先执行后定义的. 原文地址:https://www.cnblogs.com/bjio/p/12242920.html

spring MVC拦截器01

spring MVC拦截 作用:身份校验,权限检查,防止非法訪问. 场景:一个bbs系统,用户没有登录就无法发帖或者删除评论; 一个博客系统,没有登录就无法发表博文,无法添加分类,无法删除博文. spring MVC 拦截实现分为2步 (1)编写拦截器类,必须继承org.springframework.web.servlet.HandlerInterceptor 核心方法: public boolean preHandle(HttpServletRequest request, HttpServ

spring mvc拦截器和&lt;mvc:annotation-driven /&gt;的详解

MVC的拦截器 经本人在Spring mvc中对方案1和方案2的测试表明,并没有拦截静态资源,所以可以放心使用方案1和方案2,方案3可以放弃,并且可以放心使用<mvc:annotation-driven />注解. 方案一,(近似)总拦截器,拦截所有url <mvc:interceptors> <bean class="com.app.mvc.MyInteceptor" /> </mvc:interceptors> 为什么叫“近似”,前面

Spring mvc 拦截器配置

首先在web.xml中配置spring mvc入口:DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet.    DispatcherServlet是前置控制器,配置在web.xml文件中的.拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller来处理.  所以我们现在web.xml中加入以下配置: <servlet><!--s

Spring MVC拦截器

一.定义自己的拦截器 需要我们定义的类继承HandlerInterceptor 变成自定义的拦截器 package cn.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servl

Spring MVC拦截器+注解方式实现防止表单重复提交

原理:在新建页面中Session保存token随机码,当保存时验证,通过后删除,当再次点击保存时由于服务器端的Session中已经不存在了,所有无法验证通过. 注,如果是集群的方式,则需要将token放入到缓存中即可. 注解Token代码:java源码  Java代码 复制代码 收藏代码 1[email protected](ElementType.METHOD) 2[email protected] (RetentionPolicy.RUNTIME) 3.public @interface T

spring mvc拦截器HandlerInterceptor

本文主要介绍springmvc中的拦截器,包括拦截器定义和的配置,然后演示了一个链式拦截的测试示例,最后通过一个登录认证的例子展示了拦截器的应用 拦截定义 定义拦截器,实现HandlerInterceptor接口.接口中提供三个方法. public class HandlerInterceptor1 implements HandlerInterceptor{ //进入 Handler方法之前执行 //用于身份认证.身份授权 //比如身份认证,如果认证通过表示当前用户没有登陆,需要此方法拦截不再

Spring MVC 拦截器实现

自定义拦截器类public class SessionInterceptor extends HandlerInterceptorAdapter { public SessionInterceptor() { // TODO Auto-generated constructor stub } private List<String> excludedUrls; //通过属性注册不需要过滤的url list public void setExcludedUrls(List<String&g