πŸ’» Programming/JSP

[JSP] Security ( λ³΄μ•ˆ )

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