使用 Web 应用程序的程序化安全性 API 开发
使用此信息来通过程序保护 Web 应用程序的 API。
准备工作
当声明式安全性独自不足以表达应用程序的安全模型时,具有安全性意识的应用程序使用程序化安全性。
在此发行版的 WebSphere® Application Server中,注销,登录和认证 API 是 Java™ Servlet 3.0 的新增功能。
可以对 Web 认证配置几个选项,以确定 Web 客户机端与受保护和未受保护统一资源标识 (URI) 交互的方式。 此外,如果 HTTPS 客户端的证书身份验证失败,还可以指定 WebSphere Application Server 是否向网络客户端挑战基本身份验证信息。 有关更多信息,请参阅“选择认证机制”一文。
在使用 isUserInRole 方法时,请在部署描述符中,使用包含传递给此方法的角色名的role-name 子元素声明 security-role-ref 元素,或使用 @DeclareRoles 注释。 由于实际的角色是在应用程序组装阶段创建的,所以可以将逻辑角色用作角色名并在 security-role-ref 元素的描述中向组装者提供足够的线索,以将该角色链接到实际的角色。 组装期间,组装者将创建 role-link 子元素,以将角色名链接到实际的角色。 如果使用装配工具(如 Rational® Application Developer ),就可以创建安全-角色-参考元素。 您也可以在组装阶段使用组装工具创建 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 方法返回的信息。 请尽可能地使用声明式安全模型。
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>