Memo

メモ > 技術 > フレームワーク: SpringBoot > ログイン時にSSLなしになるので調査

■ログイン時にSSLなしになるので調査
「ブラウザ -443-> nginx -8080-> Spring Boot」という環境でログインボタンを押すと、SSLなしのURLにリダイレクトされるので調査 ■設定ファイルを調整 application-production.properties に以下を追加するも効果なし
server.tomcat.remoteip.remote-ip-header=x-forwarded-for server.tomcat.remoteip.protocol-header=x-forwarded-proto
Elastic Beanstalk(Java) + Spring Boot + https - Qiita https://qiita.com/peko_kun/items/5d33c9660f9b90cdd231 Spring Boot に入門した話〜架空のキャンプ場の予約アプリを Elastic Beanstalk にデプロイするまで〜?デプロイ編 - sgyatto's blog https://sgyatto.hatenablog.com/entry/2021/08/20/002610 ■Beanを設定 Beanを設定するとあるが、具体的にどこに設定するのか判らず リバースプロキシサーバーがいるときのSpring BootのリダイレクトURL作成 - Qiita https://qiita.com/nogitsune413/items/f47d07f2250c874e6e3e Forwarded Headers Example in Spring - Code Tinkering https://codetinkering.com/spring-forwarded-headers-example/ ■nginxでproxy_set_headerを追加 nginxの /etc/nginx/conf.d/https.conf にproxy_set_headerの指定を追加するも効果なし
location / { proxy_pass http://localhost:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; # 以下を追加 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Remote-Addr $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-forwarded-For $proxy_add_x_forwarded_for;
# nginx -t # systemctl restart nginx # systemctl status nginx Spring-bootでSSL(HTTPS)有効にするならnginxを使え〜完結編〜 - Apitore blog https://blog.apitore.com/2016/07/25/spring-boot-ssl-nginx/ ■常にSSLへ転送 vi /etc/nginx/conf.d/https.conf に以下を追加して一応ログインできるようになるが、二重にリダイレクトされている(いったんHTTPに飛ばされ、それからHTTPSにリダイレクトされる)
# ファイルの先頭に以下を追加 server { listen 80; listen [::]:80; return 301 https://$host$request_uri; }
【SSL対応】AWS EC2 + SpringBoot + Nginx + Let’s EncryptでHTTPS対応 | エンジニアの本気で稼ぐ不労所得戦略 https://1-lifengine.com/ec2_spring_nginx_https ■現状の対応 Spring boot redirect のdefaultはhttpになることを認識しておく - Qiita https://qiita.com/yukiyoshimura/items/29785c1caa256be69500 application.properties で以下のように定義しておく(開発環境を想定)
application.url=http://localhost:8080
application-production.properties では以下のように定義しておく(本番環境を想定)
application.url=https://refirio.net
リダイレクトを行う個所は、すべて以下のように変更する
return "redirect:/task/"; ↓ return "redirect:" + environment.getProperty("application.url") + "/task/";
securityFilterChain は一例だが以下のようにする(リダイレクトのURLを調整している)
@Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { //http.authorizeRequests().anyRequest().permitAll(); http .formLogin((form) -> form // 認証を行うURL .loginProcessingUrl("/admin/login") // ログインページのURL .loginPage("/admin/") // ログインユーザ名の項目 .usernameParameter("username") // ログインパスワードの項目 .passwordParameter("password") // 認証に成功したときにリダイレクトするURL .defaultSuccessUrl(environment.getProperty("application.url") + "/admin/home") // 認証に失敗したときにリダイレクトするURL .failureUrl(environment.getProperty("application.url") + "/admin/?error") // すべてのユーザがアクセス可能 .permitAll() ) .logout((logout) -> logout // ログアウトページのURL .logoutUrl("/admin/logout") // ログアウトしたときにリダイレクトするURL .logoutSuccessUrl(environment.getProperty("application.url") + "/admin/?logout") ) .authorizeHttpRequests((authorize) -> authorize // CSSなどはログイン無しでもアクセス可能 .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() // ログイン無しでもアクセス可能なURL .requestMatchers("/", "/error", "/dev/**", "/admin/", "/admin/login").permitAll() // 「/admin/」へのアクセスには「ADMIN」権限が必要 .requestMatchers("/admin/**").hasRole("ADMIN") // 他のページはすべて認証が必要 .anyRequest().authenticated() ); return http.build(); }
nginxの /etc/nginx/conf.d/https.conf で、SSLを強制しておく
server { listen 80; listen [::]:80; return 301 https://$host$request_uri; }
これでSSLを維持できる ※「ログイン無しでもアクセス可能なURL」に「/error」が無いと、ログインを試みる際、https://refirio.net/error?continue に遷移しようとしてしまう ただし一度管理画面を表示すると、以降は大丈夫になる 謎な挙動だが、ひとまず上記のように書くことで回避できている 改めて調査したい ↑MySQL側でのユニークエラーが発生したときなども、エラー画面を表示しようとしてログイン画面が表示されることがあるみたい ひとまず「常に /error を含めておく」とするのが無難か Spring Boot で 404 Not Found などのエラーが発生した際の表示をカスタマイズする - Qiita https://qiita.com/niwasawa/items/f3479ef16efa488039fb もっとスマートな方法があるか、引き続き確認したい Spring boot redirect のdefaultはhttpになることを認識しておく - Qiita https://qiita.com/yukiyoshimura/items/29785c1caa256be69500 IPアドレスなどについては、改めて以下などを確認しておきたい Spring Bootでリダイレクト先のURLを組み立てる - Qiita https://qiita.com/rubytomato@github/items/8d132dec042f695e50f6 ■その他 Spring Bootでリダイレクト先のURLを組み立てる - Qiita https://qiita.com/rubytomato@github/items/8d132dec042f695e50f6 java - Setup Nginx + SSL + Spring MVC + Security - Stack Overflow https://stackoverflow.com/questions/66833026/setup-nginx-ssl-spring-mvc-security Spring Security 18. HTTPS にリダイレクト - リファレンス https://spring.pleiades.io/spring-security/site/docs/5.1.7.RELEASE/reference/html/webflux-redirect-h... Spring Boot 本番対応機能 - リファレンスドキュメント https://spring.pleiades.io/spring-boot/docs/current/reference/html/actuator.html

Advertisement