JSP 13강 - 다국어, bundle, 시큐리티

voilà!·2022년 1월 23일
0

JSP 스프링

목록 보기
13/31

다국어 지원

1. 페이지에 태그 라이브러리 추가

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

2. body 태그에 아래와 같이 추가
1) fmt:setLocale 설정

<!-- ko는 한글, en은 영어 -->
	<fmt:setLocale value="${param.language}"/>
	<fmt:bundle basename="resourceBundle.message">
<!-- 패키지명.파일명 으로 접근한다(ko는 resourceBundle.properties)-->

2)${param.language} 속성을 사용해서 Korean 클릭 시 languae의 값으로 ko를, English 클릭시 en을 파라미터로 보낸다.

<div class="text-right">
	<a href="?language=ko">Korean</a>|<a href="?language=en">English</a>
	<a href="logout.jsp" class="btn btn-sm btn-success pull-right">logout</a>
</div>

3. resourceBundle 패키지 및 properties 파일 생성
bundle : 꾸러미

message.properties

<!--순서는 key = value -->
title=\uC0C1\uD488 \uB4F1\uB85D
productId=\uC0C1\uD488 \uCF54\uB4DC
pname=\uC0C1\uD488\uBA85
unitPrice=\uAC00\uACA9
description=\uC0C1\uC138 \uC124\uBA85
manufacturer=\uC81C\uC870\uC0AC
category=\uBD84\uB958
unitsInStock=\uC7AC\uACE0\uC218
condition=\uC0C1\uD0DC
productImage=\uC774\uBBF8\uC9C0
condition_New=\uC2E0\uADDC \uC81C\uD488
condition_Old=\uC911\uACE0 \uC81C\uD488
condition_Refurbished=\uC7AC\uC0DD \uC81C\uD488
button=\uB4F1\uB85D

message_en.properties

<!--순서는 key = value -->
title=Product Addition
productId=Product ID
pname=Name
unitPrice=Unit Price
description=Description
manufacturer=Manufacturer
category=Category
unitsInStock=Units in Stock
condition=Condition
productImage=Imange
condition_New=New
condition_Old=Old
condition_Refurbished=Refurbished
button=Insert
  1. 해당 값에 fmt:message 설정
<div class="form-group row">
	<label class="col-sm-2"><fmt:message key="category" /></label>
	<div class="col-sm-3">
		<input type="text" name="category" class="form-control" />
	</div>
</div>

addProduct.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="/js/validation.js"></script>
<title>상품 등록</title>
</head>
<body>
<!-- ko는 한글, en은 영어 -->
	<fmt:setLocale value="${param.language}"/>
	<fmt:bundle basename="resourceBundle.message">
<!-- 패키지명.파일명 으로 접근한다(ko는 resourceBundle.properties)-->

<!-- top 인클루드 시작 -->
	<jsp:include page="/ch03/top.jsp" />
<!-- top 인클루드 끝 -->

