티스토리 뷰

Skill/spring

HSTS 관련 설정

진열사랑 2025. 4. 7. 17:53

설정설명

maxAgeInSeconds() 브라우저가 HTTPS만 사용해야 함을 기억하는 기간 (초) - 1년은 31536000초
includeSubDomains() 서브도메인에도 HSTS 적용할지 여부
preload(true) HSTS preload list 에 등록할 의향이 있다면 설정

 

전자정부 프레임워크(egovframe)에서 HSTS (HTTP Strict Transport Security) 헤더 정책을 설정하려면, 아래 두 가지 방식 중 하나를 선택하여 구현할 수 있습니다:


✅ 1. Spring Security 사용 시 (추천)

전자정부 프레임워크는 Spring Security를 연동해서 사용하는 경우가 많기 때문에, 이를 통해 HSTS 설정을 할 수 있습니다.

🔹 설정 방법

context-security.xml 또는 Java Config에서 다음과 같이 설정:

👉 context-security.xml 사용 예 (XML 기반 설정)

<http>
    <headers>
        <hsts include-subdomains="true" max-age-seconds="31536000" preload="true"/>
    </headers>

    <!-- HTTPS 강제 리디렉션 -->
    <requires-channel>
        <intercept-url pattern="/**" access="REQUIRES_SECURE_CHANNEL"/>
    </requires-channel>
</http>

👉 Java Config (Spring Security 5 이상일 때)

전자정부 프레임워크에 Java Config 기반 Spring Security를 사용하고 있다면:

@Configuration
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .requiresChannel(channel ->
                channel.anyRequest().requiresSecure()
            )
            .headers(headers ->
                headers.httpStrictTransportSecurity(hsts ->
                    hsts.includeSubDomains(true)
                        .maxAgeInSeconds(31536000)
                        .preload(true)
                )
            );

        return http.build();
    }
}

✅ 2. 필터(Filter) 또는 인터셉터로 직접 헤더 추가 (Spring Security를 안 쓸 때)

전자정부 프레임워크에서는 Filter나 HandlerInterceptor를 이용해 HSTS 헤더를 직접 추가할 수 있습니다.

🔹 필터 방식 (HSTS 헤더 강제 추가)

@WebFilter("/*")
public class HstsHeaderFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httpReq = (HttpServletRequest) request;
        HttpServletResponse httpRes = (HttpServletResponse) response;

        // HTTPS 요청에만 적용 - 개발에 적용 안되도록 할 수 있음.
        if (httpReq.isSecure()) {
            httpRes.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload");
        }

        chain.doFilter(request, response);
    }
}

web.xml에 등록하거나, @WebFilter + @ServletComponentScan 방식으로 활성화할 수 있어요.


🔒 HSTS 헤더 예시

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함