如何使用spring安全性实现ResourceUrlEncodingFilter

时间:2018-04-16 11:11:56

标签: spring spring-security

我正在使用spring 3.2.3.RELEASE和Spring Security 3.1.4.RELEASE的应用程序。我想在我的应用程序中实现静态资源版本控制。我确实用spring 4.3.11实现了这个功能,并引用了http://www.baeldung.com/cachable-static-assets-with-spring-mvc,即工作正常。现在,当我要使用我的旧应用程序实现它时,即不工作。我正在为您提供代码片段 在我的web.xml中

<filter>
        <filter-name>resourceUrlEncodingFilter</filter-name>
        <filter-class>
            org.springframework.web.servlet.resource.ResourceUrlEncodingFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>resourceUrlEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>  

在spring.xml中

<security:http auto-config="true" pattern="/**" use-expressions="true">
        <!-- Login pages -->
        <security:form-login login-page="/login" 
                             default-target-url="/home/" 
                             login-processing-url="/j_spring_security_check" 
                             authentication-failure-url="/login?error" />
        <security:logout logout-success-url="/home/"/>
        <!-- Security zones -->
        <security:intercept-url pattern="/favicon.ico" access="hasRole(ROLE_ANONYMOUS)" />
        <security:intercept-url pattern="/login" access="permitAll"/>
        <security:intercept-url pattern="/resources/**" access="permitAll"/>
        <security:intercept-url pattern="/admin/**" access="hasRole('ROLE_SUPER_USER')" />
        <security:intercept-url pattern="/**" access="isAuthenticated()" />
      </security:http>

我们也在调度程序servlet中使用缓存过滤器

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/resources/**" />
        <bean id="responseCachingFilter" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
        <property name="cacheSeconds" value="0" />
        <property name="useExpiresHeader" value="true" />
        <property name="cacheMappings">
            <props>
            <prop key="/resources/**">21600</prop>
            </props>
        </property>
        </bean>
</mvc:interceptors>
    </mvc:interceptor>

和版本控制代码

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/")
                .setCacheControl(CacheControl.maxAge(6, TimeUnit.HOURS))
                .resourceChain(false)
                .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
                .addTransformer(new CssLinkResourceTransformer());
    }

但我的版本控制代码无效。请建议我需要更改代码或其他需要实现的方法。

提前致谢

0 个答案:

没有答案