Custom principal
Principals can be customized and new ones can be implemented based on the java.security.Principal interface. The example below shows an implementation, SamplePrincipal, of a principal. We can customize the principals to store extra information about the user, other than just the user name.
Custom principal: SamplePrincipal.java
package redbook.sg246316.loginmodule; import java.security.Principal; public class SamplePrincipal implements Principal, java.io.Serializable { private String name; public SamplePrincipal(String name) { if (name == null) throw new NullPointerException("illegal null input"); this.name = name; } public String getName() { return name; } public String toString() { return ("SamplePrincipal: " + name); } public boolean equals(Object o) { if (o == null) return false; if (this == o) return true; if (!(o instanceof SamplePrincipal)) return false; SamplePrincipal that = (SamplePrincipal) o; if (this.getName().equals(that.getName())) return true; return false; } public int hashCode() { return name.hashCode(); } }As we can see, the principal is a simple Java object with attributes, a collection of set and getter methods, and a few other supporting methods.