1. 시큐리티의 개요
시큐리티란 ?
허가된 사용자만이 특정 웹 페이지에 접근할 수 있도록 제한하는 보안 기능
사용자가 권한이 없는 데이터에 접근하는 것을 막거나 웹 공격자가 전송데이터를 중간에 가로채는 것을 방지하는 등 중요한 역할
인증(authentication)
- 사용자가 웹 브라우저를 사용하여 웹 페이지에 접근할 때 JSP 컨테이너는 요청된 페이지에 보안 제약이 있는지 확인하고 사용자에게 사용자의
이름과 암호를 확인하여 수행
권한 부여(authorization)
- 특정 사용자가 해당 페이지에 접근할 수 있는지 확인하여 승인
시큐리티 처리 방법

웹 서버에 역할과 사용자 구성하기
/설치된 톰캣의 루트/conf/ 폴더 내의 tomcat-users.xml 파일
- 2개의 역할 ❶ tomcat, ❷ role1을 가지고, 3개의 사용자 ❸ tomcat, ❹ both, ❺ role1이 서로 다른 역할에 매핑


예제 [기존 인증 정보와 새로운 인증 정보를 추가하는 예시]
Servers → tomcat-users.xml → role 부분 password="java" 로수정하기
![]() |
![]() |
2. 선언적 시큐리티 처리
시큐리티 역할 설정하기
<security-role>은 웹 애플리케이션에 사용하는 역할을 나열하는 요소
web.xml 파일에 구성


예시
![]() |
![]() |
시큐리티 제약 사항 설정하기
사용자의 요청 URL에 대한 접근 권한을 정의하는데 사용
web.xml 파일에 접근 권한 내용을 구성


<web-resource-collection> 요소
- 웹 자원에 대한 접근을 설정하는 요소
- <url-pattern>, <http-method>, <webresource-name> 등의 하위 요소로 구성



<auth-constraint> 요소
권한이 부여된 사용자만이 웹 자원에 접근할 수 있도록 이름을 설정하는 요소로 형식은 다음과 같음
- <auth-constraint> 요소에는 <web-resource-collection> 요소의 <urlpattern>과 <http-method>에 설정된 경로에 접근할 수 있는 권한이 부여된 사용자의 이름을 지정
- <auth-constraint> 요소를 생략하면 웹 서버는 사용자 인증을 요구하지 않고 사용자의 요청을 승인



<user-data-constraint> 요소
- 클라이언트와 서버 간에 데이터를 전송할 때 데이터를 보호하는 방법을 설정하는 요소



예시
![]() |
![]() |
![]() |
시큐리티 인증 설정하기
인증 처리를 위한 로그인 페이지나 오류 페이지를 호출하는 데 사용
web.xml 파일에 인증 관련 내용을 구성


<auth-method> 요소
- 웹 애플리케이션의 인증 처리 기법을 설정하는 요소
- 인증 처리 기법은 BASIC, DIGEST, FORM, CLIENT-CERT 등으로 이 중 하나를 <auth-method> 요소에 설정






<realm-name> 요소
- 기본 인증의 영역 이름을 설정하는 요소
<realm-name> 요소에 설정된 영역 이름은 대개 웹 브라우저의 로그인 대화상자에 표시
<realm-name> 요소는 FORM 기반 인증이나 다른 인증 방법에 필요하지 않기 때문에 아무런 영향을 미치지 않지만, <login-config> 요소에 대한 설명 속성이 없으므로 데이터를 문서화하는 데 일반적으로 사용


<form-login-config> 요소
- 인증 처리를 위한 로그인 및 오류 페이지를 설정하는 요소
- <auth-method> 요소가 FORM 기반 인증 처리 기법으로 설정되었을 때 사용
- <form-loginpage>, <form-error-page> 등의 하위 요소로 구성됨
- 로그인 및 오류 페이지의 경로는 웹 애플리케이션 이름(이클립스에서 프로젝트 이름에 해당됨)을 기준으로 설정



