Spring security 将每个http块映射到特定的身份验证提供程序
我想根据用户的上下文路径来确定Spring安全配置的基础。如果用户与url冲突,我希望将他们指向特定的身份验证提供商。如果他们来了,我想让他们到另一个认证提供商。这些url路径是基于REST的web服务调用,因此它们是无状态的,并且不是来自表单。当前,所有身份验证提供程序都将执行。解决这种情况的最佳方法是什么?我使用的是SpringSecurity 3.1.0.M1Spring security 将每个http块映射到特定的身份验证提供程序,spring-security,Spring Security,我想根据用户的上下文路径来确定Spring安全配置的基础。如果用户与url冲突,我希望将他们指向特定的身份验证提供商。如果他们来了,我想让他们到另一个认证提供商。这些url路径是基于REST的web服务调用,因此它们是无状态的,并且不是来自表单。当前,所有身份验证提供程序都将执行。解决这种情况的最佳方法是什么?我使用的是SpringSecurity 3.1.0.M1 <http pattern="/path1/**" create-session="stateless">
<http pattern="/path1/**" create-session="stateless">
<intercept-url pattern="/**" access="ROLE_USER,ROLE_VAR,ROLE_ADMIN" />
<http-basic />
</http>
<http pattern="/path2/**" create-session="stateless">
<intercept-url pattern="/**" access="ROLE_USER,ROLE_VAR,ROLE_ADMIN" />
<http-basic />
</http>
这对我很有用:
<security:authentication-manager alias="basicAuthenticationManager">
<security:authentication-provider user-service-ref="accountService">
<security:password-encoder hash="sha"/>
</security:authentication-provider>
<security:authentication-provider user-service-ref="accountService"/>
</security:authentication-manager>
<bean id="basicProcessingFilter" class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
<property name="authenticationManager">
<ref bean="basicAuthenticationManager" />
</property>
<property name="authenticationEntryPoint">
<ref bean="basicProcessingEntryPoint" />
</property>
</bean>
<bean id="basicProcessingEntryPoint"
class="com.yourpackage.web.util.CustomBasicAuthenticationEntryPoint">
<property name="realmName" value="yourRealm" />
</bean>
<!-- Stateless RESTful service using Basic authentication -->
<security:http pattern="/rest/**" create-session="stateless" entry-point-ref="basicProcessingEntryPoint">
<security:custom-filter ref="basicProcessingFilter" position="BASIC_AUTH_FILTER" />
<security:intercept-url pattern="/rest/new" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<security:intercept-url pattern="/rest/**" access="ROLE_USER" />
</security:http>
<!-- Additional filter chain for normal users, matching all other requests -->
<security:http use-expressions="true">
<security:intercept-url pattern="/index.jsp" access="permitAll" />
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<security:form-login login-page="/signin"
authentication-failure-url="/signin?signin_error=1"
default-target-url="/"
always-use-default-target="true"/>
<security:logout />
</security:http>
我实现了身份验证入口点,因为我需要在某些情况下发送一些特殊的错误代码,但您不需要这样做。您可以在每个http块中定义一个身份验证管理器引用:
<http pattern="/api/**" authentication-manager-ref="apiAccess">
...
</http>
<http auto-config = "true" authentication-manager-ref="webAccess">
...
</http>
<!-- Web authentication manager -->
<authentication-manager id="webAccess">
<authentication-provider
user-service-ref="userService">
</authentication-provider>
</authentication-manager>
<!-- API authentication manager -->
<authentication-manager id="apiAccess">
<authentication-provider
user-service-ref="developerService">
</authentication-provider>
</authentication-manager>
...
...
此功能已在Spring Security 3.1中添加。感谢Martin Castell的回复。您可能需要澄清的一点是,如何将rest http块映射到一个身份验证提供程序,同时将表单http块映射到另一个身份验证提供程序。这就是我试图实现的目标,在你的例子中我并不清楚。谢谢请注意,对于
身份验证管理器
,使用的是id
,而不是别名
。如果您使用alias
,Spring Security可能会选择错误的身份验证管理器。