Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无重定向的Spring安全性_Java_Spring_Spring Security - Fatal编程技术网

Java 无重定向的Spring安全性

Java 无重定向的Spring安全性,java,spring,spring-security,Java,Spring,Spring Security,我有一个Spring安全实现,它是无状态的,使用基于令牌的身份验证。我的大部分逻辑都存在于扩展AbstractAuthenticationProcessingFilter的类中。我的问题是,身份验证成功后,AbstractAuthenticationProcessingFilter执行302重定向,这是我不希望看到的。我只想完成最初的请求。如何解决这个问题?我能够通过重写成功和失败处理程序,使spring security公开的“login”rest方法返回“200OK”,而不是“302重定向”

我有一个Spring安全实现,它是无状态的,使用基于令牌的身份验证。我的大部分逻辑都存在于扩展AbstractAuthenticationProcessingFilter的类中。我的问题是,身份验证成功后,AbstractAuthenticationProcessingFilter执行302重定向,这是我不希望看到的。我只想完成最初的请求。如何解决这个问题?

我能够通过重写成功和失败处理程序,使spring security公开的“login”rest方法返回“200OK”,而不是“302重定向”。 下面的代码显示了如何实现相同的功能

        //configure http by using the successHandler 
        //and failure handler methods
        http.
            formLogin()
                .loginPage("/authentication/login")
                .loginProcessingUrl("/authentication/processLogin")
                .successHandler(successHandler())
                .failureHandler(failureHandler())
            .and()
            ......



    private AuthenticationFailureHandler failureHandler() {
        return new SimpleUrlAuthenticationFailureHandler() {
            public void onAuthenticationFailure(HttpServletRequest request,
                    HttpServletResponse response, AuthenticationException exception)
                    throws IOException, ServletException {
                response.setContentType("text/html;charset=UTF-8");
                response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication Failed. Wrong username or password or both");
            }
        };
    }


    private AuthenticationSuccessHandler successHandler() {
        return new SimpleUrlAuthenticationSuccessHandler() {
            public void onAuthenticationSuccess(HttpServletRequest request,
                    HttpServletResponse response, Authentication authentication)
                    throws IOException, ServletException {
                response.setContentType("text/html;charset=UTF-8");
                HttpSession session = request.getSession(false);
                session.setMaxInactiveInterval(60*180);
                response.getWriter().println("LoginSuccessful");
            }
        };
    }