Java 弹簧靴2.1.4控制器警告不工作
我无法获得此异常处理工作。我的问题/答案看起来很相似,但没有一个有效。我有一个带有Java 弹簧靴2.1.4控制器警告不工作,java,spring,spring-boot,exception,Java,Spring,Spring Boot,Exception,我无法获得此异常处理工作。我的问题/答案看起来很相似,但没有一个有效。我有一个带有@ControllerAdvice的类来处理应用程序级错误处理。这不是REST应用程序。这是春靴百里香 @EnableWebMvc @ControllerAdvice public class ErrorController { @ExceptionHandler(LoginFailureException.class) public ModelAndView handleLoginFailure
@ControllerAdvice
的类来处理应用程序级错误处理。这不是REST应用程序。这是春靴百里香
@EnableWebMvc
@ControllerAdvice
public class ErrorController {
@ExceptionHandler(LoginFailureException.class)
public ModelAndView handleLoginFailureException(LoginFailureException exception) {
log.info("Handle LoginFailureException");
return getModelAndView(exception.getMessage(), "user/login");
}
// other exception handling methods
private ModelAndView getModelAndView(String message, String viewName) {
ModelAndView mav = new ModelAndView();
FlashMessage flashMessage = new FlashMessage();
flashMessage.setType("danger");
flashMessage.setMessage(message);
mav.getModel().put("flashMessage", flashMessage);
mav.setViewName(viewName);
return mav;
}
}
在我的WebSecurityConfigurerAdapter
扩展类中,我有
.failureHandler(loginFailureHandler)
而LoginFailureHandler
类如下所示:
@Component
public class LoginFailureHandler implements AuthenticationFailureHandler {
private MessageSource messageSource;
@Autowired
public LoginFailureHandler(MessageSource messageSource) {
this.messageSource = messageSource;
}
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
throw new LoginFailureException(
messageSource.getMessage("email.password.not.match", null, Locale.getDefault()));
}
}
当我尝试使用无效用户名登录时,我的应用程序崩溃并抛出此异常,并且永远无法访问ExceptionHandler方法
com.company.application.controller.exception.LoginFailureException: Email or Username is not valid
at com.company.application.security.LoginFailureHandler.onAuthenticationFailure(LoginFailureHandler.java:34) ~[classes/:na]
尝试将其添加到主spring引导应用程序中,以关闭错误Mvc的自动配置 配置
@SpringBootApplication
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
尝试将其添加到主spring引导应用程序中,以关闭错误Mvc的自动配置 配置
@SpringBootApplication
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
@ControllerAdvice
仅在从@Controller
引发异常时才会被命中,而不是@组件
由于您正在用危险/闪光消息“装饰”登录页面,因此您可能必须将
请求转发到新的“页面”(例如用户/loginerror
)上?
仅在@Controller
引发异常时才会被命中,而不是@Component
由于您正在用危险/闪光消息“装饰”登录页面,因此您可能必须将请求
转发到新的“页面”(例如用户/loginerror
)上?我想知道这是什么getModelAndView()
函数正在执行。@NavjotSingh该部分未到达,但我在问题中添加了该部分,以防您想看到它。我想知道该getModelAndView()
函数正在执行什么。@NavjotSingh该部分未到达,但我在问题中添加了该部分,以防您想看到它。这没有帮助。以前,当我点击“登录”按钮时,我被重定向到登录页面,并在控制台中看到异常,但在这个位置上,我得到“HTTP状态500–内部服务器错误”onAuthenticationFailure是AuthenticationFailureHandler中spring定义的方法,您在该方法中引发异常,但只有从控制器方法impl引发的异常到达ControllerAdviceOk,然后使用AuthenticationFailureHandler在这里对我没有帮助。我必须使用类似于.failureUrl(“/user login?error=true”)
的东西进行登录,并对应用程序的其余部分使用异常处理。这没有帮助。以前,当我点击“登录”按钮时,我被重定向到登录页面,并在控制台中看到异常,但在这个位置上,我得到“HTTP状态500–内部服务器错误”onAuthenticationFailure是AuthenticationFailureHandler中spring定义的方法,您在该方法中引发异常,但只有从控制器方法impl引发的异常到达ControllerAdviceOk,然后使用AuthenticationFailureHandler在这里对我没有帮助。我必须使用类似于.failureUrl(“/user login?error=true”)
的东西进行登录,并对应用程序的其余部分使用异常处理。在我的应用程序中,组件触发器ControllerAdvice中引发的异常通常为。你说的是什么版本?在我的应用程序中,组件触发器ControllerAdvice中通常会引发异常。你说的是什么版本?