Hitachi

uCosminexus Application Server Security Management Guide


5.10.3 Implementation of the API-based session to obtain user attributes

To obtain user attributes, it is required to specify the list of attributes that should be obtained at the time of login. The following is an example of implementing the login process that specifies the list of user attributes.

<%@ 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) { ... }
%>
...

The above example obtains the specified attributes from the repository and assigns them to the UserAttributes object. These objects are managed as the java.lang.Object type. The following is an example of implementation in which the attributes obtained from the repository are assigned to the UserAttributes object.

LoginContext lc = new ...                 // This is to instantiate the LoginContext class
...
Subject subject  = lc.getSubject();
Iterator it = subject.getPublicCredentials().iterator();
UserAttributes ua= (UserAttributes)it.next();              // This is to store the 
...                                                // UserAttributes reference in ua.

As shown below, the getAttribute method is used to obtain the attribute value in String from the UserAttributes object.

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

The following is an example of implementing the session to obtain the user attribute by using the getAttribute method.

<%@ 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");
%>
...