예시
![]() |
![]() |
![]() |
전체 코드
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>JSPBook</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 선언적 시큐리티 처리 : web.xml 파일에
보안 구성(역할-role, 사용자-username, URL경로, 인증 처리)을 작성
- 역할/사용자 : tomcat-users.xml에 등록된 역할/사용자
web.xml : 웹 애플리케이션 배포 설명자 파일(톰켓설정)
-->
<!-- 시큐리티 역할(role) 설정 시작 -->
<security-role>
<role-name>tomcat</role-name>
</security-role>
<security-role>
<role-name>role1</role-name>
</security-role>
<!-- 시큐리티 역할(role) 설정 끝 -->
<!-- 시큐리티 제약 사항(constraint)을 설정 시작 -->
<security-constraint>
<!-- 웹 자원에 대한 접근 설정 -->
<web-resource-collection>
<!-- 웹자원의 이름 설정(생략 가능) -->
<web-resource-name>JSPBook</web-resource-name>
<!-- 접근 제한 요청 URL -->
<url-pattern>/ch10/security01.jsp</url-pattern>
<!-- HTTP 메소드(get, post) -->
<http-method>GET</http-method>
</web-resource-collection>
<!-- 권한이 부여된 사용자 만이 웹자원에 접근 할 수 있도록 설정 -->
<auth-constraint>
<description></description>
<role-name>role1</role-name>
</auth-constraint>
</security-constraint>
<!-- 시큐리티 제약 사항(constraint)을 설정 끝 -->
<!-- 시큐리티 제약 사항(constraint)을 설정 시작 -->
<security-constraint>
<!-- 웹 자원에 대한 접근 설정 -->
<web-resource-collection>
<!-- 웹자원의 이름 설정(생략 가능) -->
<web-resource-name>JSPBook</web-resource-name>
<!-- 접근 제한 요청 URL -->
<url-pattern>/ch10/security02.jsp</url-pattern>
<!-- HTTP 메소드(get, post) -->
<http-method>GET</http-method>
</web-resource-collection>
<!-- 권한이 부여된 사용자 만이 웹자원에 접근 할 수 있도록 설정 -->
<auth-constraint>
<description></description>
<role-name>tomcat</role-name>
</auth-constraint>
</security-constraint>
<!-- 시큐리티 제약 사항(constraint)을 설정 끝 -->
<!-- 인증(authentication) 처리를 위한 로드인 및 로그인 실패 페이지 설정 시작 -->
<login-config>
<!-- 웹 자원에 대한 인증 처리 방식 BASIC(간단, 일반) / DIGEST(암호화) /
FORM(사용자 폼 페이지) / CLIENT-CERT(공인 키 인증) -->
<!-- <auth-method>BASIC</auth-method> -->
<auth-method>FORM</auth-method>
<!-- 인증(로그인) 처리를 위한 로그인 및 오류페이지를 설정 -->
<form-login-config>
<!-- 인증(로그인) 처리를 위한 로그인 페이지 설정 -->
<form-login-page>/ch10/login.jsp</form-login-page>
<!-- 인증(로그인) 실패 시 오류페이지를 설정 -->
<form-error-page>/ch10/login_failed.jsp</form-error-page>
</form-login-config>
</login-config>
<!-- 인증(authentication) 처리를 위한 로드인 및 로그인 실패 페이지 설정 끝 -->
</web-app>
security01.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Security</title>
</head>
<body>
<h3>인증 성공!</h3>
</body>
</html>
security02.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h3>인증 성공 02! 프로그래밍적 시큐리티 처리</h3>
<!-- security를 통해서 로그인한 사용자의 아이디 -->
<p>사용자명 : <%=request.getRemoteUser() %></p>
<!-- web.xml의 <auth-method>FORM</auth-method> -->
<P>인증방법 : <%=request.getAuthType()%></P>
<p>인증한 사용자명이 role(역할) "tomcat"에 속하는 사용자 인가요?
<!-- Is that your book? -->
<%=request.isUserInRole("tomcat") %>
</p>
<p>인증한 사용자명이 role(역할) "role1"에 속하는 사용자 인가요?
<!-- Is that your book? -->
<%=request.isUserInRole("role1") %>
</p>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Security</title>
</head>
<body>
<!-- 로그인 인증 처리를 위한 Form페이지 -->
<!-- 폼기반 인증을 처리하도록 action속성의 값을 j_security_check로 작성 -->
<form name="loginForm" action="j_security_check" method="post">
<!-- name속성의 값인 j_username은 변경할 수 없음 -->
<p>아이디 : <input type="text" name="j_username"
placeholder="아이디" required/></p>
<!-- name속성의 값인 j_password은 변경할 수 없음 -->
<p>비밀번호 : <input type="password" name="j_password"
placeholder="비밀번호" required/></p>
<p><input type="submit" value="전송"/></p>
</form>
</body>
</html>
login_failed.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Security</title>
</head>
<body>
<h3>인증 실패했습니다.</h3>
<a href="/ch10/security01.jsp">로그인</a>
</body>
</html>
3. 프로그래밍적 시큐리티 처리
프로그래밍적 시큐리티(programmatic security)
- 웹 애플리케이션의 보안을 위해 코드를 작성하여 사용자의 권한 부여를 처리하는 방식
- 선언적 시큐리티의 보안으로 충분하지 않을때 request 내장 객체의 메소드를 사용하여 사용자를 승인하는 방법


참고 파일
'JSP 웹 프로그래밍 > 수업내용' 카테고리의 다른 글
| [JSP 웹 프로그래밍] 예외처리 (0) | 2024.07.12 |
|---|---|
| [JSP 웹 프로그래밍] 예외처리 (0) | 2024.07.12 |
| [JSP 웹 프로그래밍] 다국어 처리 (0) | 2024.07.10 |
| [JSP 웹 프로그래밍] 유효성 검사 (0) | 2024.07.08 |
| [JSP 웹 프로그래밍] 파일 업로드(file upload) (0) | 2024.07.04 |









