java - Spring MVC 视图控制器重定向
问题描述
是否可以不重定向到没有查询参数的视图?我有一个login
视图,我想login?error
在登录失败时重定向到浏览器中的 url。我有我自己的AuthenticationFailureHandler
:
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);
}
}
配置在WebSecurityConfigurerAdapter
:
@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?error
在用户中被重定向到login
忽略?error
参数。
这是我的 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);
}
}
推荐阅读
- r - 如何在黄土和样条回归中添加协变量,然后使用 ggplot2 在 r 中绘制它
- vue.js - vue-router : 创建一个路由器链接
- javascript - 在 Office 脚本中获取数组中元素的值
- machine-learning - 100 个离散值的多类分类的线性回归
- python - 如果现有变量位于数据框中的两个值之间,则创建等于 1 的新变量
- sql - 在 INSERT 之后一个接一个地执行 SQL Server 2012 UPDATE
- azure - 与数据库安装在同一虚拟机中时,如何保护 (SSRS) 报告服务实例?
- angular - 使用不同的环境运行 Angular Universal 应用程序
- spring-boot - 如何搜索具有加密列的数据库
- azure-devops - Azure DevOps / Trigger 管道,提供手动运行的依赖项