使用 Web 应用程序的程序化安全性 API 开发

使用此信息来通过程序保护 Web 应用程序的 API。

开始之前

当声明式安全性独自不足以表达应用程序的安全模型时,具有安全性意识的应用程序使用程序化安全性。

authenticate、login、logout、getRemoteUser、isUserInRole 和 getAuthType Servlet 安全性方法是 javax.servlet.http.HttpServletRequest 接口的方法。有关这些 Servlet 安全性方法的更详细的信息,请阅读“Servlet 安全性方法”一文。
注:

注销、登录和认证 API 是本发行版的 WebSphere® Application Server 中 Java™ Servlet 3.0 的新增内容。

可以对 Web 认证配置几个选项,以确定 Web 客户机端与受保护和未受保护统一资源标识 (URI) 交互的方式。并且,可以指定 WebSphere Application Server 在 HTTPS 客户机的证书认证失败时是否要求 Web 客户机端提供基本认证信息。有关更多信息,请参阅“选择认证机制”一文。

[z/OS]您可以启用登录模块以表明这些调用返回了哪个主体类。请参阅有关使用 Java 认证和授权服务登录模块以将注册表主体映射到系统授权工具用户标识的主题,以了解更多信息。

在使用 isUserInRole 方法时,请在部署描述符中,使用包含传递给此方法的角色名的role-name 子元素声明 security-role-ref 元素,或使用 @DeclareRoles 注释。由于实际的角色是在应用程序组装阶段创建的,所以可以将逻辑角色用作角色名并在 security-role-ref 元素的描述中向组装器提供足够的线索,以将该角色链接到实际的角色。组装期间,组装器将创建 role-link 子元素,以将角色名链接到实际的角色。如果使用诸如 Rational® Application Developer 之类的组装工具,那么可以创建 security-role-ref 元素。您也可以在组装阶段使用组装工具创建 security-role-ref 元素。

过程

  1. 在 Servlet 代码中添加必需的安全性方法。
  2. 创建带有 role-name 字段的 security-role-ref 元素。如果在开发期间未创建 security-role-ref 元素,那么确保在组装阶段创建它。

结果

程序化保护的 Servlet 应用程序。

示例

要以编程方式保护应用程序,需要执行这些步骤。当 Web 应用程序需要访问外部资源并希望使用它自己的授权表(外部资源到远程用户映射)来控制对外部资源的访问时,此操作尤其有用。在这种情况下,请使用 getUserPrincipal 或 getRemoteUser 方法来获取远程用户,然后,应用程序可以查询自己的授权表以执行授权。远程用户信息还可以帮助从外部源(如数据库)或从另一个企业 Bean 检索相应的用户信息。可以按类似的方式使用 isUserInRole 方法。
在开发后,可以创建 security-role-ref 元素:
<security-role-ref>
   <description>Provide hints to assembler for linking this role 
                name to an actual role here<\description>
   <role-name>Mgr<\role-name>
</security-role-ref>
组装期间,组装器创建 role-link 元素:
<security-role-ref>
   <description>Hints provided by developer to map the role 
                name to the role-link</description>
   <role-name>Mgr</role-name>
   <role-link>Manager</role-link>
