Spring Security を使用している場合は、Spring Boot リファレンスsecurity.require_ssl=true
に記載されているように、application.properties に追加することでこれを行うことができます。Spring Security 構成をカスタマイズする場合、次のようなものが必要になります。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// ...
.requiresChannel()
.anyRequest().requiresSecure();
}
}
Spring Security を使用しておらず、war ファイルを使用しているため、最も簡単な方法は、以下を含む web.xml を作成することです。
src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<security-constraint>
<web-resource-collection>
<web-resource-name>all</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>
アプリケーション全体のセキュリティ制約をプログラムで設定する方法がないため、web.xml を使用する必要があります。詳細については、How to programmatically setup a <security-constraint> in Servlets 3.x? を参照してください。