본문 바로가기

STUDY/Spring

Spring | 스프링 시큐리티(Spring Security) (1) 세팅하기

1. pom.xml에 dependency추가

 

  • spring-security-core
  • spring-security-web
  • spring-security-config
  • spring-security-taglibs
<!-- Spring Security -->
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-core</artifactId>
  <version>${spring.security.version}</version>
</dependency>		
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-web</artifactId>
  <version>${spring.security.version}</version>
</dependency>		
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-config</artifactId>
  <version>${spring.security.version}</version>
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
  <artifactId>spring-security-taglibs</artifactId>
  <version>${spring.security.version}</version>
</dependency>

 

 

2. web.xml에 context추가 및 filter추가

 

spring-security.xml 등록

 

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
  	/WEB-INF/spring/root-context.xml
  	/WEB-INF/spring/spring-security.xml
  </param-value>
</context-param>

 

 

filter추가 (웹 애플리케이션의 모든 요청을 매핑)

※ 무조건 한글 encoding필터 뒤에 오도록 배치할 것!!! 한글 인코딩 필터 앞에 배치할 경우 한글깨짐

 

<filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

 

 

 

3. spring-security.xml파일 작성

 

spring폴더 우클릭 - New - Other - Spring Bean Configuration File선택

 

경로 확인

 

 

최소한의 설정만 우선 해두었음.

*스프링 시큐리티 5.0.x버전 사용시 spring-security-5.0.xsd에서 -5.0부분 삭제

 

<?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:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<security:http>
		<security:form-login />
	</security:http>
	
	<security:authentication-manager>	
	</security:authentication-manager>

</beans>

 

 

 

4. 스프링 시큐리티 설정이 잘 되었나?

 

login이라는 페이지를 작성하지 않았는데도 시큐리티가 만들어 주었음!