配置DelegatingFilterProxy使用Spring管理filter chain

項目環境:JDK7 + Maven3.04html

0. 項目使用springmvc做爲controller層web

1. 引入spring-securityspring

<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中添加namespaceapi

<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配置mvc

<bean id='swaggerFilter' class="com.hiv.web.swagger.SwaggerFilter">
    <constructor-arg value='${hiv.swagger.enabled}' />
  </bean>
  <bean id='versionFilter' class="com.hiv.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.xmlapp

<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>

配置完成ui

參考資料:url

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

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

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

相關文章
相關標籤/搜索