<!-- content 시작 -->
	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3"><fmt:message key="title" /></h1>
			<!-- key에는  message.properties에서 적어놓은 속성들을 적는다-->
		</div>
	</div>
	<div class="container">
		<div class="text-right">
			<a href="?language=ko">Korean</a>|<a href="?language=en">English</a>
			<a href="logout.jsp" class="btn btn-sm btn-success pull-right">logout</a>
		</div>
	<!-- addProduct_process.jsp : common-fileupload -->
	<!-- addProduct_process2.jsp : cos.jar -->
		<form name="newProduct" action="addProduct_process2.jsp" 
			method="post" class="form-horizontal" enctype="multipart/form-data">
			<div class="form-group row">
				<label class="col-sm-2"><fmt:message key="productId" /></label>
				<div class="col-sm-3">
					<input type="text" name="productId" id="productId"
						class="form-control" />
				</div>
			</div>
			<div class="form-group row">
				<label class="col-sm-2"><fmt:message key="pname" /></label>
				<div class="col-sm-3">
					<input type="text" name="pname" id="pname"
						class="form-control" />
				</div>
			</div>
			<div class="form-group row">
				<label class="col-sm-2"><fmt:message key="unitPrice" /></label>
				<div class="col-sm-3">
					<input type="text" name="uniPrice" id="uniPrice"
						class="form-control" />
				</div>
			</div>
			<div class="form-group row">
				<label class="col-sm-2"><fmt:message key="description" /></label>
				<div class="col-sm-5">
					<textarea name="description" rows="2" cols="50" class="form-control"></textarea>
				</div>
			</div>
			<div class="form-group row">
				<label class="col-sm-2"><fmt:message key="manufacturer" /></label>
				<div class="col-sm-3">
					<input type="text" name="manufacturer"
						class="form-control" />
				</div>
			</div>
			<div class="form-group row">
				<label class="col-sm-2"><fmt:message key="category" /></label>
				<div class="col-sm-3">
					<input type="text" name="category"
						class="form-control" />
				</div>
			</div>
			<div class="form-group row">
				<label class="col-sm-2"><fmt:message key="unitsInStock" /></label>
				<div class="col-sm-3">
					<input type="text" name="unitsInStock" id="unitsInStock"
						class="form-control" />
				</div>
			</div>
			<div class="form-group row">
				<label class="col-sm-2"><fmt:message key="condition" /></label>
				<div class="col-sm-5">
					<input type="radio" name="condition" id="condition1" value="new" />
						<label for="condition1"><fmt:message key="condition_New" /></label>
					<input type="radio" name="condition" id="condition2" value="old" />
						<label for="condition2"><fmt:message key="condition_Old" /></label>
					<input type="radio" name="condition" id="condition3" value="Refurbished" />
						<label for="condition3"><fmt:message key="condition_Refurbished" /></label>
				</div>
			</div>
<!-- 			<div class="form-group row">
				<label class="col-sm-2">이미지</label>
				<div class="col-sm-5">
					<input type="file" name="productImage" class="form-control" />
				</div>
			</div> -->
			<div class="form-group row">
				<label class="col-sm-2"><fmt:message key="productImage" /></label>
				<div class="col-sm-5">
					<input type="file" name="productImage2" class="form-control" />
				</div>
			</div>
			<div class="form-group row">
				<div class="col-sm-offset-2 col-sm-10">
					<input type="button" class="btn btn-primary" value="<fmt:message key="button" />"/>
				</div>
			
			</div>
		</form>	
	</div>
<!-- content 끝 -->
<script type="text/javascript">
CKEDITOR.replace("description");
</script>

<!-- bottom 인클루드 시작 -->
	<jsp:include page="/ch03/bottom.jsp" />
<!-- bottom 인클루드 끝 -->
</fmt:bundle>
</body>
</html>

시큐리티

  1. 시큐리티란?
  • 허가된 사용자만이 접근할 수 있도록 제한하는 보안 기능
  1. 프로세스?
  • 사용자가 웹 페이지에 접근
  • JSP 컨테이너(Tomcat)는 요청된 페이지에 보안 제약(Constraints)이 있는지 확인
  • 사용자에게 인증(authentication)을 요청
  • 권한 부여(authorization) : JSP 컨테이너는 사용자가 해당 페이지에 접근할 수 있는지 확인. 승인
  1. 역할?
  • 사용자가 권한이 없는 데이터에 접근하는 것을 막음
  • 스니핑(sniffing) : 웹 공격자가 전송 데이터를 중간에 가로채는 것 방지
  1. 그래서 시큐리티란?
  • 허가된 사용자만이 특정 웹 페이지에 접근할 수 있도록 제한하는 보안 기능
  • 사용자가 권한이 없는 데이터에 접근하는 것을 막아줌
  • 웹 공격자가 전송 데이터를 중간에 가로채는 것을 방지(스니핑 방지)
  1. 선언적 시큐리티
  • web.xml 파일(웹 어플리케이션(JSPBook)) 배포 설명자)에 보안 구성을 작성하여 수행
  • 보안 역할(role), 보안 제약 사항(constraint), 인증 처리(login.jsp, login-failed.jsp) 설정

