无法为所有网址设置Spring安全性

时间:2014-11-13 06:22:11

标签: java spring spring-mvc spring-security

我创建了一个带弹簧安全性的spring mvc应用程序。我尝试使用spring security为所有url设置身份验证。

Springsecurity.xml

<http auto-config="true" use-expressions="true">
        <intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />

        <!-- access denied page -->
        <access-denied-handler error-page="/403" />
        <form-login 
            login-page="/login" 
            default-target-url="/welcome"
            authentication-failure-url="/login?error" 
            username-parameter="username"
            password-parameter="password" />
        <logout logout-success-url="/login?logout" />
        <!-- enable csrf protection -->
        <csrf />
    </http>
    <authentication-manager>
        <authentication-provider user-service-ref="userDetailsService" >
            <password-encoder hash="bcrypt" />    
        </authentication-provider>
    </authentication-manager>

当我向/**提供intercept-url时,页面无法加载。它会超时。 但是当给/admin拦截网址时,它完美无缺。为什么会这样?

1 个答案:

答案 0 :(得分:2)

您的所有请求的拦截模式都可以,但您需要为登录页面添加例外,请尝试添加

  <http security="none" pattern="/login"/>
关于评论的

更新

上述方法完全关闭了给定URL的Spring安全性。当您使用CSFR时,这意味着Spring安全过滤器也应该关注此URL,但不是为了进行身份验证,而是为了包含可以防止会话固定攻击的不可预测令牌。无论如何,这是一种使用spring安全性处理URL的方法,而不会提示进行身份验证。请使用以下

,而不是使用上述内容
<intercept-url pattern="/login" access="isAnonymous()"/>

里面的

<http auto-config="true" use-expressions="true">
        <intercept-url pattern="/login" access="isAnonymous()"/>
        <intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />
        ...