使用 Web 应用程序的程序化安全性 API 开发
使用此信息来通过程序保护 Web 应用程序的 API。
开始之前
当声明式安全性独自不足以表达应用程序的安全模型时,具有安全性意识的应用程序使用程序化安全性。
注销、登录和认证 API 是本发行版的 WebSphere® Application Server 中 Java™ Servlet 3.0 的新增内容。
可以对 Web 认证配置几个选项,以确定 Web 客户机端与受保护和未受保护统一资源标识 (URI) 交互的方式。并且,可以指定 WebSphere Application Server 在 HTTPS 客户机的证书认证失败时是否要求 Web 客户机端提供基本认证信息。有关更多信息,请参阅“选择认证机制”一文。
您可以启用登录模块以表明这些调用返回了哪个主体类。请参阅有关使用 Java 认证和授权服务登录模块以将注册表主体映射到系统授权工具用户标识的主题,以了解更多信息。
在使用 isUserInRole 方法时,请在部署描述符中,使用包含传递给此方法的角色名的role-name 子元素声明 security-role-ref 元素,或使用 @DeclareRoles 注释。由于实际的角色是在应用程序组装阶段创建的,所以可以将逻辑角色用作角色名并在 security-role-ref 元素的描述中向组装器提供足够的线索,以将该角色链接到实际的角色。组装期间,组装器将创建 role-link 子元素,以将角色名链接到实际的角色。如果使用诸如 Rational® Application Developer 之类的组装工具,那么可以创建 security-role-ref 元素。您也可以在组装阶段使用组装工具创建 security-role-ref 元素。
过程
- 在 Servlet 代码中添加必需的安全性方法。
- 创建带有 role-name 字段的 security-role-ref 元素。如果在开发期间未创建 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>
<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>
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
....
}
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
....
}
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
....
}
@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);
}
}
<security-role-ref>
<description> </description>
<role-name>Mgr</role-name>
</security-role-ref>