我正在学习本教程: http://www.mkyong.com/spring-security/spring-security-hello-world-example/
在spring-security-xml
<http auto-config="true">
<intercept-url pattern="/welcome*" access="ROLE_USER" />
</http>
并且在 web.xml 中,我们必须定义实际的过滤器
<!-- Spring Security -->
<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>
所以我不明白,我们正在将拦截映射到 2 个位置的 2 个 url。到 /welcome*
和 /*
。为什么我们需要这两者?我在这里遗漏了什么吗?
最佳答案
DelegatingFilterProxy
不是 Spring Security 类。它来自 Spring Web 包。
Proxy for a standard Servlet 2.3 Filter, delegating to a Spring-managed bean that implements the Filter interface. Supports a "targetBeanName" filter init-param in web.xml, specifying the name of the target bean in the Spring application context.
当你使用
<http auto-config="true">
</http>
Spring Security 创建(隐式)名称为 springSecurityFilterChain
的 bean (这就是为什么你的 <filter-name>springSecurityFilterChain</filter-name>
中有 web.xml
)并且所有请求( /*
)都由它处理(由 Spring Security)。
然后配置 Spring Security 并为其提供更具体的 URL (/*welcome
)。
<intercept-url pattern="/welcome*" access="ROLE_USER" />
这就像在说:
- 所有 URL 请求(
/*
)都应该由 Spring Security 调查 - 当 URL 匹配
/welcome*
时校长应该有ROLE_USER
角色。
如果您的应用程序需要更高级的安全处理,您可以自己创建过滤器链 bean 并手动配置所有过滤器。
例子:
<!-- Filter Chain -->
<bean id="springSecurityFilterChain"
class="org.springframework.security.web.FilterChainProxy">
<constructor-arg>
<list>
<sec:filter-chain pattern="/favicon.ico"
filters="none"/>
<sec:filter-chain pattern="/img/**"
filters="none"/>
<sec:filter-chain pattern="/**"
filters="bannedIPsFilter, <!-- custom filter -->
channelProcessingFilter,
securityContextPersistenceFilter,
concurrentSessionFilter,
logoutFilter,
secondAuthenticationFilter, <!-- custom filter -->
openIDAuthenticationFilter,
usernamePasswordAuthenticationFilter,
anonymousAuthenticationFilter,
captchaFilter, <!-- custom filter -->
sessionManagementFilter,
exceptionTranslationFilter,
filterSecurityInterceptor,
switchUserProcessingFilter"
/>
</list>
</constructor-arg>
</bean>
关于java - Spring Security 过滤器有多个 URL 拦截映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13797207/