LoginSignup
14
12

More than 5 years have passed since last update.

Spring Bootでセッションクッキーの名前を変える

Last updated at Posted at 2015-08-02

セッションクッキーとは、JavaのwebappでデフォルトだとJSESSIONIDってなってるやつ。
以下のように、ServletContextInitializerを返す@Beanを定義すれば良い。eiryu.sessionと言う名前に変えている。
単なる設定なので、Spring Frameworkでも同じように変更出来ると思われる。
プロパティで出来るかと思って色々漁ってみたが、見つからなかった。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.context.annotation.Bean;


@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ServletContextInitializer servletContextInitializer() {
        return servletContext -> servletContext.getSessionCookieConfig().setName("eiryu.session");
    }

}

使い道

セキュリティ対策。
JSESSIONIDだと一発でJavaのwebappとバレる。PHPSESSIDとかにしてPHP webappと見せかけるとかも考えられる。
尚、Spring MVCはコントローラのRequestMappingの定義を/listとかにしていれば/list.phpのリクエストも受け付けることが出来るので偽装は簡単。/list/とか書くとダメ。

デフォルト厨なので、本当はあんまりカスタムしない方が好きなのだが、セキュリティはいかにデフォルトから離れた定義にしてあるかが重要だと思うので、割りと大事かもしれん。

あとは、webapp作る上での遊び心かな。。

参考

14
12
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
14
12