Java 重定向到上一页Spring Security

Java 重定向到上一页Spring Security,java,spring,spring-security,Java,Spring,Spring Security,我有一个web应用程序,在单独的页面上有一个登录选项。然而,当我登录时,我会被发送回主页,而不是返回到我所在的页面。我使用的是Spring Security提供的默认登录/注销页面 以下是我的HttpSecurity配置: @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/*")

我有一个web应用程序,在单独的页面上有一个登录选项。然而,当我登录时,我会被发送回主页,而不是返回到我所在的页面。我使用的是Spring Security提供的默认登录/注销页面

以下是我的HttpSecurity配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
    .antMatchers("/api/*").hasRole("Admin")
    .antMatchers("/manager").hasAnyRole("Admin", "Manager")
    .antMatchers("/sales").hasAnyRole("Admin", "Manager", "Sales")
    .antMatchers("/checkout").authenticated()
    .and().formLogin().permitAll()
    .and().logout().permitAll()
    .logoutSuccessUrl("/");
}

本文介绍了如何实现需求的主要思想,其中包括源代码示例。您只需更改此方法

protected void handle(final HttpServletRequest request, final HttpServletResponse response, final 

Authentication authentication) throws IOException {

    if (response.isCommitted()) {
        logger.debug("Response has already been committed. Unable to redirect to " + request.getRequestURI());
        return;
    }
    redirectStrategy.sendRedirect(request, response, request.getRequestURI());
}`

MySimpleUrlAuthenticationSuccessHandler
class

中,在登录后执行此代码后,我将转到“project/project/login”,而不是返回到“project/previousPath”。