JAVA/JSP
[JSP] 시큐리티(security)
아잠만_
2024. 7. 10. 17:32
시큐리티(security)
허가된 사용자만이 특정 웹페이지 에 접근할 수 있도록 제한하는 보안 기능
- 인증(authentication) : 로그인(아이디, 비밀번호)
- 인가(authorization) : 로그인 후 권한 체킹
server>tomcat-users.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<tomcat-users version="1.0" xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd">
<!--
By default, no user is included in the "manager-gui" role required
to operate the "/manager/html" web application. If you wish to use this app,
you must define such a user - the username and password are arbitrary.
Built-in Tomcat manager roles:
- manager-gui - allows access to the HTML GUI and the status pages
- manager-script - allows access to the HTTP API and the status pages
- manager-jmx - allows access to the JMX proxy and the status pages
- manager-status - allows access to the status pages only
The users below are wrapped in a comment and are therefore ignored. If you
wish to configure one or more of these users for use with the manager web
application, do not forget to remove the <!.. ..> that surrounds them. You
will also need to set the passwords to something appropriate.
-->
<!--
<user username="admin" password="<must-be-changed>" roles="manager-gui"/>
<user username="robot" password="<must-be-changed>" roles="manager-script"/>
-->
<!--
The sample user and role entries below are intended for use with the
examples web application. They are wrapped in a comment and thus are ignored
when reading this file. If you wish to configure these users for use with the
examples web application, do not forget to remove the <!.. ..> that surrounds
them. You will also need to set the passwords to something appropriate.
-->
<!--
시큐리티(security) : 허가된 사용자만이 특정 웹페이지에 접근할 수 있도록 제한하는 보안 기능
1) 인증(authentication) : 로그인(아이디, 비밀번호)
2) 인가(authorization) : 로그인 후 권한 체킹
-->
<role rolename="tomcat"/><!-- 역할 -->
<role rolename="role1"/><!-- 역할 -->
<user username="tomcat" password="java" roles="tomcat"/><!-- 사용자 -->
<user username="both" password="java" roles="tomcat,role1"/><!-- 사용자 -->
<user username="role1" password="java" roles="role1"/><!-- 사용자 -->
</tomcat-users>
WEB-INF>web.xml
선언적 시큐리티 처리
web.xml 파일에 보안 구성을 작성 (역할-role, 사용자-username, URL 경로)을 작성
- 역할/사용자 : tomcat-users.xml에 등록된 역할/사용자
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>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>
<!-- 시큐리티 인증 설정 끝 -->
</web-app>
jsp로 접속
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>Security</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 role? -->
<%=request.isUserInRole("tomcat") %></p>
<p>인증한 사용자명이 role(역할) "role1"에 속하는 사용자 인지 여부
<!-- Is that your role? -->
<%=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" method="post" action="j_security_check">
<!-- name속성의 값인 j_username은 변경할 수 없음 -->
<p>아이디 : <input type="text" name="j_username" placeholder="아이디" required="required"/></p>
<!-- name속성의 값인 j_password도 변경할 수 없음 -->
<p>비밀번호 : <input type="password" name="j_password" required="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></title>
</head>
<body>
<h1>로그인 실패!!!</h1>
</body>
</html>