JSP - 安全性

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

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

基于角色的身份验证

servlet规范中的身份验证机制使用一种名为基于角色的安全性的技术.我们的想法是,不是在用户级别限制资源,而是创建角色并按角色限制资源.

您可以在文件中定义不同的角色 tomcat-users.xml ,位于confcat的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 ="both"处于"tomcat"角色和"role1"角色.

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

以下是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/* 匹配的网址为我们已经定义了允许管理员角色访问该资源的用户admin.

基于表单的身份验证

当您使用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_username j_password . < form> 标记中的操作必须为 j_security_check .必须使用 POST 作为表单方法.同时,您必须修改< 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_password 密码输入控件.当您看到这一点时,表示表单中包含的信息将提交给服务器,服务器将检查名称和密码.如何做到这一点是服务器特定的.

检查标准领域实现,以了解 j_security_check 如何为Tomcat容器工作..

Servlet/JSP中的编程安全性

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

S.No.Method&描述
1

字符串getAuthType()

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

2

boolean isUserInRole(java.lang.String role)

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

3

字符串getProtocol()

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

4

boolean isSecure()

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

5

原理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 方法.