Java SpringMVC视图控制器重定向

Java SpringMVC视图控制器重定向,java,spring,spring-boot,spring-mvc,spring-security,Java,Spring,Spring Boot,Spring Mvc,Spring Security,是否可以不重定向到没有查询参数的视图?我有一个login视图,当登录失败时,我想重定向到浏览器中的login?errorurl。我有自己的身份验证失败处理程序: public class SomeCustomHandler implements AuthenticationFailureHandler { private final SimpleUrlAuthenticationFailureHandler authenticationFailureHandler = new SimpleU

是否可以不重定向到没有查询参数的视图?我有一个
login
视图,当登录失败时,我想重定向到浏览器中的
login?error
url。我有自己的
身份验证失败处理程序

public class SomeCustomHandler implements AuthenticationFailureHandler {
  private final SimpleUrlAuthenticationFailureHandler authenticationFailureHandler = new SimpleUrlAuthenticationFailureHandler("/login?error");

  @Override
  public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    authenticationFailureHandler.onAuthenticationFailure(request, response, exception);
  }
}
@Override
protected void configure(HttpSecurity http) throws Exception {
  http
      .requestMatchers()
      .antMatchers("/login", "/oauth/authorize")
    .and()
      .authorizeRequests()
      .anyRequest().authenticated()
    .and()
      .formLogin()
      .loginPage("/login")
      .failureHandler(new SomeCustomHandler())
    .permitAll();
}
websecurityConfigureAdapter
中配置:

public class SomeCustomHandler implements AuthenticationFailureHandler {
  private final SimpleUrlAuthenticationFailureHandler authenticationFailureHandler = new SimpleUrlAuthenticationFailureHandler("/login?error");

  @Override
  public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    authenticationFailureHandler.onAuthenticationFailure(request, response, exception);
  }
}
@Override
protected void configure(HttpSecurity http) throws Exception {
  http
      .requestMatchers()
      .antMatchers("/login", "/oauth/authorize")
    .and()
      .authorizeRequests()
      .anyRequest().authenticated()
    .and()
      .formLogin()
      .loginPage("/login")
      .failureHandler(new SomeCustomHandler())
    .permitAll();
}
但当登录失败时,返回的
login?错误
将在用户中重定向到
login
忽略
错误
参数

这是我的MvcWebConfigurer:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
  registry.addViewController("/login/**").setViewName("login"); 
}  

我想你可以试着这样做:

public class SomeCustomHandler implements AuthenticationFailureHandler {
    private final SimpleUrlAuthenticationFailureHandler authenticationFailureHandler = new SimpleUrlAuthenticationFailureHandler("/login?error");

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        if(request.getPathInfo().startsWith("/login") && !request.getParameterMap().isEmpty()) {
            authenticationFailureHandler.setUseForward(true);
        }
        authenticationFailureHandler.onAuthenticationFailure(request, response, exception);
    }
}

请更改注册表.addViewController(“/login/**”).setViewName(“login”)到注册表.addViewController(“/login”).setViewName(“login”)并重试,不幸的是,它仍然是一样的。尝试添加登录视图?错误
registry.addViewController(“/login?error”).setViewName(“errorpage”)
还添加
.antMatchers(“/login?error”).permitAll()
配置(HttpSecurity http)中
我希望有一个
/login
页面,因为我会有多个错误原因,例如
?error=bad credentials
?error=locked account
,并且
/login
页面上会有一个
页面,根据作为查询参数传递的错误类型显示错误消息。创建单独的
errorpage
将导致为每种错误类型创建新页面,这些页面之间的唯一区别是错误消息。