繁体   English   中英

除了“dev”之外,Spring redirectAttributes 在其他配置文件中不起作用

[英]Spring redirectAttributes not working in other profiles besides "dev"

我有一个控制器,它应该在处理后重定向到另一个端点,但它仅在我将活动配置文件设置为dev时才有效。 当配置文件不是dev ,控制器返回登录页面而不是重定向到配置文件端点。

这是我的控制器

public String completeSignUp(
          @RequestParam final String token, @RequestParam final String userId,
          Model model, RedirectAttributes redirectAttributes, HttpServletRequest httpServletRequest) {
    LOG.debug("About to complete the sign-up process with token: {} and userId {}", token, userId);
    try {
      InputValidationUtility.validateInputs(getClass(), token, userId, model);
      UserDto userDto = updateUserAndSendConfirmationEmail(token, userId, model, httpServletRequest);
      if (Objects.isNull(userDto) || model.containsAttribute(SignUpControllerConstant.SIGN_UP_ERROR)) {
        model.addAttribute(UserConstant.USER_MODEL_KEY, new UserRequestModel());
        return SignUpControllerConstant.SIGN_UP_VIEW_NAME;
      }
      // automatically authenticate the userDto since there will be a redirection to profile page
      UserUtility.authenticateUser(userService, userDto.getUsername());
      model.addAttribute(SignUpControllerConstant.SIGN_UP_SUCCESS_KEY, true);
      model.addAttribute(USER_REQUEST_MODEL_KEY_NAME, new UserRequestModel());
      redirectAttributes.addFlashAttribute(ProfileControllerConstant.NEW_PROFILE, true);
      LOG.debug("Redirecting to profile page...");
      return "redirect:/profile";
    } catch (Exception e) {
      LOG.error(SignUpControllerConstant.ERROR_CREATING_USER, e);
      model.addAttribute(SignUpControllerConstant.ERROR, SignUpControllerConstant.ERROR_CREATING_USER);
      return SignUpControllerConstant.SIGN_UP_VIEW_NAME;
    }
  }

配置文件页面需要身份验证并具有端点/profile

此代码适用于“开发”配置文件

我有一个 bean 声明将 cookie 设置为严格,这有时会干扰重定向。

@Bean
  WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
    return tomcatServletWebServerFactory -> tomcatServletWebServerFactory.addContextCustomizers(context -> {
      Rfc6265CookieProcessor processor = new Rfc6265CookieProcessor();
      processor.setSameSiteCookies("strict");
      context.setCookieProcessor(processor);
    });
  }

一旦我删除它,重定向就起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM