Thank you for this post the jconsole -debug was the break through
I believe what you are missing is that you have not configured "a" in the jmxremote.access, Using your above example, when i changed a few lines it worked for me
note the addition of the
user = new JMXPrincipal(name);
and using the uncommented lines in "jmxremote.access"
controlRole readwrite
I hope this helps other programmers in the same situation.
public class EncryptedFileLoginModule implements LoginModule {
private Subject subject;
private CallbackHandler callbackHandler;
private Map sharedState;
private Map options;
private JMXPrincipal user;
private String name;
private String password;
private boolean succeeded = false;
public EncryptedFileLoginModule() {
System.out.println("Login Module - constructor called");
}
public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
Map<String, ?> options) {
System.out.println("Login Module - initialize called");
this.subject = subject;
this.callbackHandler = callbackHandler;
this.sharedState = sharedState;
this.options = options;
System.out.println("testOption value: " + (String) options.get("testOption"));
succeeded = false;
}
public boolean login() throws LoginException {
System.out.println("Login Module - login called");
if (callbackHandler == null) {
throw new LoginException("Oops, callbackHandler is null");
}
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("name:");
callbacks[1] = new PasswordCallback("password:", false);
try {
callbackHandler.handle(callbacks);
} catch (IOException e) {
throw new LoginException("Oops, IOException calling handle on callbackHandler");
} catch (UnsupportedCallbackException e) {
throw new LoginException("Oops, UnsupportedCallbackException calling handle on callbackHandler");
}
NameCallback nameCallback = (NameCallback) callbacks[0];
PasswordCallback passwordCallback = (PasswordCallback) callbacks[1];
name = nameCallback.getName();
password = new String(passwordCallback.getPassword());
if ("controlRole".equals(name) && "a".equals(password)) {
System.out.println("Success! You get to log in!");
// Create a new user principal
user = new JMXPrincipal(name);
succeeded = true;
return succeeded;
} else {
System.out.println("Failure! You don't get to log in");
succeeded = false;
throw new FailedLoginException("Sorry! No login for you.");
}
}
public boolean abort() throws LoginException {
System.out.println("Login Module - abort called");
return false;
}
public boolean commit() throws LoginException {
System.out.println("Login Module - commit called");
subject.getPrincipals().add(user);
return succeeded;
}
public boolean logout() throws LoginException {
System.out.println("Login Module - logout called");
user = null;
succeeded = false;
return false;
}
}
class UserPrincipal implements Principal, Serializable {
private static final long serialVersionUID = -4604480892359393296L;
private String name;
public UserPrincipal(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return name;
}
public boolean equals(Object o) {
return o instanceof UserPrincipal &&
((UserPrincipal)o).name.equals(name);
}
public int hashCode() {
return name.hashCode();
}
}
class StatePrincipal implements Principal, Serializable {
private static final long serialVersionUID = 8429580270033209093L;
private String state;
public StatePrincipal(String state) {
this.state = state;
}
public String getName() {
return state;
}
public String toString() {
return state;
}
public boolean equals(Object o) {
return o instanceof StatePrincipal && ((StatePrincipal)o).equals(state);
}
public int hashCode() {
return state.hashCode();
}
}