Spring LogoutHandler:sendRedirect状态为302

时间:2016-02-23 11:08:54

标签: java spring spring-security

我试图实现spring logoutSuccessHandler。  当用户注销时,我将调用重定向到具有状态302的targetURL。这是我实现此代码的代码:

  public class LogoutHandler extends SimpleUrlLogoutSuccessHandler {

public LogoutHandler(AuthenticationContext context) {
    this.context = context;
}

@Override
protected void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
        throws IOException, ServletException {
    String targetUrl = "mytarget";

   authentication.setAuthenticated(false);
   response.setStatus(HttpServletResponse.SC_FOUND);

    response.setHeader("Location", targetUrl);
    response.setContentType(MediaType.TEXT_PLAIN_VALUE);
    response.getWriter().write(targetUrl);
    response.getWriter().flush();
    response.getWriter().close();

 }

}

但是此代码不起作用,我已取消此/ logout webservice的状态。 我们怎么能实现lougoutHandler?

2 个答案:

答案 0 :(得分:1)

您可以实施自定义LogoutSuccessHandler

static class RedirectLogoutSuccessHandler implements LogoutSuccessHandler {

    private String logoutUrl;
    private int statusCode;

    public RedirectLogoutSuccessHandler(String logoutUrl, int statusCode) {
        this.logoutUrl = logoutUrl;
        this.statusCode = statusCode;
    }

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
                                Authentication authentication) throws IOException, ServletException {
        response.setStatus(statusCode);
        response.setHeader("Location", logoutUrl);
    }
}

使用以下命令进行配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .logout()
            .logoutSuccessHandler(new RedirectLogoutSuccessHandler("http://www.google.com", HttpServletResponse.SC_MOVED_TEMPORARILY));
}

或者@ M.Deinum建议使用SimpleUrlLogoutSuccessHandler并注入自定义RedirectStrategy

static class CustomRedirectStrategy implements RedirectStrategy {

    @Override
    public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
        response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
        response.setHeader("Location", url);
    }
}

并配置它:

@Override
protected void configure(HttpSecurity http) throws Exception {
    SimpleUrlLogoutSuccessHandler logoutSuccessHandler = new SimpleUrlLogoutSuccessHandler();
    logoutSuccessHandler.setDefaultTargetUrl("http://www.google.com");
    logoutSuccessHandler.setRedirectStrategy(new CustomRedirectStrategy());
    //...
    http
        .logout()
            .logoutSuccessHandler(logoutSuccessHandler);
}

答案 1 :(得分:0)

您可以使用Spring引导来指定注销成功URL,如下所示: -

public class RelayCommand : ICommand
{
    private Action _handler;

    public RelayCommand(Action handler)
    {
        _handler = handler;
    }

    public RelayCommand(Action handler, Func<bool> p) : this(handler)
    {
        this.p = p;
    }

    private bool _isEnabled;
    private Func<bool> p;

    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            if (value != _isEnabled)
            {
                _isEnabled = value;
                if (CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }
    }

    public bool CanExecute(object parameter)
    {
        //return IsEnabled;
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _handler();
    }
}