cde*_*zaq 6 java spring spring-mvc spring-security
在我的Spring MVC Web应用程序中,只有具有足够权限的用户才能访问某些区域.我需要能够允许用户以不同的用户身份登录才能使用这些页面(有点像覆盖),而不仅仅是"拒绝访问"消息.
如何使用Spring Security执行此操作?
这是我期待的流程,更详细一点:
注意:页面X有一个需要保留的大而长的查询字符串.
如何使用Spring Security执行此操作?
这是我的spring安全配置文件:
<?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>
Run Code Online (Sandbox Code Playgroud)
我正在使用Spring Security 3.1 Active Directory连接功能的略微修改版本.修改只是加载所有用户组,包括通过组嵌套到达的组,而不仅仅是用户直接成员的组.我还使用自定义用户对象,其中嵌入了我的应用程序的User对象,以及执行常规LDAP映射的自定义LDAP映射器,然后添加到我的用户中.
有一种特殊的身份验证方案尚未实现,其中用户根据以单点登录方式从外部应用程序(或通过Kerberos)传递的用户名进行身份验证.
如何检查角色?
如果您在安全上下文中定义它们,如下所示:
<intercept-url pattern="/adminStuff.html**" access="hasRole('ROLE_ADMIN')" />
Run Code Online (Sandbox Code Playgroud)
defaultFailureUrl您可以设置SimpleUrlAuthenticationFailureHandler,当权限较低的用户尝试访问安全 URL 时,FaliureHandler应该将您重定向到defaultFailureUrl可能是您的登录页面的 。
您可以FaliureHandler在过滤器中的该FORM_LOGIN_FILTER位置注入 a 。
<bean id="myFaliureHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<property name="defaultFailureUrl" value="http://yourdomain.com/your-login.html"/>
</bean>
<bean id="myFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationFailureHandler" ref="myFaliureHandler"/>
</bean>
<http>
<custom-filter position="FORM_LOGIN_FILTER" ref="myFilter" />
</http>
Run Code Online (Sandbox Code Playgroud)
在评论中回答1)。
考虑到您的命名空间配置,这比我想象的要多一些工作。
您需要做的是删除<form-login>定义并添加一个“自定义” UsernamePasswordAuthenticationFilter(这是处理元素的过滤器<form-login>)。
您还需要删除<access-denied-handler>.
所以你的配置看起来像这样:
<bean id="myFaliureHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<property name="defaultFailureUrl" value="http://yourdomain.com/your-login.html"/>
</bean>
<bean id="myFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationFailureHandler" ref="myFaliureHandler"/>
<!-- there are more required properties, but you can read about them in the docs -->
</bean>
<bean id="loginUrlAuthenticationEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<property name="loginFormUrl" value="/login"/>
</bean>
<http entry-point-ref="authenticationEntryPoint" auto-config="false">
<!-- your other http config goes here, just omit the form-login element and the access denied handler -->
<custom-filter position="FORM_LOGIN_FILTER" ref="myFilter" />
</http>
Run Code Online (Sandbox Code Playgroud)
如果您还没有的话,通常还可以查看有关自定义过滤器的 spring 文档。目前,我们在我当前的公司中使用此配置,如果用户在页面上没有所需的权限,则强制用户重新登录。
| 归档时间: |
|
| 查看次数: |
2930 次 |
| 最近记录: |