【问题标题】:How to do correct redirection with Spring MVC?如何使用 Spring MVC 进行正确的重定向?
【发布时间】:2021-03-06 18:28:29
【问题描述】:

登录 .jsp 页面将表单验证数据发送到 AuthenticationController:

@PostMapping("/authentication")
public String auth(HttpServletRequest request, HttpServletResponse response, @Valid @ModelAttribute("loginUser") LoginUser loginUser) throws ServletException {
   request.login(loginUser.getLogin(), loginUser.getPassword());
   SavedRequest savedRequest = requestCache.getRequest(request, response);
   if (savedRequest != null) {
       if (request.isUserInRole("ROLE_ADMIN") || request.isUserInRole("ROLE_MODERATOR")) {
           return "redirect:/admin";
       } else {
            return "redirect:/";
       }
   } else {
       return "redirect:/";
   }
}

HomeController 代码:

@GetMapping("/")
public String homePage(Model model) {
    List<Product> topList = productService.getTop();
    if (!topList.isEmpty()) {
        deleteBrandName(topList);
    }
    model.addAttribute("topList", topList);
    return "web/index";
}

HttpConfigure 代码:

.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login/auth")
.defaultSuccessUrl("/", true)
.failureUrl("/login?error")
.usernameParameter("login")
.passwordParameter("password")
.and()
...
.csrf().disable()

成功登录后,我有这个:

AOP 日志是:

2021-02-11 22:32:16 INFO : -------------------- Login -----------------------
2021-02-11 22:32:16 INFO : USER "test" entered.
2021-02-11 22:32:16 INFO : --------------------------------------------------

但我总是有登录页面

如何进行正确的重定向?

【问题讨论】:

    标签: java spring-mvc spring-security


    【解决方案1】:

    解决了!

    在表单标签中,添加如下参数。

    redirect="true"
    

    现在一切正常。

    【讨论】:

      猜你喜欢
      • 2019-10-18
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多