티스토리 뷰

Spring

Spring Security (Spring Boot) #2

§무명소졸§ 2020. 1. 10. 17:46

SecurityConfiguration

application.yml 파일에서도 spring security 관련 설정을 할 수 있지만 spring boot 2.x 에서 많은 부분이 deprecated 됐고 좀더 세밀한 설정을 하기 위해서는 WebSecurityConfigurerAdapter를 상속 받는 클래스를 생성해야된다. 아래 코드는 #1의 설정을 자바로 설정한 코드이다.

@EnableWebSecurity
public class S1SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    	http.formLogin();
        http.authorizeRequests().anyRequest().authenticated();   
    }
}

 

http.formLogin()

메소드는 Spring에서 제공하는 default form(#1에서 로그인 UI) 을 사용할 수 있게 해준다. 이하 아래 이미지들의 메소드들로 로그인 처리 및 UI의 customize를 제공한다.

http.authorizeRequests().anyRequest().authenticated();

라인은 요청에 대한 권한처리를 한다. 지금 설정은 모든 요청에 대해서 인증과 권한을 체크한다. 해당 라인이 없다면 /my-page 요청시 로그인 과정없이 바로 조회가 된다.

http.basic()

http.form() 을 제거하고 http.basic() 라인을 추가하면 http 의 기본 인증을(RFC 7617) 사용한다.

@EnableWebSecurity
public class S1SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic();
        http.authorizeRequests().anyRequest().authenticated();
    }
}

/my-page 페이지 요청시 응답헤더에 WWW-Authenticate: Basic realm="Realm"을 포함시키고 이러한 응답을 받은 브라우저는 기본 로그인 Dialog 를 출력합니다.

응답헤더

HTTP/1.1 401
WWW-Authenticate: Basic realm="Realm"  //Http Basic 인증일 경우 응답헤더에 포함
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: text/html;charset=UTF-8
Content-Language: ko-KR
Content-Length: 301
Date: Fri, 10 Jan 2020 10:01:02 GMT
Keep-Alive: timeout=60
Connection: keep-alive

로그인 Dialog

로그인 Dialog에 사용자 이름과 비밀번호를 입력하고 로그인을 하면 /my-page 에 접근할 수 있다. 단 http 기본 인증을 사용할 경우 아이디 비밀번호가 노출될 수 있기 때문에 보안 수준이 높지 않은 곳에서만 사용해야 된다.

Http Basic 의 보안취약점 확인

/my-page 요청시에 헤더 정보를 살펴보면 아래 빨간색 표시 영역에 인증정보 Authoriziation: Basic bTJzajoxMjM0 라인을 확인할 수 있다. 암호화된 문자열처럼 보이지만 base64 인코딩 됐을뿐이다.

온라인 인코딩 Tool 사이트에서 확인 https://www.base64decode.org/

아래와 같이 디코딩을 하면 비밀번호를 그대로 확인할 수 있다.

 

Base64 Decode and Encode - Online

Decode from Base64 or Encode to Base64 - Here, with our simple online tool.

www.base64decode.org

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크