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

时间:2011-03-28 12:20:35

标签: java authentication spring-security

我们有两种登录方式。

    

  • 用户名和密码由请求标头中的其他应用程序发送。检查IT,如果用户名和密码正确,则进入。[为此编写自定义过滤器]
  •      
  • 如果请求标头中不存在用户名和密码,则会显示登录屏幕。
  • 如果请求标头中存在用户名和密码,如果错误,我会看到 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 个答案:

    答案 0 :(得分:2)

    要在身份验证失败时显示登录页面,您应该在<access-denied-handler error-page="/login.jsp"/><intercept-url pattern="/*Login*" access="hasRole('ROLE_ANONYMOUS')"/>

    中使用相同的网址

    例如:

    <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是一个发送用户lo login.jsp页面的方法。

    答案 1 :(得分:0)

    身份验证失败时的默认行为是显示HTTP状态401.如果SimpleUrlAuthenticationFailureHandler用于处理失败(由AbstractAuthenticationProcessingFilter完成,则可以通过设置{{1}来覆盖此行为} defaultFailureUrl

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

    答案 2 :(得分: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方法中,您可以添加您的filterpipeline和其他相关内容。 您还可以配置对特定端点的角色访问,如您所见 https://docs.spring.io/spring-security/site/docs/current/guides/html5/form-javaconfig.html

    相关问题