</security-role-ref>
可以在任何 servlet doGet、doPost、doPut 和 doDelete 服务方法中添加程序化 Servlet 安全性方法。下列示例描述使用程序化安全性 API:
public void doGet(HttpServletRequest request, 
HttpServletResponse response) {

   ....
   // to logoff the current user
	 request.logout();
    
   // to login with a new user
   request.login(“bob”,”bobpwd”)

   // to get remote user using getUserPrincipal()
   java.security.Principal principal = request.getUserPrincipal();
   String remoteUser = principal.getName();
 
   // to get remote user using getRemoteUser()
   remoteUser = request.getRemoteUser();

   // to check if remote user is granted Mgr role
   boolean isMgr = request.isUserInRole("Mgr");

   // use this information in any way as needed by 
   // the application 
   ....
                  
}
可以在任何 servlet doGet、doPost、doPut 和 doDelete 服务方法中使用用户标识和密码进行程序化登录。以下示例描述了如何使用程序化登录/注销 API:
public void doGet(HttpServletRequest request, 
HttpServletResponse response) {

   ....
   // to logout the current user. If you are not already authenticate, then no need to call the logout() method.
   request.logout();
    
   // to login with a new user
   request.login(“utle”,”mypwd”)

   // the user utle subject now set on the thread and the LTPA SSO cookie is set in the response
   ....
                  
}
可以在任何 servlet doGet、doPost、doPut 和 doDelete 服务方法中使用不同标识进行程序化认证。在此示例中,如果 Web Servlet 已配置为使用基本认证,那么 Web 服务器返回响应代码 401,并且将显示登录提示,然后您可以输入要认证的用户标识和密码。以下示例描述了如何使用程序化登录/注销 API:
public void doGet(HttpServletRequest request, 
HttpServletResponse response) {

   ....
   // to logout the current user. If you are not already authenticate, then no need to call the logout() method.
    
   // to login with a new user
   request.authenticate(response);

   // the new user subject now set on the thread and the LTPA SSO cookie is set in the response
   ....
                  
}
开发 Servlet 3.0 模块时,可以通过使用 Java 注释来定义 isCallerInRole 方法中 rolename 参数的值,而不是通过在部署描述符中声明 security-role-ref 元素进行定义。
@javax.annotation.security.DeclareRoles("Mgr")
public void doGet(HttpServletRequest request, 
HttpServletResponse response) {

   ....

   // to get remote user using getUserPrincipal()
   java.security.Principal principal = request.getUserPrincipal();
   String remoteUser = principal.getName();
 
   // to get remote user using getRemoteUser()
   remoteUser = request.getRemoteUser();

   // to check if remote user is granted Mgr role
   boolean isMgr = request.isUserInRole("Mgr");

   // use this information in any way as needed by 
   // the application 
   ....
                  
}

以下示例使用程序化安全模型说明了 Web 应用程序或 Servlet。

此示例说明了程序化安全模型的一种用法(不一定限于这一种用法)。应用程序可以通过其他任何对于该应用程序有意义的方法来使用由 getUserPrincipal、isUserInRole 和 getRemoteUser 方法返回的信息。请尽可能地使用声明式安全模型。

File : HelloServlet.java

public class HelloServlet extends javax.servlet.http.HttpServlet {

	public void doPost(
		javax.servlet.http.HttpServletRequest request,
		javax.servlet.http.HttpServletResponse response)
		throws javax.servlet.ServletException, java.io.IOException {
	}
public void doGet(
		javax.servlet.http.HttpServletRequest request,
		javax.servlet.http.HttpServletResponse response)
		throws javax.servlet.ServletException, java.io.IOException {
			
        String s = "Hello";
  	
        	
        // get remote user using getUserPrincipal()
        java.security.Principal principal = request.getUserPrincipal();
        String remoteUserName = "";
        if( principal != null )
         	remoteUserName = principal.getName();
// get remote user using getRemoteUser()
        String remoteUser = request.getRemoteUser();

        // check if remote user is granted Mgr role
        boolean isMgr = request.isUserInRole("Mgr");

        // display Hello username for managers and bob. 
        if ( isMgr || remoteUserName.equals("bob") )
            s = "Hello " + remoteUserName;

		 String message =  "<html> \n" +
            		"<head><title>Hello Servlet</title></head>\n" +
      			"<body> /n +"
				"<h1> "  +s+ </h1>/n " + 
		byte[] bytes = message.getBytes();
		
		// displays "Hello" for ordinary users 
        // and displays "Hello username" for managers and "bob".
        response.getOutputStream().write(bytes);
	}

}
在开发 Servlet 后,可以为 HelloServlet Servlet 创建安全角色引用,如以下示例所示:
<security-role-ref>
     <description> </description>
     <role-name>Mgr</role-name>
</security-role-ref>

下一步做什么

开发应用程序后,使用组装工具创建角色,并将实际的角色链接到 security-role-ref 元素中的角色名称。请参阅有关使用组装工具保护 Web 应用程序的信息。

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tsec_web
文件名:tsec_web.html