配置DelegatingFilterProxy使用Spring管理filter

项目环境:JDK7 + Maven3.04

0. 项目使用springmvc作为controller层

1. 引入spring-security

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-web</artifactId>
  <version>4.0.1.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-config</artifactId>
  <version>4.0.1.RELEASE</version>
</dependency>

2. 在spring context中添加namespace

<beans  ... xmlns:security="http://www.springframework.org/schema/security"
  xsi:schemaLocation="...
                        http://www.springframework.org/schema/security
                        http://www.springframework.org/schema/security/spring-security-4.0.xsd">

3. 在spring context中添加filter chain配置

<bean id=‘swaggerFilter‘ class="com.education.web.swagger.SwaggerFilter">
    <constructor-arg value=‘${education.swagger.enabled}‘ />
  </bean>
  <bean id=‘versionFilter‘ class="com.education.web.spring.VersionFilter">
    <constructor-arg value=‘${project.build.version}‘ />
  </bean>
  <bean id="myfilterChainProxy" class="org.springframework.security.web.FilterChainProxy">
    <security:filter-chain-map request-matcher="ant">
      <security:filter-chain pattern="/js/*.js" filters="versionFilter" />
      <security:filter-chain pattern="/api-docs" filters="swaggerFilter" />
      <security:filter-chain pattern="/api-docs/**" filters="swaggerFilter" />
      <security:filter-chain pattern="/swagger/**" filters="swaggerFilter" />
    </security:filter-chain-map>
  </bean>

4. 配置web.xml

<filter>
  <filter-name>myfilterChainProxy</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  <init-param>
    <param-name>targetFilterLifecycle</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>myfilterChainProxy</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

配置完成

参考资料:

http://www.springframework.org/schema/security/spring-security-4.0.xsd

http://docs.spring.io/autorepo/docs/spring-security/4.0.1.RELEASE/apidocs/org/springframework/security/web/FilterChainProxy.html

http://blog.csdn.net/imxiangzi/article/details/8812305

时间: 2024-08-10 02:10:50

配置DelegatingFilterProxy使用Spring管理filter的相关文章

实现一个支持正则匹配的Filter以及Spring管理Filter遇到的问题

相信很多人都会对Http的Filter的url-pattern不支持正则而烦恼吧.就比如,在web项目中Struts,当我们想要对所有的或者某一个Action进行过滤,而不过滤其他的Action,尤其是不想过滤静态资源,如果你的Struts配置了Action后缀可能会好一些,很可惜我的项目就没有设置后缀,所有没法使用url-pattern仅支持的几种规则,所以就自己实现了一个抽象类,用来支持可配置需过滤以及不需过滤的url规则,且支持正则,具体源码可见下方. import java.io.IOE

Spring管理Filter和Servlet

Spring管理filter和servlet 在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象  的创建.如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用 WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())来获得WebApplicationContext,然后调用

Spring 管理Filter和Servlet

本文转载自:http://www.open-open.com/lib/view/open1417248512252.html 在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象的创建.如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用WebApplicationContextUtils.getRequiredWebApplicationContext(getServletCo

Spring配置与第一Spring HelloWorld

林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 本文将主讲了Spring在Eclipse下的配置,并用Spring执行了第一个HelloWorld. 一.下载须要的文件 这里我们已经配置好Java的执行环境和装好Eclipse了. 下载Spring 下载地址:http://maven.springframework.org/release/org/springframework/spring/ 下载commons-logging 下载地

Spring配置及第个Spring HelloWorld

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文将主讲了Spring在Eclipse下的配置,并用Spring运行了第一个HelloWorld. 一.下载需要的文件 这里我们已经配置好Java的运行环境和装好Eclipse了. 下载Spring 下载地址:http://projects.spring.io/spring-framework/ 下载commons-logging 下载地址:http://commons.apache.or

spring管理SessionFactory中XML配置

1. 综合练习目标 2. 综合练习需求 3.模块划分 1. 综合练习目标 <1>复习 Java 基本语法 <2>熟悉掌握Java开发常用API <3>尝试建立面向对象思想 2. 综合练习需求 <1>接收用户的命令行输入 <2>以文件为基础完成数据的增删改查操作          3.模块划分 UI模块:(Java存在文本中都是以字符型式) 数据验证模块:验证用户输入是否合法 spring管理SessionFactory中XML配置,布布扣,bub

spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常

java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常 这个错误,网上说的原因一大推,反正我这个出的问题 是因为 虽然我在 spring 里把事务都配置好了,结果运行就出现这个错误,看看配置都没什么问题,都是网上 案例 照 写编码的,还是错的,结果发现是因为 我

深入探索spring技术内幕(二): 剖析spring管理Bean的原理与配置

求二叉树的宽度和深度 给定一个二叉树,获取该二叉树的宽度和深度. 例如输入 a / \ b c / \ / \ d e f g 返回3. 详细描述: 接口说明 原型: int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight) 输入参数: head 需要获取深度的二叉树头结点 输出参数(指针指向的内存区域保证有效): pulWidth 宽度 pulHeight 高度 返回值: 0 成功 1 失败

JSP中Filter中访问Spring管理的beans

@Override public void init(FilterConfig filterConfig) {  //unchecked = filterConfig.getInitParameter("unchecked").split(",");  WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContex