gpt4 book ai didi

java - 无法为所有 url 设置 Spring 安全性

转载 作者:行者123 更新时间:2023-12-02 05:16:30 26 4
gpt4 key购买 nike

我使用 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>

当我向 /** 提供拦截 URL 时,页面不会加载。它会超时。但是,当将拦截网址提供给 /admin 时,它可以完美地工作。为什么会出现这种情况?

最佳答案

您的所有请求的拦截模式都可以,但您需要为登录页面添加一个异常(exception),请尝试添加

  <http security="none" pattern="/login"/>

关于评论的更新

上述方法完全关闭给定 URL 的 Spring 安全性。当您使用 CSFR 时,这意味着 Spring Security 过滤器也应该关注此 URL,但不是为了身份验证,而是为了包含可防止 session 固定攻击的不可预测的 token 。无论如何,这里有一种使用 spring security 处理 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')" />
...

关于java - 无法为所有 url 设置 Spring 安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26902584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com