스프링 시큐리티, 설정
스프링 시큐리티는 애플리케이션에 보안 기능을 구현할 때 사용하는 프레임워크로서, 주로 서블릿 컨테이너에
배포하는 웹 애플리케이션의 보안 기능을 구현할 때 활용한다.
특징
다양한 옵션을 제공
기본 구현 클래스의 동작 방식을 커스터마이징 할 수 있는 다양한 옵션을 제공한다.
그래서 기본 동작 방식이 보안 요구사항에 부합하지 않더라도 옵션 값을 변경하는 방법으로 요구사항을 충족하도록
설정 할 수 있다.
다양한 확장점을 제공
동작 방식을 커스터마이징 할 수 있는 다양한 확장점을 사용한다. 그래서 기본 동작 방식이 보안 요구사항에
부합하지 않더라도 확장 클래스를 만드는 방법으로 요구사항을 충족할 수 있다.
기본적인 보안기능
기본적인 보안기능으로 인증과 인가라는 두가지 기능이 있다.
인증기능
애플리케이션 사용자의 정당성을 확인한다.
인가기능
애플리케이션의 리소스나 처리에 대해 접근을 제어한다.
강화된 보안기능
인증과 인가라는 기본적인 기능 외에도 웹 애플리케이션의 보안을 강화하기 위한 기능을 제공한다.
기능 |
설명 |
세션 관리 기능 |
세션 하이재킹(session hijacking)이나 세션 고정(session fixation) 공격으로부터 사용자를 보호하고, 세션의 라이프사이클(생성, 파기, 타임아웃)을 제어한다. |
CSRF방지 기능 |
크로스 사이트 요청 변조(Cross-Site Request Forgery: CSRF) 공격으로부터 사용자를 보호한다. |
브라우저의 보안 기능과의 연계기능 |
브라우저의 보안기능과 연계해서 브라우저 기능을 악용한 공격에서 사용자를 보호 할 수 있는 보안 헤더를 출력한다. |
시큐리티 라이브러리의 설정
pom.xml 정의
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
시큐리티의 빈 정의
설정 클래스의 작성
package.springbook.config;
import org.springframework.security.config.annotation.web.builders.*;
import org.springframework.security.config.annotation.web.configuration.*;
@EnableWebSecurity //클래스에 @EnableWebSecurity를 지정하면 스프링 시큐리티가 제공하는 설정 클래스가 임포트되고 스프링 시큐리티를 이용할 때 필요한 컴포넌트의 빈이 자동으로 정의된다.
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { //부모클래스로 WebSecurityConfigurerAdapter클래스를 지정한다. 상속하면 기본적으로 적용되는 빈의 정의를 간단히 커스터마이징 할 수 있다.
@Override
public void configure(WebSecurity web){
web.ignoring().antMatchers("/resources/**"); //보안기능이 필요 없는 리소스(CSS나 자바스크립트)에는 스프링 시큐리티를 적용하지 않는다.
}
}
이렇게 만든 설정 클래스를 사용해 DI 컨테이너가 만들어지도록 정의
web.xml의 설정 예
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<!-- contextConfigLocation에 작성한 설정 클래스를 지정 -->
<param-name>contextConfigLocation</param-name>
<param-value>springbook.config.WebSecurityConfig</param-value>
</context-param>
xml파일 작성
XML기반 설정 방식을 사용할 때는 다음과 같은 파일을 작성.
XML파일(security-context.xml)의 작성 예
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
">
<!-- 스프링 시큐리티가 제공하는 XML 네임스페이스를 활성화.
이 예에서는 sec라는 이름을 할당하고 있다.
네임스페이스를 이용하면 스프링 시큐리티의 컴포넌트를 빈으로 간단히 정의할 수 있다. -->
<sec:http>
<!-- <sec:http>요소를 정의. 정의하면 시큐리티를 이용할 때 필요한 컴포넌트의 빈이 자동으로 정의 -->
<sec:intercept-url pattern="/**" access="isAuthenticated()" />
<sec:form-login />
<!-- 이 예에서는 보안 설정이 제대로 적용되었는지 확인하기 쉽도록 요청되는 모든 경로에 인증을 하도록 만들었다.
인증방식으로는 폼 기반 인증 기능을 사용 -->
</sec:http>
<sec:authentication-manager />
<!-- <sec:authentication-manager>요소를 정의해 인증용 컴포넌트를 빈으로 정의한다.
이 요소를 정의하지 않으면 서버를 가동할 때 오류가 발생한다. -->
</beans>
보안 기능이 필요없는 리소스(CSS나 자바스크립트)에 대해서는 다음과 같이 빈을 정의해서 스프링 시큐리티가 인증을
하지 않게 만든다. 만약 <sec:http>요소가 여러개 정의되어 있다면 정의한 순서대로 경로 패턴을 매칭하기 때문에
위에서 설정한 <sec:http>요소보다 다음 설정을 순서상 먼저 기술 해야 한다.
스프링 시큐리티가 인증하지 않도록 하기위한 빈 정의 예
<!-- 인증이 필요없는 리소스에 대한 경로 패턴을 지정 -->
<sec:http pattern="/resources/**" security="none" />
이렇게 만든 XML파일을 사용해 DI컨테이너가 만들어지도록 정의한다.
web.xml의 설정 예
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<!-- contextConfiglocation에 작성한 XML파일을 지정 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/META-INF/spring/security-context.xml</param-value>
<context-param>
서블릿 필터 설정
마지막으로 스프링 시큐리티에서 제공하는 서블릿 필터 클래스(FilterrChainProxy)를 서블릿 컨테이너에 등록한다.
web.xml의 설정 예
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<!-- 스프링 프레임워크가 제공하는 DelegatingFilterProxy를 사용해 DI컨테이너에서 관리되는
빈(FilterChainProxy)을 서블릿 컨테이너에 등록.
서블릿 필터의 이름으로 DI 컨테이너에서 관리되는 빈의 이름(springSecurityFilterChain)을
지정한다. -->
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 스프링 시큐리티를 적용할 URL 패턴을 지정. 이 예에서는 모든 요청에 대해 스프링 시큐리티를 적용-->
(참고 : 스프링 철저 입문)