#스프링 시큐리티
어플리케이션에서 보안 기능을 구현하는 데 사용되는 프레임워크(디자인패턴+라이브러리)
필터기반으로 동작하므로 스프링 MVC와 분리되어 동작한다.
#주요 기능
인증(Authentication) : 사용자의 정당성 확인(로그인)
인가(Authorization) : 리소스나 처리에 대한 접근 제어(권한)
#제공 기능
- 세션관리
- 로그인처리
- 암호화 처리
- 자동 로그인
- CSRF 토큰 처리
※ CSRF ( Cross-site request forgery) 토큰 처리 : 웹 사이트 취약점 공격을 막아준다.
사용자가 자신도 모르게 공격자가 의도한 행위 (수정,삭제,등록)을
특정 웹 사이트에 요청하게 하는 공격
#웹화면 접근 정책
Security 설정을 통하여 특정 URL에 대한 접근을 제한함
1 . 회원게시판(board)
- 목록(list) : 모든이 접근 가능
- 등록(register) : 로그인한 회원만 접근 가능
2. 관리자게시판(notice)
- 목록(list) : 모든이 접근 가능
- 등록(register) : 로그인한 관리자만 접근 가능
# 시큐리티 실전
#pom.xml
설치할 라이브러리 목록 5.0.7버전으로 통일
spring-security-web
spring-security-config
spring-security-core
spring-security-taglibs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<!-- Spring-Security-Web -->
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- Spring Security Config -->
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- Spring Security Core -->
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- 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.7.RELEASE</version>
</dependency>
|
cs |
#web.xml
web.xml 에서 security 설정을 위한 xml문의 위치(/WEB-INF/spring/security-context.xml)를 지정한다.
1
2
3
4
5
6
7
8
9
|
<!-- contextConfigLocation에 스프링 시큐리티 설정 파일을 지정 -->
<!-- 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>
|
cs |
#security-context.xml
security의 상세 설정을 세팅한다.
밑의 코딩문에서는 security:user를 통하여 권한을 부여할 역활이름과 암호, 그리고
해당 권한을 갖고 security:intercept-url에서 로그인화면을 조정할수있다.
Security:intercept-url | |
pattern | url의 위치 |
access | permitAll (모두입장가능) hasRole(name) name만 입장가능 |
security:user | |
name | 이름(ID)설정 |
password | 암호설정 |
authorities | 권한부여 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<?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>
<!--
URI 패턴으로 접근 제한을 설정함
pattern: url 요청경로 access
aceess : 접근 제한 정책 (permitAll-모든이에게 열려있음)
-->
<security:intercept-url pattern="/board/list" access="permitAll"/>
<security:intercept-url pattern="/board/register" access="hasRole('ROLE_MEMBER')"/>
<security:intercept-url pattern="/notice/list" access="permitAll"/>
<security:intercept-url pattern="/notice/register" access="hasRole('ROLE_ADMIN')"/>
<!--
폼 기능 인증 기능 사용
접근 제한에 걸리면 스프링 시큐리티가 기본적으로 제공하는 로그인 페이지로 이동함
-->
<security:form-login/>
</security:http>
<security:authentication-manager>
<!--
지정된 아이디와 패스워드로 로그인이 가능하도록 설정함
authorities(허가설정)
member사용자 : /board/list, /board/register, /notice/list
admin사용자 : /board/list, /board/register, /notice/list , /notice/register
스프링 시큐리티 5버전부터는 패스워드 암호화 처리기를 반드시 이용하도록 변경이 되있음
암호화 처리기를 사용하지 않도록 "{noop}" 문자열을 비밀번호 앞에 사용함
-->
<security:authentication-provider>
<security:user-service>
<security:user name="member" password="{noop}1234" authorities="ROLE_MEMBER"/>
<security:user name="admin" password="{noop}abcd" authorities="ROLE_ADMIN"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
|
cs |
#테스트
pattern = "/board/list" access = "permitAll" 인 상태에서는 모두 입장 가능한 상태이기때문에
설정해둔 jsp문은 출력된다. 하지만
pattern="/board/register" access="hasRole('ROLE_MEMBER')"은 회원 상태에서 입장이 가능하기때문에
다음과 같이 아이디와 비밀번호를 넣도록 나온다.
마찬가지로 admin 으로 걸려있는 jsp문도
admin 아이디와 비밀번호를 넣지않으면 입장이 불가능하게된다.
'Coding story > go Spring,JSP' 카테고리의 다른 글
다음 주소창을 이용한 주소 입력/datepicker를 통한 날짜입력 (0) | 2022.02.17 |
---|---|
(스프링) 세션을 통한 로그인 (0) | 2022.02.15 |
#Spring(4) - JDBC연동 준비 (0) | 2022.02.03 |
인터넷 서점 만들어보기(JSP/Java)- 3.신간 추가 기능 / 관리자 로그인 (0) | 2022.02.03 |
#Spring(3) - 스프링의 예제로 본 동작 원리 (0) | 2022.01.29 |
최근댓글