目录

JSP - 安全性( Security)

JavaServer Pages和servlet为Web开发人员提供了几种机制来保护应用程序。 通过在应用程序部署描述符中标识资源并为其分配角色,以声明方式保护资源。

提供多种级别的身份验证,从使用标识符和密码的基本身份验证到使用证书的复杂身份验证。

角色认证

servlet规范中的身份验证机制使用称为role-based security的技术。 我们的想法是,不是在用户级别限制资源,而是创建角色并按角色限制资源。

您可以在文件tomcat-users.xml定义不同的角色,该文件位于conf的Tomcat主目录之外。 此文件的示例如下所示 -

<?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>

此文件定义username, passwordrole之间的简单映射。 请注意,给定用户可能有多个角色; 例如, username = "both"是“tomcat”角色和“role1”角色。

一旦确定并定义了不同的角色,就可以使用WEB-INF目录中提供的web.xml文件中的《security-constraint》元素,将基于角色的安全性限制放在不同的Web应用程序资源上。

以下是web.xml中的示例条目 -

<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>

以上条目意味着 -

  • 对/ secured/*匹配的URL的任何HTTP GET或POST请求都受安全限制的约束。

  • 具有经理角色的人员可以访问受保护的资源。

  • login-config元素用于描述BASIC形式的身份验证。

如果您尝试浏览任何URL(包括/security目录),将显示以下对话框,询问用户名和密码。 如果您向用户提供"admin"和密码"secrer" ,那么您将访问与/secured/*匹配的URL,因为我们已将用户admin定义为允许访问此资源的manager角色。

基于表单的认证

使用FORM身份验证方法时,必须提供登录表单以提示用户输入用户名和密码。 以下是login.jsp的简单代码。 这有助于为同一目的创建表单 -

<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!">
      </form>
   </body>
</html>

在这里,您必须确保登录表单必须包含名为j_usernamej_password的表单元素。 《form》标记中的操作必须是j_security_checkPOST必须用作表单方法。 同时,您必须修改《login-config》标记以将auth-method指定为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>

现在,当您尝试使用URL /secured/*访问任何资源时,它将显示上述表单,询问用户ID和密码。 当容器看到“ j_security_check ”操作时,它使用一些内部机制来验证调用者。

如果登录成功并且调用者被授权访问安全资源,则容器使用session-id从该点开始为调用者标识登录会话。 容器使用包含session-id的cookie维护登录会话。 服务器将cookie发送回客户端,只要调用者将此cookie与后续请求一起呈现,容器就会知道调用者是谁。

如果登录失败,则服务器将返回由form-error-page设置标识的页面

这里, j_security_check是使用基于表单的登录的应用程序必须为登录表单指定的操作。 在相同的表单中,您还应该有一个名为j_username的文本输入控件和一个名为j_usernamepassword input control 。 当您看到这一点时,表示表单中包含的信息将提交给服务器,服务器将检查名称和密码。 如何做到这一点是服务器特定的。

检查Standard Realm Implementations以了解j_security_check如何为Tomcat容器工作。

Servlet/JSP中的编程安全性

HttpServletRequest对象提供以下方法,可用于在运行时挖掘安全信息 -

S.No. 方法和描述
1

String getAuthType()

getAuthType()方法返回一个String对象,该对象表示用于保护Servlet的身份验证方案的名称。

2

boolean isUserInRole(java.lang.String role)

isUserInRole()方法返回一个布尔值:如果用户处于给定角色,则返回true;否则返回false。

3

String getProtocol()

getProtocol()方法返回一个String对象,表示用于发送请求的协议。 可以检查该值以确定是否使用了安全协议。

4

boolean isSecure()

isSecure()方法返回一个布尔值,表示请求是否是使用HTTPS生成的。 值为true表示它是,并且连接是安全的。 值false表示请求不是。

5

Principle getUserPrinciple()

getUserPrinciple()方法返回一个java.security.Principle对象,该对象包含当前经过身份验证的用户的名称。

例如,对于链接到管理器页面的JavaServer Page,您可能具有以下代码 -

<% if (request.isUserInRole("manager")) { %>
   <a href = "managers/mgrreport.jsp">Manager Report</a>
   <a href = "managers/personnel.jsp">Personnel Records</a>
<% } %>

通过检查用户在JSP或servlet中的角色,您可以自定义网页以仅向用户显示她可以访问的项目。 如果您需要在身份验证表单中输入的用户名,则可以在请求对象中调用getRemoteUser方法。

↑回到顶部↑
WIKI教程 @2018