JSPμ servlets μ Web κ°λ°μλ€μ μν΄μ 보μ(μΈμ¦)μ μ²λ¦¬ν μ μλ λ€μν λ°©λ²μ μ 곡ν©λλ€.
μ€λμ κ·Έ μ€ λκ°μ§μ λν΄μλ§ μμ보λλ‘ νκ² μ΅λλ€.
μν κΈ°λ° μΈμ¦
servlet μμ μ 곡νλ μν κΈ°λ° μΈμ¦μ μ¬μ©μ λ 벨μμ 리μμ€λ₯Ό μ ννλ κ²μ΄ μλλΌ μ¬μ©μμκ² μν μ λΆμ¬νκ³ νΉμ μν μ κ°μ§ λͺ¨λ μ¬μ©μμ λν΄μ 리μμ€λ₯Ό μ ννλ λ°©λ²μ λλ€.
ν°μΊ£ ν λλ ν 리μ confλλ ν 리μ tomcat-users.xml νμΌμ λ§λ€κ³ μλμ½λλ₯Ό λ£μ΄μ£ΌμΈμ.
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager"/>
<role rolename="admin"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="role1" password="tomcat" roles="role1"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="admin" password="secret" roles="admin,manager"/>
</tomcat-users>
μ μ½λκ° λ νκ³ μλμ§λ λμΆ© λ΄λ μμκ² μ£ ? μν μ μμ±νκ³ μ¬μ©μλ₯Ό λ§λ€λ μν μ λΆμ¬νκ³ μμ΅λλ€.
μ΄μ web.xmlνμΌμ <security-constraint> νκ·Έλ₯Ό μΆκ°νκ³ μλμ²λΌ λ£μ΄λ³΄μΈμ. λ¬Όλ‘ url-patternμ μ¬λ¬λΆμ΄ κ°μ§κ³ μλ μΉμ ν리μΌμ΄μ μ μ€μ λ‘ μ‘΄μ¬νλ urlμ΄μ΄μΌκ²μ£ ? κ·ΈλμΌ ν μ€νΈκ° κ°λ₯ν©λλ€~
<web-app>
...
<security-constraint>
<web-resource-collection>
<web-resource-name> SecuredBookSite </web-resource-name>
<url-pattern>/secured/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description> Let only managers use this app </description>
<role-name>manager</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>manager</role-name>
</security-role>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
...
</web-app>
μ μμ€μ½λκ° μλ―Ένλ κ²μ΄ 무μΈμΈκ° νλ©΄ λ€μκ³Ό κ°μ΅λλ€.
-
HTTP GET μ΄λ POST λ°©μμ μμ²μ λν΄μ /secured/* URLμ λν μμ²μ΄ λ€μ΄μ€λ©΄ 보μμ€μ μ μ μ½μ λ°κ²λ©λλ€.
-
맀λμ μν μ κ°μ§κ³ μλ μ¬μ©μμ λν΄μλ§ /secured/* 리μμ€μ μ κ·Όμ΄ κ°λ₯ν©λλ€.
-
λ§μ§λ§μΌλ‘ login-config νκ·Έκ° κΈ°λ³Έννμ μΈμ¦μ μν΄μ μ¬μ©λμμ΅λλ€.
μ΄μ /securityλ΄λΆμ νμ΄μ§λ‘ μ κ·Όμ νλ €λ©΄ μ¬μ©μλͺ κ³Ό λΉλ°λ²νΈλ₯Ό μ λ ₯νλΌλ μ°½μ΄ λ¨κ²λ©λλ€. μ μν μ μλ μν μ κ°μ§κ³ μμ§ μκ±°λ μΈμ¦μ μ€ν¨νλ©΄ μ μμ΄ λΆκ°λ₯νλ νμ΄μ§κ° λλ κ²μ΄μ£ .
Formμ μ΄μ©ν μΈμ¦
FORM μΈμ¦μ μ¬μ©νλ €λ©΄ μ¬μ©μμκ² λ‘κ·ΈμΈ νΌμ μ 곡ν΄μ£Όμ΄μΌ ν©λλ€.
<html>
<body bgcolor="#ffffff">
<form method="POST" action="j_security_check">
<table border="0">
<tr>
<td>Login</td>
<td><input type="text" name="j_username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="j_password"></td>
</tr>
</table> <input type="submit" value="Login!"> </center>
</form>
</body>
</html>
μ μ½λλ₯Ό login.jspμ λ£μ΄μ£ΌμΈμ. <form> νκ·Έ μμμλ actionμ κ°μ j_security_check μ¬μΌ ν©λλ€. POST λ°©μμ΄ μ¬μ©λμ΄μΌ νλ€λ κ²μ μκ³ κ³μκ² μ£ ? μ μ΄μ web.xmlμ <login-config> νκ·Έλ₯Ό FORMμ μ΄μ©νλλ‘ μμ ν΄μΌν©λλ€.
<web-app>
...
<security-constraint>
<web-resource-collection>
<web-resource-name> SecuredBookSite </web-resource-name>
<url-pattern>/secured/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description> Let only managers use this app </description>
<role-name>manager</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>manager</role-name>
</security-role>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
...
</web-app>
μ΄μ /secured/* μ£Όμλ‘ μ μμ μλν΄ λ³΄μΈμ. μ¬μ©μ IDμ ν¨μ€μλλ₯Ό μ λ ₯νλΌκ³ ν κ²λλ€. 컨ν μ΄λκ° "j_security_check" μ‘μ μ λ§λλ©΄μμ²μ μΈμ¦νκΈ°μν λ΄λΆ λ©μΉ΄λμ¦μ μ€νν©λλ€.
λ‘κ·ΈμΈμ΄ μ±κ³΅νκ³ λ¦¬μμ€λ₯Ό μ‘°νν μ μλ μν μ κ°μ§κ³ μλ€λ©΄ 컨ν μ΄λλ μ¬μ©μλ₯Ό νμΈνκΈ°μν΄μ session-id λ₯Ό λΉκ΅ν©λλ€. 컨ν μ΄λλ μΈμ μμ΄λλ₯Ό κ°μ§κ³ μλ μΏ ν€μ ν¨κ» μΈμ μ μ μ§νκ²λ©λλ€. μλ²κ° μ΄ μΏ ν€λ₯Ό ν΄λΌμ΄μΈνΈλ‘ λλλ €λ³΄λ΄κ³ μ΄ν μμ²λλ§λ€ μ΄ μΏ ν€λ₯Ό μλ²λ‘ μ μ‘νκ²λλ©΄ 컨ν μ΄λλ μμ²νλ ν΄λΌμ΄μΈνΈλ₯Ό μλ³ν μ μμ΅λλ€.
λ‘κ·ΈμΈμ΄ μ€ν¨νλ©΄ μλ²λ μλ¬νμ΄μ§λ₯Ό μ μ‘νκ² λ©λλ€.
j_security_check κ° ν°μΊ£ 컨ν μ΄λμμ μ΄λ»κ² λμνλμ§μ λν΄μ λ μμΈν μ 보λ₯Ό 보μλ €λ©΄ Standard Realm Implementations μ΄κ³³μ κ°λ³΄μΈμ.
Servlet/JSPμ μ½λλ‘ λ³΄μμ μ©νκΈ°
HttpServletRequest κ°μ²΄κ° 보μκ΄λ ¨ν΄μ μ 곡νλ λ©μλλ€μ λ€μκ³Ό κ°μ΅λλ€.
SN | Method and Description |
---|---|
1 | String getAuthType() The getAuthType() method returns a String object that represents the name of the authentication scheme used to protect the Servlet. |
2 | boolean isUserInRole(java.lang.String role) The isUserInRole() method returns a boolean value: true if the user is in the given role or false if they are not. |
3 | String getProtocol() The getProtocol() method returns a String object representing the protocol that was used to send the request. This value can be checked to determine if a secure protocol was used. |
4 | boolean isSecure() The isSecure() method returns a boolean value representing if the request was made using HTTPS. A value of true means it was and the connection is secure. A value of false means the request was not. |
5 | Principle getUserPrinciple() The getUserPrinciple() method returns a java.security.Principle object that contains the name of the current authenticated user. |
μλ₯Όλ€μ΄ 맀λμ κΆνμ κ°κ³ μλ μ¬μ©μλ€μ μν λ§ν¬λ₯Ό μ 곡νλ JavaServer Page λ₯Ό λ§λ€λ €λ©΄ μλ μ½λλ₯Ό μ½μ ν΄μ£Όμλ©΄ λλκ±°μ£
<% if (request.isUserInRole("manager")) { %>
<a href="managers/mgrreport.jsp">Manager Report</a>
<a href="managers/personnel.jsp">Personnel Records</a>
<% } %>
κ·Έλ¬λ©΄ μ¬μ©μ μν μ κ²μ¬ν λ€ λ§€λμ μν μ κ°μ§κ³ μλ μ¬μ©μμκ²λ§ λ§ν¬λ₯Ό 보μ¬μ£Όκ² λ©λλ€. λ§μ½ λ‘κ·ΈμΈ νΌμμ μ λ ₯ν μ¬μ©μλͺ μ΄ νμν κ²½μ°μλ getRemoteUser λ©μλλ₯Ό νΈμΆνλ©΄ λ©λλ€.
Reference : http://www.tutorialspoint.com/jsp/jsp_security.htm
'π» Programming > JSP' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
JSP vs Javascript (Difference between JSP and Javascript) (0) | 2019.06.28 |
---|---|
[JSP] Database CRUD (λ°μ΄ν°λ² μ΄μ€ μ°λ) (0) | 2019.02.15 |
[JSP] JSTL ( JSP Standard Tag Library ) (0) | 2019.02.15 |
[JSP] Page Redirection ( νμ΄μ§ 리λλ μ ) (0) | 2019.02.15 |
[JSP] File Upload ( νμΌ μ λ‘λ ) (0) | 2019.02.15 |