[eGov]전자정부 프레임워크(v3.10.0)와 Spring Security(스프링 시큐리티) 연동
- -
전자정부 스프링 시큐리티 연동
1.pom.xml에서 스프링 시큐리티 라이브러리 설정(호환되는 버전인지 확인 필수)
이 글에서는 v3.10.0 기준 호환되는 스프링 시큐리티 버전 4.2.13.릴리즈로 설정했다.
먼저 properties 태그에 시큐리티 버전을 명시해 준다.
<security.version>4.2.13.RELEASE</security.version>
다음으로 dependency를 설정한다.
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${security.version}</version>
</dependency>
2.context-security.xml 파일 생성 & 설정
스프링 시큐리티 설정을 하는 방법은 Java 클래스, xml 파일 두 가지가 있다. 여기서는 xml 파일 기준으로 설명한다.
위 경로에 xml 파일을 생성 후 아래와 같이 설정해 준다.
<?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:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<http auto-config="true" use-expressions="true">
<session-management invalid-session-url="/">
<!-- 동일 ID의 세션 최대 개수가 한개, 그 이상일 경우는 기존 세션 무효화 -->
<concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
</session-management>
<!-- 권한에 따라 url 접속 제한 설정 -->
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/edu_project/myPage**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" />
<intercept-url pattern="/**" access="permitAll" />
<csrf disabled="true" />
<form-login username-parameter="m_id"
password-parameter="m_pw" login-processing-url="/login"
login-page="/home" default-target-url="/login_success"
authentication-failure-url="/login_fail" />
<remember-me key="remember-key"
token-validity-seconds="604800"
remember-me-parameter="remember-me-param" />
<logout logout-url="/logout" logout-success-url="/logout_After"
invalidate-session="true"
delete-cookies="JSESSIONID,SPRING_SECURITY_REMEMBER_ME_COOKIE" />
</http>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="SELECT m_id AS m_id, m_pw AS m_pw, enabled FROM member WHERE m_id = ?"
authorities-by-username-query="SELECT m_id AS m_id, levels AS levels FROM member WHERE m_id = ?" />
<password-encoder ref="bcryptPasswordEncoder" />
</authentication-provider>
</authentication-manager>
<beans:bean id="bcryptPasswordEncoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
</beans:beans>
이 포스팅은 시큐리티 연동에 관한 내용이므로, 설정 파일의 개념에 대한 내용은 추후 다른 포스팅에서 하고, 일단은 넘어간다.
3.web.xml 파일에서 context-security.xml 파일 경로 확인 및 시큐리티 필터체인 추가
스프링 프레임워크의 경우 web.xml에서 context-security를 설정 파일로 지정하고 파일 경로를 명시해줘야 하는데, 전자정부 디폴트 설정에서는 사진과 같이 이미 spring 폴더의 context-로 시작하는 파일은 모두 설정 파일이라고 설정이 되어있다. 따라서 아까 사진에서 보여준 경로에 파일 이름까지 알맞게 생성했다면 이 부분은 건드리지 않아도 된다.
다음으로 스프링 시큐리티 필터 체인을 추가해 준다.
<!-- Spring Security Filter-->
<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>
4.DB 테이블 설정
유저의 권한(levels)과 계정 활성화(enabled) 상태가 포함된 테이블을 작성해 준다.
create table member(
id varchar2(8) primary key,
pass varchar2(180),
levels varchar2(10),
enabled varchar2(1) default '1');
5.컨트롤러에 login-page, 로그인 성공, 실패, 로그아웃시 실행할 로직 구현
context-security.xml 설정 파일의 form-login 태그에 설정했던 url을 매핑하는 내용을 컨트롤러에서 구현하면 된다.
여기까지 연동 과정이고 설정 파일 설명 및 로그인 기능 구현은 아래 글 참고!
https://hyewonkim1996.tistory.com/21
[Spring Security]로그인 커스터마이징(왕초보)(1) - context-security 뜯어보기, 비밀번호 암호화(BCrypt)
전자정부 표준프레임워크(v3.10.0)로 진행했고, 시큐리티 관련 설정은 이 글 참고 https://hyewonkim1996.tistory.com/19 [eGov]전자정부 프레임워크(v3.10.0)와 Spring Security(스프링 시큐리티) 연동 전자정부 스프
hyewonkim1996.tistory.com
https://hyewonkim1996.tistory.com/22
[Spring Security]로그인 커스터마이징(왕초보)(2) - 로그인, 로그아웃 기능 구현
전자정부 표준프레임워크(v3.10.0)로 진행했고, 시큐리티 관련 설정은 이 글 참고 https://hyewonkim1996.tistory.com/19 [eGov]전자정부 프레임워크(v3.10.0)와 Spring Security(스프링 시큐리티) 연동 전자정부 스프
hyewonkim1996.tistory.com
'🔲 Framework > 🌐 전자정부 표준프레임워크' 카테고리의 다른 글
[eGov]전자정부 표준프레임워크(3.10.0) RESTful API 설정, @RequestBody 오류, GetMapping 404 오류 (0) | 2024.01.29 |
---|---|
[eGov]전자정부 표준프레임워크(v3.10.0) DB 연동(2) - MyBatis로 쿼리 실행 (0) | 2024.01.25 |
[eGov]전자정부 표준프레임워크(v3.10.0) DB 연동(1) - 샘플 프로젝트 생성, 사전 작업 (0) | 2024.01.25 |
[eGov]전자정부 표준프레임워크(v3.10.0) 설치 및 Maven 환경설정 (0) | 2024.01.24 |
소중한 공감 감사합니다