项目环境: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.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.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

 posted on 2015-07-02 10:54  hiv  阅读(5037)  评论(0编辑  收藏  举报