Spring

Spring Security

NYoun 2020. 7. 28. 19:10

Spring Security란?

 

  Spring Security란 애플리케이션에 보안 기능을 구현할 때 사용하는 프레임워크로서, 주로 서블릿 컨테이너에 배포하는

  웹 어플리케이션의 보안기능을 구현할 때 활용한다.

 

Spring Security의 특징

 

  다음과 같은 두가지 특징이 있다.

 

    1. 다양한 옵션을 제공

       기본 구현 클래스의 동작 방식을 커스터마이징할 수 있는 다양한 옵션을 제공한다. 그래서 기본 동작 방식이

       보안 요구사항에 부합하지 않더라도 옵션값을 변경하는 방법으로 요구사항을 충족하도록 설정할 수 있다.

 

    2. 다양한 확장점을 제공

      동작방식을 커스터마이징할 수 있는 다양한 확장점을 제공한다.

      그래서 기본 동작 방식이 보안 요구사항에 부합하지 않더라도 확장 클래스를 만드는 방법으로 요구사항을 충족할

      수 있다.

 

기본적인 보안기능

 

  기본적인 보안기능으로 인증과 인가라는 두가지 기능을 제공한다.

기능

설명

인증 기능

애플리케이션 사용자의 정당성을 확인한다

인가 기능

애플리케이션의 리소스나 처리에 대해 접근을 제어한다.

 

강화된 보안기능

 

  인증과 인가 외에도 웹 애플리케이션의 보안을 강화하기 위한 기능을 제공한다.

기능

설명

세션 관리 기능

세션 하이재킹(Session hijacking)이나 세션 고정(Session Fixation) 공격으로부터 사용자를 보호하고, 세션의 라이프 사이클(생성, 파기, 타임아웃)을 제어한다.

CSRF 방지 기능

크로스 사이트 요청 변조(Cross-Site Request Forhery: CSRF) 공격으로부터 사용자를 보호한다.

브라우저의 보안

기능과의 연계 기능

브라우저 보안기능과 연계해서 브라우저 기능을 악용한 공격에서 사용자를 보호할 수 있는 보안 헤더를 출력한다.

 

Spring Security의 설정

 

  라이브러리 설정.

  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>

  Bean 정의 예

package study.SecurityConfig;

import org.springframework.security.config.annotation.web.builders.*;
import org.springframework.security.config.annotation.web.configuration.*;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

	@Override
	public void configure(WebSecurity web) {
		web.ignoring().antMatchers("/resources/**");
	}
}

  @EnableWebSecurity를 지정하면 스프링 시큐리티가 제공하는 설정 클래스가 임포트되고 스프링 시큐리티를 이용할

  때 필요한 컴포넌트의 빈이 자동으로 정의된다.

  WebSecurityConfigurerAdapter를 상속하면 기본적으로 적용되는 빈의 정의를 간단히 커스터마이징 할 수 있다.

 

  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>study.Securityconfig.WebSecurityConfig</param-value>
	</context-param>

  xml파일 작성

    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라는 이름을 할당하고 있다.
         XML 네임스페이스를 이용하면 스프링 시큐리티의 컴포넌트를 빈으로 간단히
         정의할 수 있다.*/
	<sec:http>
		<sec:intercept-url pattern="/**" access="isAuthenticated()"/>
		<sec:form-login/>
	</sec:http>
	/* <sec:http>요소를 정의하면 스프링 시큐리티를 이용할 때 필요한 컴포넌트의 빈이 자동으로
         정의된다. 
         이 예에서는 보안이 제대로 적용되었는지 확인하기 쉽도록 모든 경로에 인증을
         하도록 하였다. 인증 방식으로는 폼 기반 인증 기능을 사용. */
         
	<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에 작성한 설정 클래스를 지정 -->
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/META-INF/spring/security-context.xml</param-value>
	</context-param>

 

서블릿 필터 설정

  마지막으로 스프링 시큐리티에서 제공하는 서블릿 필터 클래스(FilterChainProxy)를 서블릿 컨테이너에 등록한다.

 

  web.xml의 설정 예

	<filter>
		<filter-name>springSecurityFilterChain</filter-name>
		<filter-class>
			org.springframework.web.filter.DelegatingFilterProxy
		</filter-class>
	</filter>
    	/* 스프링에서 제공하는 DelegatingFilterProxy를 사용해 DI 컨테이너에서 관리되는
       	빈(FilterChainProxy)을 서블릿 컨테이너에 등록.
     	  서블릿 필터의 이름으로 DI 컨테이너에서 관리되는 빈의 이름(springSecurityFilterChain)을
     	  지정한다. 이 빈은 config에서 설정한 것에 의해 자동으로 DI 컨테이너에 추가된다. */
	<filter-mapping>
		<filter-name>springSecurityFilterChain</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
   

 

(참고 : 스프링 철저 입문)