개발 블로그
[eGov] 스프링 시큐리티 기본 설정 본문

프로젝트에 DB가 연결이 된 상태이다
1. 의존성 등록(pom.xml)
<!-- Spring Security 사용을 위한 dependency 등록 Start -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<!-- Spring Security 사용을 위한 dependency 등록 End -->
<!-- JSP에서 Spring Security Tag 사용을 위한 dependency 등록 Start -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<!-- JSP에서 Spring Security Tag 사용을 위한 dependency 등록 Start -->
2. 필터 등록 (web.xml)
스프링 시큐리티는 서블릿 필터로 동작한다
<!-- Spring Security -->
<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>
web.xml 제일 앞에 추가해놓는다
web.xml 파일에 보면
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
이 설정이 있는데 이거 지워주기
2. 스프링 시큐리티 설정파일을 로딩하도록 지정 (web.xml)
스프링 시큐리티에 사용되는 빈들을 위한 설정파일을 스프링 프레임웍이 읽어들이도록 설정 파일 지정을 추가
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:egovframework/spring/context-*.xml
</param-value>
</context-param>
전자 정부 프레임워크라 root-context.xml이 따로 없고
기본 설정으로 context 파일을 읽는 것이 있길래 별 다르게 추가 할 것 없이
기본설정대로 스프링 시큐리티 파일을 만들기로 함
3. 스프링 시큐리티 기본 설정 (security-context.xml)
[resources] - [spring] 에서 오른쪽 클릭 후 New 에 Spring Bean Configuration File을 클릭한다




자동으로 생성이 된다 근데 이거 그대로 스프링 시큐리티 기본 설정을 추가하게 되면
<http auto-config='true'>
<intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="1234" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="guest" password="1234" authorities="ROLE_USER" />
<user name="guest2" password="a1234" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
http 태그 밑에 계속 빨간 줄이 생기는 오류가 난다 ㅡㅡ
그래서 저번에 했던 프로젝트랑 다른 점을 비교해서 수정해줬다

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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-4.2.xsd">
<http auto-config='true'>
<intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="1234" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="guest" password="1234" authorities="ROLE_USER" />
<user name="guest2" password="a1234" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
4. TEST

컨트롤러, jsp 아무 설정을 하지 않았는데도 서버를 실행하면 로그인 화면이 뜬다
<http auto-config='true'>
<intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />
</http>
이 설정에따라 url 패턴이 뭐가 되든 'ROLE_ADMIN' 계정이어야 access 할 수 있기 때문이다
하드코딩이 되어있는 대로 admin 계정에 로그인 하면






'전자 정부 프레임워크' 카테고리의 다른 글
[eGov] MVC 파일 구조 변경하고 그에 따른 설정도 변경 (0) | 2023.03.31 |
---|---|
[eGov] 스프링 시큐리티 DB와 연결하기 (0) | 2023.03.31 |
[eGov] eGovFrameWebProject sample 삭제 (0) | 2023.03.30 |
[eGov] MVC - Service, Mapper (0) | 2023.03.30 |
[eGov] Oracle / Mybatis DB 연결 (0) | 2023.03.28 |