romworld

JSP 19 - 시큐리티(security) 본문

WEB/JSP

JSP 19 - 시큐리티(security)

inderrom 2023. 1. 11. 12:48

시큐리티

허가된 사용자만 특정 웹 페이지에 접근할 수 있도록 제안하는 보안 기능

- 인증 (authentication)

  • 아이디와 비밀번호가 맞는지 확인

- 권한 부여

  • 특정 사용자가 해당 페이지에 접근할 수 있는지 확인하여 승인

Servers - Tomcat  v8.5 -  <tomcat-users.xml> 맨 밑으로 내리면 주석 처리되어 있다. 풀고 추가해주면 된다.

<!--  시큐리티
1) 인증(Authentication) : 아이디/비밀번호가 맞니? ex) 아이디/비번이 맞아야 로그인 가능
2) 인가(Authorization) : 그 메뉴에 들어갈 수 있니?(권한) ex) 관리자 아이디어야 관리자 메뉴에 접근 가능
 -->
 <!--  role : 권한/역할 
 	권한(role)   |	 tomcat       | 	role1     | manager
 	//////////////////////////////////////////////////
 	사용자(user) |   tomcat,both  | both, role1	   | admin
 	
 -->
<role rolename="tomcat"/><!-- 관리자 -->
  <role rolename="role1"/><!-- 교수 -->
  <role rolename="manager"/><!-- 학생 -->
  <user username="tomcat" password="java" roles="tomcat"/><!-- 관리자 -->
  <user username="both" password="java" roles="tomcat,role1"/><!-- 관리자,교수 -->
  <user username="role1" password="java" roles="role1"/><!-- 교수 -->
  <user username="admin" password="java" roles="manager"/><!-- 학생 -->
</tomcat-users>

 

프로젝트내

WEB-INF - <web.xml>

안에 security 권한과 제약사항을 설정해준다.

<?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_3_1.xsd" id="WebApp_ID" version="3.1">
  <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>
  <!-- role : 권한, tomcat-users.xml에서 등록한 role만 사용 가능 -->
  <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-role>
  	<role-name>tomcat</role-name>
  </security-role>
  <security-role>
  	<role-name>manager</role-name>
  </security-role>

  <!-- constraint : 제약사항(약속) -->
  <security-constraint>
  	<!-- 웹 자원(프로젝트명, 접근제한을 할 대상 URL)에 대한 약속 목록 -->
  	<web-resource-collection>
  		<web-resource-name>JSPBook</web-resource-name>
  		<url-pattern>/ch10/security01.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<!-- 웹 자원에 권한이 부여된 사용자만이 접근할 수 있도록 설정-->
  	<auth-constraint>
  		<description></description>
  		<!-- 
  		role1 권한을 가진 role1, both 사용자의 경우
  		아이디(both)와 비밀번호(java)를 맞게 입력했다면 
  		/ch10/security01.jsp URL로 접근 가능.
  		반드시 tomcat-users.xml에 등록된 role(역할/권한)과 사용자여야 함
  		만약 모든 사용자에게 권한을 부여하려면 *로 표시
  		 -->
  		<role-name>role1</role-name>
  	</auth-constraint>
  	<!-- 
  	인증(authentication) : 로그인
  	인가(authrization) : 권한(로그인 된 상태)
  	인증 : 인가 = 1 : N
  	
  	* 관계차수
  	- 1:1
  	- 1:N 90%이상 차지
  	- N:M (다대다의 관계는 RDB에서는 구현 불가. 1:N으로 해소)
  	 -->
  </security-constraint>
  <!-- ch09/모든.jsp에 접근하려면
  	tomcat 권한이 있어야함
  	tomcat 권한을 갖고 있는 user는 tomcat, borh
   -->
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBook</web-resource-name>
  		<url-pattern>/ch09/*</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  
  		<role-name>tomcat</role-name>
  	</auth-constraint>
  </security-constraint>  
  
 <!-- login-config 
  인증(authentication) 처리를 위한 로그인 페이지나 오류 페이지를 호출하는 데 사용함
  로그인 관련 메시지를 표시할 수 있음
   -->
  <login-config>
  <!--  로그인 페이지를 기본으로 제공 -->
<!--   	<auth-method>BASIC</auth-method> -->
		<auth-method>FORM</auth-method>
		<!-- 사용자 정의 FORM 인증 처리 기법 설정  -->
		<form-login-config>
			<!-- 로그인 페이지 -->
			<form-login-page>/login.jsp</form-login-page>
			<!-- 로그인 실패 시 보여줄 페이지 -->
			<form-error-page>/login_failed.jsp</form-error-page>
		</form-login-config>
  </login-config>
</web-app>

 

권한을 추가할 때는 

 

<security-role>
  	<role-name>role1</role-name>
  </security-role>

<security-role>태그에 꼭 감싸주고

 

<security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBook</web-resource-name>
  		<url-pattern>/ch09/*</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  
  		<role-name>tomcat</role-name>
  	</auth-constraint>
  </security-constraint>

<security-constraint>태그를 써준다.

 


 

로그인을 성공했을 때

<login.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>Security</title>
</head>
<body>
	<!-- /ch01/security01.jsp에 접근 시 통과해야 할 관문   -->
	<form name="loginForm" action="j_security_check" method="post">
		<p>사용자명 : <input type="text" name="j_username"/></p>
		<p>비밀번호 : <input type="password" name="j_password"/></p>
		<p> <input type="submit" value="로그인"/></p>
	</form>
</body>
</html>

username=role1 pass=java

<security01.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>Security</title>
</head>
<body>
	<p>인증 성공했습니다.</p>
	<!--  사용자명 : role1 -->
	<p>사용자명 : <%=request.getRemoteUser() %></p>
	<!-- 인증방법 : FORM -->
	<!-- <auth-method>FORM</auth-method> -->
	<p>인증방법 : <%=request.getAuthType() %></p>
	<!-- 로그인 한 사용자가 role1이라는 rol1(권한)에 속해있는지 체킹 
		true/false로 return됨
		
		역할명 체킹 : true
	-->
	<p>role1 역할명 체크  : <%=request.isUserInRole("role1") %></p>
	<p>tomcat 역할명 체크  : <%=request.isUserInRole("tomcat") %></p>
	<p>manager 역할명 체크  : <%=request.isUserInRole("manager") %></p>
</body>
</html>

로그인을 실패했을 때

<login_failed.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>Security</title>
</head>
<body>
	<p>인증 실패했습니다.</p>
</body>
</html>

 

Comments