본문 바로가기

Spring/Spring Security

[Spring Security] 1. Spring Web Security 개념과 설정

728x90
반응형

Spring Web Security란?

일반적인 공격에 대한 인증, 권한 부여 및 보호를 제공하는 프레임 워크

명령형 애플리케이션과 반응형 애플리케이션을 모두 지원하는 이 클래스는 스프링 기반 애플리케이션을 보호하기 위한 사실상의 표준이라고 시큐리티 레퍼런스 문서에서 설명하고 있다.

 

간단히 말해 스프링 프레임워크의 하위 프로젝트로, 웹 보안과 관련된 프레임워크라고 할 수 있음

                  → 보안 관련 다양한 옵션들을 제공해 편리하게 사용 가능

 

설정

pom.xml

		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-web</artifactId>
			<version>5.2.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-config</artifactId>
			<version>5.2.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-core</artifactId>
			<version>5.2.5.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.2.5.RELEASE</version>
		</dependency>

이 때, 스프링 시큐리티의 버전은 스프링 버전보다 낮아야 한다.

스프링 설정은 5.2.6이기 때문에 스프링 시큐리티는 그보다 낮은 버전인 5.2.5로 설정했다.

 

스프링 시큐리티의 버전은 아래에서 확인할 수 있다.

https://spring.io/projects/spring-security#learn

 

Spring Security

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements

spring.io

 

WEB-INF/spring/security-context.xml

xml 생성하는 방법

- Namespaces - security 체크

- 네임스페이스 선택으로 생성된 코드 수정 (spring-security 버전 삭제)

선택된 부분이 버전이다. 삭제하자

- 아래 코드 추가

<security:http>
	<security:form-login />
</security:http>

<security:authentication-manager>

</security:authentication-manager>

 

web.xml

- 아래 체크한 부분을 추가한다

 

src/main/java/(__.__.controller 패키지)/SampleController.java

package com.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import lombok.extern.log4j.Log4j;

@Log4j
@RequestMapping("/sample/*")
@Controller
public class SampleController {

	@GetMapping("/all")
	public void doAll() {
		log.info("do all can access everybody");
	}
	
	@GetMapping("/member")
	public void doMember() {
		log.info("logined member");
	}
	
	@GetMapping("/admin")
	public void doAdmin() {
		log.info("admin only");
	}
}

스프링 시큐리티에 의해 제어가 필요한 URI를 설계하고 적용한 것이다.

 all : 로그인 하지 않은 사용자도 접근 가능

 member : 로그인 한 사용자들만 접근 가능

 admin : 로그인 한 사용자들 중 관리자 권한을 가진 사용자만 접근 가능

 

WEB-INF/views/sample/all.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- all or member or admin -->
<h1>/sample/all page</h1>
</body>
</html>
728x90
반응형