1) tomcat-users.xml 설정
34번째 줄<role>부터 주석 제거
<role rolename> 은 권한
<user username> 은 사용자

  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat1234" roles="tomcat"/>
  <user username="both" password="both1234" roles="tomcat,role1"/>
  <user username="role1" password="role1234" roles="role1"/>

2) JSPBook web.xml 설정
마지막줄에 추가

 	<!-- 시큐리티 admin 관리 시작 -->
<!-- 	<security-role>
		<description></description>
		<role-name>admin</role-name>
	</security-role> -->
	<!-- 시큐리티 제약 사항 설정 -->
	<security-constraint>
		<web-resource-collection>
			<web-resource-name>JSPBook</web-resource-name>
			<!-- 접근을 제한할 요청 경로 -->
			<url-pattern>/ch04/addProduct.jsp</url-pattern>
			<http-method>GET</http-method>
		</web-resource-collection>
		<auth-constraint>
			<description></description>
			<!-- 권한이 부여된 role 이름 -->
			<role-name>admin</role-name>
			<!-- 위의 admin과 url-pattern을 admin권한을 가진 사용자만 이용할 수 있도록 매핑 -->
		</auth-constraint>
	</security-constraint>
	<security-constraint>
		<web-resource-collection>
			<web-resource-name>JSPBook</web-resource-name>
			<!-- 접근을 제한할 요청 경로  -->
			<url-pattern>/ch03/*</url-pattern>
			<http-method>GET</http-method>
		</web-resource-collection>
		<auth-constraint>
			<description></description>
			<!-- 권한이 부여된 role 이름 -->
			<role-name>tomcat</role-name>
		</auth-constraint>
	</security-constraint>
	<!-- 시큐리티 인증 설정 -->
	<login-config>
		<!-- BASIC 인증 처리 기법으로 설정 -->
		<!-- <auth-method>BASIC</auth-method> -->
		<!-- FORM 기반 인증 처리 기법으로 설정 -->
		<auth-method>FORM</auth-method>
		<form-login-config>
			<!-- 인증 처리를 위한 로그인 페이지 설정 -->
			<form-login-page>/ch04/login.jsp</form-login-page>
			<!-- 인증 실패 시 오류 페이지 설정 -->
			<form-error-page>/ch04/login_failed.jsp</form-error-page>
		</form-login-config>
	</login-config>
	<!-- 시큐리티 역할 role1  관리 끝 -->
	<!-- 
		웹 어플리케이션이 시작할 때(톰켓을 시작할 때) DBCPInit 서블릿 클래스가
		자동으로 시작되고, init() 메서드가 호출됨
	-->
	<servlet>
		<servlet-name>oracleDriverLoader</servlet-name>
		<servlet-class>jdbc.DBCPInit</servlet-class>
		<load-on-startup>1</load-on-startup>
		<!-- 1 : 톰켓이 재 기동할 때 이 클래스를 가장 먼저 실행해라~ -->
	</servlet>

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>
	<h2>인증 성공했습니다.</h2>
	<h4>사용자명 : <%=request.getRemoteUser() %></h4>
	<h4>인증방법 : <%=request.getAuthType() %></h4>
	<h4>
		역할명(role) tomcat에 속한 사용자가 로그인한건가요?
		<%=request.isUserInRole("tomcat") %>
	</h4>
	<h4>
		역할명(role) role1에 속한 사용자가 로그인한건가요?
		<%=request.isUserInRole("role1") %>
	</h4>
</body>
</html>

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>
<link rel="stylesheet" href="" />
</head>
<body>
	<!-- 폼 기반 인증 처리를 위해 j_security_check을 사용 -->
	<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>

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>
	<h2>인증 실패했습니다.</h2>
</body>
</html>

0개의 댓글