【问题标题】:web.xml to WebSecurityConfigurerAdapterweb.xml 到 WebSecurityConfigurerAdapter
【发布时间】:2014-06-26 14:23:16
【问题描述】:

我正在使用 WebSecurityConfigurerAdapter,我想知道是否有办法配置 WebSecurityConfigurerAdapter 中的错误处理程序,就像它曾经在 web.xml 中一样 我想捕获我所有的 UsernameNotFoundException/401 并处理它。

示例:

<error-page>
<error-code>401</error-code>
<location>/SngErrorHandler</location>
</error-page>

<error-page>
<exception-type>org.springframework.security.core.userdetails.UsernameNotFoundException</exception-type>
<location>/SngErrorHandler</location>
</error-page> 

【问题讨论】:

    标签: java spring jakarta-ee servlets


    【解决方案1】:

    根据AbstractAuthenticationFilterConfigurer 的javadoc(对于Spring-Security 3.2.x),Spring security 通常会自动为您重定向到“/login?error”

    如果您想重定向到另一个页面并且您正在使用登录表单身份验证,您可以通过在您的 WebSecurityConfigurerAdapter 的 configure(HttpSecurity http) 方法中添加 http.formLogin().failureUrl("/SngErrorHandler") 来实现。

    如果您使用其他身份验证方法,则必须对此进行调整。

    【讨论】:

    • 嗨,我的 SngErrorHandler 应该是什么样子?
    • @orikoko 传统上,在 web.xml 中,错误页面是简单的 HTML 页面,所以我想这就是你想要的。在我展示的方法中,SngErrorHandler 可以是任何 URL(但如果无需身份验证即可访问它会更好......)。如果这不是您所需要的,您应该编辑您的问题以添加任何精确度。
    • 嗨,在 web.xml 中,SngErrorHandler 不是 html,这是一个生成响应(字符串)的 servlet 类。
    猜你喜欢
    • 2022-08-12
    • 2023-03-25
    • 2015-08-05
    • 2021-12-04
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    相关资源
    最近更新 更多