Spring Security:如果身份验证失败,重定向到登录页面

9
我们有两种登录方式:
  • 用户名和密码通过请求头由另一个应用程序发送。IT进行检查,如果用户名和密码正确,则进入[A为此编写了自定义过滤器]
  • 如果请求头中没有用户名和密码,则呈现登录屏幕。
  • 当请求头中存在用户名和密码但出现错误时,会显示一个HTTP状态401-身份验证失败:错误的凭据页面。
    如何在身份验证失败时显示登录页面?
    下面是security.xml中的代码。
        <http auto-config="true" use-expressions="true">
                 <access-denied-handler error-page="/login.jsp"/>
                <intercept-url pattern="/*Login*" access="hasRole('ROLE_ANONYMOUS')"/>
                <intercept-url pattern="/*" access="hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')"/>
                <custom-filter ref="requestHeaderFilter" before="FORM_LOGIN_FILTER"/>
                <form-login login-page="/login.jsp"/>
    
        </http>
    

    如果您需要更多信息,请告诉我。

    编辑:在我的应用程序中添加RequestHeader过滤器的代码

    public class RequestHeaderProcessingFilter extends AbstractAuthenticationProcessingFilter{
    
    private String usernameHeader = "j_username";
    private String passwordHeader = "j_password";
    
    
    protected RequestHeaderProcessingFilter() {
        super("/login_direct");
     }
    
    //getters and setters
    
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        String username = request.getHeader(usernameHeader);
        String password = request.getHeader(passwordHeader);
    
         SignedUsernamePasswordAuthenticationToken authRequest =
            new SignedUsernamePasswordAuthenticationToken(username, password);
    
          return this.getAuthenticationManager().authenticate(authRequest); 
    }
    

    }

    3个回答

    2
    为了在认证失败时显示登录页面,您应该在 <access-denied-handler error-page="/login.jsp"/><intercept-url pattern="/*Login*" access="hasRole('ROLE_ANONYMOUS')"/> 中使用相同的URL。
    例如:
    <global-method-security secured-annotations="enabled" />
    
    <http auto-config="true" access-denied-page="/app/sesiones/procesarLogin"> 
        <logout logout-success-url="/app/sesiones/login" />
        <form-login 
            authentication-failure-url="/app/sesiones/login?error=true"
            login-page="/app/sesiones/login" default-target-url="/app/sesiones/procesarLogin" />
        <intercept-url pattern="/app/privados/*" access="ROLE_USER" />
    </http>
    

    在这个例子中,用户在注销后也被重定向到登录页面。 /procesarLogin 是一个将用户发送到 login.jsp 页面的方法。

    1

    当身份验证失败时,默认的行为是显示HTTP状态码401。如果使用SimpleUrlAuthenticationFailureHandler来处理失败(如AbstractAuthenticationProcessingFilter所做的那样),则可以通过将defaultFailureUrl设置为/login.jsp来覆盖此行为。

    或者,还可以定义适当操作的自定义错误处理程序。


    嗨Raghuram,谢谢。我在哪里配置defaultFailureUrl?我必须将其插入到上面提供的代码中吗? - Vinoth Kumar C M
    @vinoth。这取决于requestHeaderFilter实现的方式。答案中提到的类展示了一种实现方式。 - Raghuram
    嗨,我已经添加了requestHeaderFilter的实现。我们需要对此进行更改吗? - Vinoth Kumar C M

    0

    你也可以尝试这个:

    @Configuration
    @EnableWebSecurity
    public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        .
        .
        .
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            .
            .
            .
    
            http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login").invalidateHttpSession(true).deleteCookies("JSESSIONID").permitAll();
        }
    }
    

    在configure方法中,您可以添加您的过滤器管道和其他相关内容。还可以配置角色访问特定的端点,就像您所看到的那样https://docs.spring.io/spring-security/site/docs/current/guides/html5/form-javaconfig.html

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接