Spring 重定向属性在除"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

此代码适用于"dev"配置文件

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

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

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

最新更新