環境
Spring Tool Suite 4
Version: 4.1.0.RELEASE
Build Id: 201812201400
$ java -version
java version "1.8.0_201"
macOS 10.14.3
★やりたいこと
Spring Boot で、http →httpsへのリダイレクトをapplication.propertiesで設定したい。
現状のapplication.properties
http → https へのリダイレクト設定 #####################
server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.protocol-header=x-forwarded-proto
security.require-ssl=true
server.ssl.protocol=TLS
#######################################################
SSL設定 #####################################################
server.port: 8443
server.ssl.key-store: keystore.p12
server.ssl.key-store-password: hogehoge
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat
★できること
SSLでの開発環境へのアクセスは可能
例:https://localhost8443/
Helo Wordは表示される。
http://localhost8443/ で来た場合に https://localhost8443/ へのリダイレクトをかけたい。
★やったこと
https://code.i-harness.com/ja-jp/q/196bc83
1- pom.xmlにバネセキュリティの依存性を追加する
2 - このクラスをアプリケーションのルートパッケージに追加します。
を実施しましたが、意図する動きができません。
◼︎:SecurityConfig.java
package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requiresChannel().anyRequest().requiresSecure();
}
}
上記、ファイルをDemoApplicationと同階層に配置。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }
}
お手数をお掛けいたしますが、情報などご教授頂けますと幸いです。
よろしくお願いします。
※サーバー(apache/tomcat)側でのやり方は、知っています。
spring内で完結したく、ご質問をさせていただきました。
あなたの回答
tips
プレビュー