Java Utililty Methods Security

List of utility methods to do Security

Description

The list of methods to do Security are organized into topic(s).

Method

Stringget(Configuration configuration, String section, String key)
get
AppConfigurationEntry configurationEntries[] = configuration.getAppConfigurationEntry(section);
if (configurationEntries == null) {
    String errorMessage = "Could not find a '" + section + "' entry in this configuration.";
    throw new IOException(errorMessage);
for (AppConfigurationEntry entry : configurationEntries) {
    Object val = entry.getOptions().get(key);
    if (val != null)
...
StringgetCommonName(String name)
Gets the common name (CN) component of an X.500 principal name.
try {
    LdapName ldapName = new LdapName(name);
    for (Rdn rdn : ldapName.getRdns()) {
        if (rdn.getType().equalsIgnoreCase("cn")) {
            return rdn.getValue().toString();
    return name;
...
StringgetDirectiveValue(HashMap directivesMap, String directive, boolean mandatory)
A directive is a parameter of the digest authentication process.
String value = directivesMap.get(directive);
if (value == null) {
    if (mandatory) {
        throw new AuthenticationException("\"" + directive + "\" mandatory directive is missing");
    return "";
return value;
...
AppConfigurationEntry[]getEntries(Configuration configuration, String section)
Get configurations for a section
if (configuration == null) {
    return null;
AppConfigurationEntry configurationEntries[] = configuration.getAppConfigurationEntry(section);
if (configurationEntries == null) {
    String errorMessage = "Could not find a '" + section + "' entry in this configuration.";
    throw new IOException(errorMessage);
return configurationEntries;
CgetPublicCredential(Class type, Subject subject)
Get the public credential of the given type.
for (Object cred : subject.getPublicCredentials()) {
    if (type.isInstance(cred)) {
        return type.cast(cred);
return null;
longgetRefreshTime(KerberosTicket ticket)
get Refresh Time
long start = ticket.getStartTime().getTime();
long end = ticket.getEndTime().getTime();
return start + (long) ((end - start) * TICKET_RENEW_WINDOW);
MapgetSaslProps()
get Sasl Props
Map<String, String> props = new HashMap<String, String>();
props.put(Sasl.POLICY_NOPLAINTEXT, "true");
return props;
EncryptionKeygetServerPrivateKey(Subject subject, int keyType)
get Server Private Key
Set<Object> serverCredentials = subject.getPrivateCredentials(Object.class);
KerberosPrincipal serverPrincipal = null;
KeyTab serverKeyTab = null;
for (Iterator<Object> i = serverCredentials.iterator(); i.hasNext();) {
    Object cred = i.next();
    if (cred instanceof KerberosKey) {
        KerberosKey key = (KerberosKey) cred;
        if (key.getKeyType() == keyType) {
...
SubjectgetSubject()
Obtain current authenticated subject
InitialContext ctx = new InitialContext();
return (Subject) ctx.lookup("java:comp/env/security/subject");
KerberosTicketgetTicketGrantingTicket(Subject subject)
get Ticket Granting Ticket
Set<KerberosTicket> tickets = subject.getPrivateCredentials(KerberosTicket.class);
for (KerberosTicket ticket : tickets) {
    if (isOriginalTicketGrantingTicket(ticket)) {
        return ticket;
throw new IllegalArgumentException("kerberos ticket not found in " + subject);