【发布时间】:2011-11-25 09:56:57
【问题描述】:
在我的 Spring MVC Web 应用程序中,某些区域只有具有足够权限的用户才能访问。我需要能够允许用户以其他用户身份登录才能使用这些页面(有点像覆盖),而不仅仅是“拒绝访问”消息。
如何使用 Spring Security 做到这一点?
这是我想要的流程,更详细一点:
- 用户 A 从外部应用程序进入页面 X 并通过标头进行身份验证
- 用户 A 无权使用页面 X,因此被带到登录屏幕并显示一条消息,指示他们必须以具有足够权限的用户身份登录才能使用此页面
- 用户 B 登录,并拥有足够的权限,并被带到页面 X。
注意:页面 X 有一个大而长的查询字符串需要保留。
如何使用 Spring Security 做到这一点?
这是我的 Spring Security 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<debug />
<global-method-security pre-post-annotations="enabled">
<!-- AspectJ pointcut expression that locates our "post" method and applies
security that way <protect-pointcut expression="execution(* bigbank.*Service.post*(..))"
access="ROLE_TELLER"/> -->
</global-method-security>
<!-- Allow anyone to get the static resources and the login page by not applying the security filter chain -->
<http pattern="/resources/**" security="none" />
<http pattern="/css/**" security="none" />
<http pattern="/img/**" security="none" />
<http pattern="/js/**" security="none" />
<!-- Lock everything down -->
<http
auto-config="true"
use-expressions="true"
disable-url-rewriting="true">
<!-- Define the URL access rules -->
<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/about/**" access="permitAll and !hasRole('blocked')" />
<intercept-url pattern="/users/**" access="hasRole('user')" />
<intercept-url pattern="/reviews/new**" access="hasRole('reviewer')" />
<intercept-url pattern="/**" access="hasRole('user')" />
<form-login
login-page="/login" />
<logout logout-url="/logout" />
<access-denied-handler error-page="/login?reason=accessDenied"/>
<!-- Limit the number of sessions a user can have to only 1 -->
<session-management>
<concurrency-control max-sessions="1" />
</session-management>
</http>
<authentication-manager>
<authentication-provider ref="adAuthenticationProvider" />
<authentication-provider>
<user-service>
<user name="superadmin" password="superadminpassword" authorities="user" />
</user-service>
</authentication-provider>
</authentication-manager>
<beans:bean id="adAuthenticationProvider" class="[REDACTED Package].NestedGroupActiveDirectoryLdapAuthenticationProvider">
<beans:constructor-arg value="[REDACTED FQDN]" />
<beans:constructor-arg value="[REDACTED LDAP URL]" />
<beans:property name="convertSubErrorCodesToExceptions" value="true" />
<beans:property name="[REDACTED Group Sub-Tree DN]" />
<beans:property name="userDetailsContextMapper" ref="peerReviewLdapUserDetailsMapper" />
</beans:bean>
<beans:bean id="peerReviewLdapUserDetailsMapper" class="[REDACTED Package].PeerReviewLdapUserDetailsMapper">
<beans:constructor-arg ref="UserDAO" />
</beans:bean>
</beans:beans>
我正在使用 Spring Security 3.1 Active Directory 连接功能的略微修改版本。修改只是加载用户组的所有组,包括通过组嵌套到达的组,而不仅仅是用户直接属于的组。我还使用了一个自定义用户对象,其中嵌入了我的应用程序的用户对象,以及一个执行正常 LDAP 映射的自定义 LDAP 映射器,然后添加我的用户。
有一个尚未实现的特殊身份验证方案,其中用户基于从外部应用程序(或通过 Kerberos)以单点登录方式传递的用户名进行身份验证。
【问题讨论】:
-
不同的用例,但与stackoverflow.com/questions/6337825/…有关。
-
@BarendGarvelink - 是的,我看到了,但我不想暂时“假装”成为另一个用户。我要做的是让他们有效地重新验证为不同的用户(本质上是注销登录),然后继续访问他们试图访问但无法访问的页面。
标签: java spring spring-mvc spring-security