: 시큐리티 설정(security-context.xml)을 통해서 특정 URI에 대한 접근을 제한함
1) 회원 게시판(board)
2) 공지사항 게시판(notice)
1) 지정된 형식으로 테이블을 생성해야 함
2) USERS(사용자 관리), AUTHORITIES(권한 관리)
<!-- 스프링 시큐리티 라이브러리 의존관계 정의 시작 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.7.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.7.RELEASE</version>
</dependency>
<!-- 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>
<!-- 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>
<!-- 스프링 시큐리티 라이브러리 의존관계 정의 끝 -->
web.xml에 노랭이와 필터 추가
<!-- 서블릿 필터 클래스를 서블릿 컨테이너에 등록함 (필터 쓰자~)-->
<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>
security-context.xml 작성하기
<?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-4.3.xsd">
<security:http>
<!--
URI 패턴으로 접근 제한을 설정함
pattern : url 요청 경로
access : 접근 제한 정책
-->
<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 : ROLE_ADMIN, ROLE_MEMBER -> 위에있는 이름에 권한을 가진것(access값)
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>
BoardController
package kr.or.ddit.board;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/board")
@Controller
public class BoardController {
private static final Logger logger =
LoggerFactory.getLogger(BoardController.class);
@RequestMapping("/list")
public void list() {
logger.info("list:access to all");
}
@RequestMapping("register")
public void registerForm() {
logger.info("registerForm : access to member");
}
}
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h3>BOARD LIST : access to all</h3>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>BOARD > REGISTER</title>
</head>
<body>
<h3>BOARD REGISTER : access to member</h3>
</body>
</html>
NoticeController
package kr.or.ddit.notice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/notice")
@Controller
public class NoticeController {
private static final Logger logger =
LoggerFactory.getLogger(NoticeController.class);
/*
1. void
- 호출하는 URL과 동일한 뷰 이름을 나타냄
- 요청이 /notice/list이면, 뷰는 /notice/list.jsp를 가리킴
*/
@RequestMapping("/list")
public void list() {
logger.info("list : access to all");
//return "/board/list";
}
@RequestMapping("/register")
public void registerForm() {
logger.info("registerForm : access to all");
}
}
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>NOTICE > LIST</title>
</head>
<body>
<h3>NOTICE LIST : access to all</h3>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>NOTICE > REGISTER</title>
</head>
<body>
<h3>NOTICE REGISTER : access to admin</h3>
</body>
</html>