Skip to content

Instantly share code, notes, and snippets.

@sineadmcl13
Last active April 13, 2017 07:54
Show Gist options
  • Save sineadmcl13/f87913fc50c5baf766fd7e08ce2a0c9a to your computer and use it in GitHub Desktop.
Save sineadmcl13/f87913fc50c5baf766fd7e08ce2a0c9a to your computer and use it in GitHub Desktop.
Spring Redirect Attributes being lost when redirecting to absolute url via a gateway such as Zuul

Spring Redirect Attributes and redirect to absolute url via Zuul

When using the Spring RedirectAttributes, it uses a FlashMap with a targetRequestPath This targetRequestPath is checked against each incoming request and if they match the it is added to the Model

However I found when using RedirectAttributes and then redirecting to an absoluteUrl to integrate with zuul, the request being checked against the FlashMap targetRequestUrl included the zuul route and so failed to match.

This gist shows my fix by stripping the zuul route from the targetRequest being matched against

@Configuration
public class Config extends WebMvcConfigurerAdapter {
@Bean
public FlashMessageHandler flashMapManager(){
return new FlashMessageHandler();
}
}
@Component
public class FlashMessageHandler extends SessionFlashMapManager {
@Value("#{'${spring.application.name}'.toUpperCase()}")
private String applicationName;
@Override
protected boolean isFlashMapForRequest(FlashMap flashMap, HttpServletRequest request) {
String expectedPath = flashMap.getTargetRequestPath();
flashMap.setTargetRequestPath(expectedPath.replace("/"+applicationName, ""));
return super.isFlashMapForRequest(flashMap, request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment