개발자의 오르막

[#Error NginX, SpringBoot ] 스프링 시큐리티 Https, ngnix 설정 본문

Trouble Shouting

[#Error NginX, SpringBoot ] 스프링 시큐리티 Https, ngnix 설정

계단 2020. 5. 13. 11:40

# 개발환경

 - Spring Boot 2.0 , Gradle

 - SpringBoot Security

 - ngnix

 

# 문제상황

 - Web 시큐러티를 설정할 때 특정 화면은 인증을 요구하는 스프링부트 시큐리티 로직 사용

 - 로컬서버, 개발서버에는 문제없이 작동

 - 운영서버 (nginx) 에서는 권한 인증을 위한 로그인 페이지 이동할 시 인덱스 화면으로 자동 이동

 

 

# 시큐러티 로그인 부분

http.authorizeRequests()
                .antMatchers("/로그인이 필요한 URL/**").hasAnyRole("해당 롤")
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated();

 - 위의 '로그인이 필요한 URL' 로 이동 시 원래는 로그인 페이지로 이동하였으나, 운영서버에서는 자동으로

    index 화면으로 리다이렉트 됨.

 - 그러나 로직 상 로그인페이지에서 자동적으로 index 페이지로 리다이렉트 하는 부분은 없었음.

 - 그리고, 로컬, 개발서버에서는 정상 작동으로 한 것을 보아 nginx 서버 측을 의심

 

- 로컬, 개발서버에서는 http URL 을 사용해도 무관하였지만, 운영서버에서는 http 로 다이렉팅 될 시 

  자동적으로 https을 붙인  index 화면으로 리다이렉팅 되고 있었음.

 

 - 스프링 시큐리티에서 login 페이지로 다이렉팅 할 때, http 가 아닌 https url 로 리다이렉팅 하게 하는 설정 필요

 


# 스프링 시큐리티 - HTTP/HTTPS 맵핑

https://www.baeldung.com/spring-channel-security-https

 

Spring HTTP/HTTPS Channel Security | Baeldung

The article briefly demonstrates how to protect sensitive data by configuring HTTPS and discuss further configuration details of the channel security feature.

www.baeldung.com

 

 

- https 적용이 되지 않은 기존  security 소스

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests() 
      .antMatchers("/anonymous*")
      .anonymous();
 
    http.authorizeRequests()
      .antMatchers("/login*")
      .permitAll();
 
    http.authorizeRequests()
      .anyRequest()
      .authenticated();

  로컬, 개발서버에서 http 로 구성된 Basic Security 소스이다. 여기서 모든 페이지에 어떤 인증 절차를 둘 것인지,

  인증시 어떤 페이지로 이동시킬 것인지 구성할 수 있다. 

  현재 로컬, 개발서버에서 http 로 구성되어 있으나, 운영서버인 nginx 에서는 https 로 구성되어 있기 때문에

  위의 시큐리티 소스로는, http://localhost:8080/login 으로 갈 수밖에 없다.

 

* Tomcat 에 https 가 적용되지 않았을 시에는 위의 공식 사이트 - 3.HTTPS Server Configuration 을 참고해서

  Tomcat 설정을 해주자.

 

# 해결법

 - 해결법에는 크게 2가지가 있다.

   1) security 소스 추가

http.requiresChannel()
  .antMatchers("/login*").requiresSecure();

   2) xml 태그의 속성 추가

<intercept-url pattern="/login*" access="permitAll" requires-channel="https"/>

 

 - 1번 방법으로 풀이했을 때의 소스 예시

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/spiiters/me").hasRole("SPITTER")
            .antMatchers(HttpMethod.POST,".spittles").hasRole("SPITTER")
            .anyRequest().permitAll()
        .and()
        .requiresChannel()
            .antMatchers("/spitter/form").requiresSecure();
}

 

 - 두번째 방법으로는 nginx 서버 설정을 바꾸는 방법이 있었다.

   현재 http 의 url 이 들어올 때 https://index  url 로 return 되게끔 설정이 되어 있었다.

   이를 nginx.config 설정에 들어가서 바꿔주는 방법으로 하였다.

server {
       listen         80;
       server_name    example1.com example2.com;
       return         301 https://$host$request_uri;
}

 


# 참고 블로그

- https://m.blog.naver.com/kimnx9006/220638156019

 

[스프링프레임워크] 스프링 시큐리티 -3. 요청 가로채기

요청 가로채기각 요청에 대해서 보안 수준을 잘 조절하기 위한 키는 WebSecurityConfigurerAdapter의 c...

blog.naver.com

 

 

Comments