[JAVA] I want to Flash Attribute in Spring even if I set a reverse proxy! (do not do)

Conclusion

If the URL to access and the request URL received by Spring are different, you will not be able to pass parameters to the redirect destination even with FlashAttribute! !!

The beginning of things

For example, suppose you have a proxy that changes a request for domain / ~ to domain / hoge / ~. I'm sure it looks like this: kissing: (appropriate)

nginx.conf


  location / {
    proxy_pass http://tomcat:8080/hoge/;
  }

In that, when I executed addFlashAttribute of RedirectAttributes, I encountered a situation where it was not mapped at the redirect destination. Maybe something like this: kissing_heart: (more appropriate)

HogeController.java


@RequestMapping(value="/hoge/save", method = RequestMethod.POST)
public String save(@ModelAttribute("form") HogeForm form, RedirectAttributes redirectAttributes) {
        :
        :
    redirectAttributes.addFlashAttribute("form", form);
    return "redirect:/complete";
}

@RequestMapping(value="/hoge/complete", method = RequestMethod.GET)
public String complete(@ModelAttribute("form") HogeForm form) {
    log.info(form); //empty! !!
    return "hoge.html";
}

You can afford FlashAttribute! The problem that occurred just before I thought. I investigated from the reverse proxy direction, but I do not know the cause at all. .. ..

:thinking: :thinking: :expressionless: :sleepy: :sleeping:

A few days later

: desktop: "If RedirectAttributes doesn't work, you can use FlashMap directly."

What an outrage! Looking at the implementation method while thinking, I am calling a method called setTargetRequestPath. .. ..

: hashed: ... target ... pass?

Let's explain! !!

What was happening was that the redirected path (/ complete) and the redirected path (/ hoge / complete) did not match, so the FlashMap values were not bound! !!

Repair example

With this kind of modification, parameters can be passed to the redirect destination even if a reverse proxy is set.

AbstractController.java


protected String redirect(String path, Map<String,Object> attributeMap) {
	//Internal path(@RequestMapping(value)The value of the)
	String innerPath = "/hoge" + path;
	//Refill to FlashMap
	FlashMap flashMap = new FlashMap();
	attributeMap.forEach(flashMap::put);
	//Set FlashMap target to internal path
	flashMap.setTargetRequestPath(innerPath);
	//FlashMap set
	RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashMap, request, response);
	//The redirect destination is an external path(Access from a browser)
	return "redirect:" + path;
}

HogeController.java


@RequestMapping(value="/hoge/save", method = RequestMethod.POST)
public String save(@ModelAttribute("form") HogeForm form) {
        :
        :
    return redirect("/complete", Map.of("form", form));
}

Even if you are using rewrite and the internal path and external path cannot be simply replaced like this time, the principle is the same, so please do your best ** and specify the internal path and external path.

FlashMap was written in the following article in an easy-to-understand manner. I wanted to find it sooner. .. .. : innocent: Pass the value to the redirect destination without using Redirect Attributes in Spring MVC

Recommended Posts

I want to Flash Attribute in Spring even if I set a reverse proxy! (do not do)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to create a Parquet file even in Ruby
If hash [: a] [: b] [: c] = 0 in Ruby, I want you to extend it recursively even if the key does not exist.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
"Teacher, I want to implement a login function in Spring" ① Hello World
I want to be eventually even in kotlin
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to do something like "cls" in Java
I want to use a little icon in Rails
I want to define a function in Rails Console
I want to click a GoogleMap pin in RSpec
MockMVC returns 200 even if I make a request to a path that does not exist
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...
What to do if you get a java.io.IOException in GlassFish
I want to find a relative path in a situation using Path
I want to set the conditions to be displayed in collection_check_boxes
I want to give a class name to the select attribute
How to set up a proxy with authentication in Feign
[Wire Mock] I want to set up a stub / mock server in Java and perform E2E tests.
Do I need a test if I do DDD in a language with types?
What to do if you get a gcc error in Docker
[Rails] What to do if data is not registered in DB
[Ruby] I want to put an array in a variable. I want to convert to an array
What to do if you get a DISPLAY error in gym.render ()
I want to change the value of Attribute in Selenium of Ruby
What to do if Operation not permitted is displayed when you execute a command in the terminal
[Rails] I want to send data of different models in a form
What to do if you get a groovy warning in Thymeleaf Layout
What to do if the prefix c is not bound in JSP
I want to select multiple items with a custom layout in Dialog
[Note] I want to get in reverse order using afterLast with JdbcTemplate
I can't build if I set the build destination to a simulator with XCode12!
If you want to dynamically embed values & add text to attribute values in Thymeleaf 3
I want to display a PDF in Chinese (Korean) with thin reports
My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot
I want to ForEach an array with a Lambda expression in Java
[Java Spring MVC] I want to use DI in my own class
I want to develop a web application!
I want to write a nice build.gradle
I want to RSpec even at Jest!
I want to write a unit test!
What to do if you get Could not save master table to file after importing a project in Eclipse
Steps to set a favicon in Rails
I want to use @Autowired in Servlet
Add if not in Set, error message if
I want to do team development remotely
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
What to do if the changes are not reflected in the jar manifest file
[Swift] What to do if the app icon is set but not reflected
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Royal road edition that is neither magic nor anything)
I want to send an email in Java.
I want to pass APP_HOME to logback in Gradle
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I want to simply write a repeating string
How to add a classpath in Spring Boot
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
[Xcode] I want to manage images in folders
Put nginx 1.18 in CentOS6 and set reverse proxy