美文网首页
Spring mvc 拦截器

Spring mvc 拦截器

作者: gentel_liao | 来源:发表于2018-09-13 08:21 被阅读0次

1.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/tx 
            http://www.springframework.org/schema/tx/spring-tx-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/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">

</beans>  

2.java拦截器编写

创建路径


image.png
package com.xxx.unionpay.intercerptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class UnionPayIntercerptor implements HandlerInterceptor {
    private final static Logger log = LoggerFactory.getLogger("UnionPayIntercerptor");

    private List<String> excludedUrls;
    public List<String> getExcludedUrls() {  return excludedUrls; }
    public void setExcludedUrls(List<String> excludedUrls) { this.excludedUrls = excludedUrls; }

    /**
     * 返回值:表示是否需要将当前请求拦截
     * true: 请求继续运行
     * false:请求被终止
     * o:    表示被拦截的请求目标对象
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //判断请求是否需要使用该拦截器,在spring_mvc配置里面
        String requestUri = httpServletRequest.getRequestURI();
        for (String url : excludedUrls){
            if (requestUri.endsWith(url)){
                return true;
            }
        }

        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        //可通过modelAndView参数改变现实的视图,或修改发往视图的方法
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}

3.注册拦截器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx
            http://www.springframework.org/schema/tx/spring-tx-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/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">

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/xxx/v1.0/xxx/*.fgl*"/>
            <bean class="com.xxx.unionpay.intercerptor.UnionPayIntercerptor">
                <property name="excludedUrls">
                    <list>
                        <value>xxx/v1.0/xxx/xxx.fgl</value>
                        <value>xxx/v1.0/xxx/xxx.fgl</value>
                        <value>xxx/v1.0/xxx/xxx.fgl</value>
                        <value>xxx/v1.0/xxx/xxx.fgl</value>
                        <value>xxx/v1.0/xxx/xxx.fgl</value>
                        <value>xxx/v1.0/xxx/xxx.fgl</value>
                    </list>
                </property>
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>
    
</beans>

相关文章

网友评论

      本文标题:Spring mvc 拦截器

      本文链接:https://www.haomeiwen.com/subject/gtebgftx.html