Hitachi

Cosminexus V11 アプリケーションサーバ 機能解説 セキュリティ管理機能編


5.10.3 ユーザ属性の取得の実装(APIを使用する場合)

ユーザ属性を取得するには,ログイン時に取得したい属性の一覧を指定する必要があります。ユーザ属性の一覧を指定する場合のログイン処理の実装例を,次に示します。

<%@ page import="com.cosminexus.admin.auth.callback.WebPasswordHandler" %>
<%@ page import="com.cosminexus.admin.auth.AttributeEntry" %>
<%@ page import="javax.security.auth.login.LoginContext" %>
...
<%
  AttributeEntry[] attributes = new AttributeEntry[2];
  attributes[0] = new AttributeEntry("cn", "full name", null);
  attributes[1] = new AttributeEntry("employeeNumber", "employee ID", null);
  LoginContext lc = new LoginContext("Portal",
    new WebPasswordHandler(request, response, attributes, "login.html", true));
  try { lc.login(); } catch (LoginException e) { ... }
%>
...

属性の一覧に従って属性をリポジトリから取得し,UserAttributesオブジェクトに設定します。取得した属性は,java.lang.Object型として管理されています。リポジトリから取得した属性をUserAttributesオブジェクトに設定する実装例を次に示します。

LoginContext lc = new ...              // LoginContextクラスのインスタンス化
...
Subject subject  = lc.getSubject();
Iterator it = subject.getPublicCredentials().iterator();
UserAttributes ua= (UserAttributes)it.next();   // uaにUserAttributesの
...                                             // リファレンスが格納される。

UserAttributesオブジェクト属性の値を参照するには,次のようにUserAttributesオブジェクトに対してgetAttributeメソッドを使用してString型で取得します。

String role = (String)ua.getAttribute("Portal Role");

getAttributeメソッドでユーザ属性を取得する場合の実装例を次に示します。

<%@ page import="com.cosminexus.admin.auth.UserAttributes" %>
<%@ page import="javax.security.auth.Subject" %>
...
<%
  ...
  Subject subject = lc.getSubject();
  UserAttributes attrs = (UserAttributes)subject.getPublicCredentials().iterator().next();
  String fullname = (String)attrs.getAttribute("full name");
  String eid = (String)attrs.getAttribute("employee ID");
%>
...