Spring redirectAttributes не работает в других профилях, кроме "dev" - PullRequest
0 голосов
/ 23 февраля 2020

У меня есть контроллер, который должен перенаправлять на другую конечную точку после обработки, но он работает только тогда, когда я устанавливаю активный профиль на 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.

Этот код работает в профиле "dev"

1 Ответ

0 голосов
/ 23 февраля 2020

Мне объявили, что bean-компонент настроен на строгие куки, что иногда мешало перенаправлению.

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

Перенаправление сработало после его удаления.

...