LoginSignup
0
1

More than 3 years have passed since last update.

リバースプロキシを設定しててもSpringでFlashAttributeしたい!(しない)

Posted at

結論

アクセスするURLとSpringが受け取るリクエストURLが異なると、FlashAttributeしてもリダイレクト先にパラメータを渡せないぞ!!

事の始まり

例えば、domain/~のリクエストをdomain/hoge/~に変更するプロキシが組まれていたとします。
きっとこんな感じ:kissing:(適当)

nginx.conf
  location / {
    proxy_pass http://tomcat:8080/hoge/;
  }

その中で、RedirectAttributesのaddFlashAttributeを実行したところ、リダイレクト先でマッピングされない事態に遭遇しました。
たぶんこんな感じ:kissing_heart:(さらに適当)

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); // 空っぽ!!
    return "hoge.html";
}

FlashAttributeなんて余裕やろ!と思っていた矢先に起きた問題。
リバースプロキシ方面から調査したのですが、原因が全くわからず。。。

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

数日後

:desktop:「RedirectAttributesがダメなら、FlashMapを直接使えばいいじゃない」

なんという暴論!と思いながら実装方法を眺めていると、setTargetRequestPathというメソッドを呼んでいる。。。

:hushed:・・・ターゲットの・・・・パス?

説明しよう!!

何が起こっていたかというと、リダイレクト先のパス(/complete)と、リダイレクトされたパス(/hoge/complete)が不一致であったため、FlashMapの値がバインドされていなかったのだ!!

改修例

こういう改修をすれば、リバースプロキシを設定していてもリダイレクト先へパラメータを渡せるようになる。

AbstractController.java
protected String redirect(String path, Map<String,Object> attributeMap) {
    // 内部パス(@RequestMapping(value)の値)
    String innerPath = "/hoge" + path;
    // FlashMapに詰め替え
    FlashMap flashMap = new FlashMap();
    attributeMap.forEach(flashMap::put);
    // FlashMapのターゲットを内部パスに設定
    flashMap.setTargetRequestPath(innerPath);
    // FlashMapのセット
    RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashMap, request, response);
    // リダイレクト先は外部パス(ブラウザからのアクセス先)
    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));
}

rewriteを使っていて、今回のように内部パスと外部パスが単純に置き換えられない場合も、原理は一緒なので頑張って内部パスと外部パスを指定してください。

FlashMapについては、以下の記事でわかりやすく書いてありました。もっと早く見つけたかった。。。:innocent:
SpringMVCでRedirectAttributesを使用せずにリダイレクト先に値を渡す

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1