스프링 Security(2) 실습
개요
시큐리티가 필요한 URL 정의가 필요.
❗시큐리티가 필요한 제어대상 URL
💡 1. 로그인을 하지 않은 사용자도 접근 가능한 URL ➔ /sample/all
💡 2. 로그인 한 사용자들이 접근할 수 있는 URL ➔ /sample/member
💡 3. 로그인 한 사용자들 중에서도 관리자 권한을 가진 사용자만이 접근할 수 있는 URL ➔ /sample/admin
참고한 블로그
https://taetoungs-branch.tistory.com/156
Spring Web Security의 기본 설정
STEP1. pom.xml
<!-- 스프링 공식 문서에는 spring-security-web만 존재하지만 다양한 처리를 하기 위해 아래와 같이 web,config,core도 pom.xml에 추가하도록 한다. (3개는 모두 동일한 버전일 것) -->
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.6.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.6.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<!-- JSP에서 스프링 시큐리티 관련된 태그 라이브러리를 활용하기 위해 아래의 spring-security-taglibs도 추가한다. -->
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
참고한 블로그
(pom.xml의 내용을 수정하면 반드시 maven update 해주기)
STEP2. security-context.xml
1. new > other xml이나, Spring Bean Configuration File 선택
2. 이름은 security-context.xml 으로 생성한다.
# 경로: /bck/src/main/webapp/WEB-INF/spring/security-context.xml
3. 이클립스에서 작성한 security-context.xml의 namespace에서 security항목을 체크한다.
❗로드 시점에 초기화가 필요한 것
💡 스프링 시큐리티는 단독으로 설정할 수 있기 때문에 기존의 root-context.xml이나 servlet-context.xml과는 별도로 security-context.xml을 작성하는 것이 좋다.
STEP3 web.xml 설정
스프링 시큐리티를 스프링 MVC에서 사용하기 위해 필터를 이용해서 스프링 동작에 관여하도록 설정한다.
<!-- AS-IS -->
<!--
<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>
-->
<!-- TO-BE -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- security-context.xml의 경로를 추가하여 설정파일의 위치를 알려주는 아래의 코드로 변경해준다. -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml
/WEB-INF/spring/security-context.xml </param-value>
</context-param>
❗개행으로 구분
💡 **
STEP4. security-context.xml (추가설정)
<!-- AS-IS -->
<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-5.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- TO-BE -->
<?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>
❗개행으로 구분
💡 스프링 시큐리티가 동작하기 위해서는 Authenrication Manager라는 존재와 스프링 시큐리티의 시작 지점이 필요하기 때문에 위와 같이 최소한의 설정을 지정한 후에 실행한다.
💡 xml을 이용해서 스프링 시큐리티를 설정할 때 5.0 네임스페이스에서 문제가 발생한다고 함.
💡 xml을 이용해서 스프링 시큐리티를 설정할 때 5.0 네임스페이스에서 문제가 발생한다고 함. (5.0.xsd라고 쓰여진 부분을 제거하면 된다.)
-
STEP5. 서버 실행
❗서버를 실행하였을 때 에러가 발생하지 않는다면 스프링 시큐리티의 최소한의 설정은 마무리 된 것이다!
Leave a comment