느림보 개발
1. 스프링 시큐리티 기본 설정 본문
pom.xml
- 스프링 시큐리티는 스프링의 여러 하위 프로젝트 중 하나이므로 필요한 버전 추가 (위 3개는 버전 동일해야함)
- JSP에서 스프링 시큐리티 관련 태그 라이브러리를 활용할 수 있도록 함
<!-- 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>
<!-- 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-taglibs -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
security-context.xml 설정
- 스프링 시큐리티는 단독으로 설정할 수 있기 때문에 기존의 root-cotext나 servlet-cotext과는 별도로 security-context를 설정하는 것이 좋다.
- /src/main/webapp/WEB-INF/spring/security-context.xml
- namespace에서 security 항목 체크 → 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/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">
</beans>
web.xml 설정
스프링 시큐리티가 스프링 MVC에서 사용되기 위해서는 필터를 사용해서 스프링 동작에 관여하도록 설정한다.
<!-- 스프링 시큐리티 필터 설정 -->
<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>
<!-- 스프링 시큐리티 필터 설정 -->
필터 작성 후 실행시 에러가 발생하는데 그 원인은 'springSecurityFilterChain' 이라는 빈이 제대로 설정되지 않아서 스프링 시큐리티의 설정 파일을 찾을 없다고 나온다.
이에 대한 처리는 ① 작성된 security-context.xml을 로딩하도록 설정하는 작업과
②security-cotext.xml의 최소한의 설정이 필요하다.
web.xml
- web.xml로 가서 작성된 security-context.xml을 로딩하도록 설정하는 작업
- 모든 서블릿과 필터가 공유하는 루트 스프링 컨테이너의 정의
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<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>
security-context
- security-cotext.xml의 최소한의 설정
- 스프링 시큐리티가 동작하기 위해서는 Authentication Manager 라는 존재와 스프링 시큐리티의 시작 지점이 필요하기 때문에 아래와 같이 최소한의 설정을 지정한 후에 실행한다 → 위의 오류 해결
.. 생략 ..
<security:http>
<security:form-login/>
</security:http>
<security:authentication-manager>
</security:authentication-manager>
</beans>
참고
코드로 배우는 스프링 웹 프로젝트 개정판 - 구멍가게 코딩단 -
'코드 정리 > Spring Security' 카테고리의 다른 글
6. 로그아웃 (0) | 2023.01.21 |
---|---|
5. 로그인 후 출력할 페이지 설정 (0) | 2023.01.20 |
4. 커스텀 로그인 페이지 (0) | 2023.01.18 |
3. 접근 제한 메시지의 처리 (0) | 2023.01.17 |
2. 접근 제한 설정 및 여러 권한을 가지는 사용자 설정 (0) | 2023.01.16 |