Guest User

Untitled

a guest
May 30th, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. Java EE 6 on Glassfish 3.1 CallbackHandler throws NullPointerException
  2. public class PasswordSafeLoginModule implements LoginModule {
  3.  
  4. private Subject subject;
  5. private CallbackHandler callbackHandler;
  6. private Object sharedState;
  7. private Object options;
  8.  
  9. private Set<Principal> principalsAdded;
  10. private boolean authenticated;
  11. private String username;
  12. private String password;
  13.  
  14. @Override
  15. public void initialize(Subject subject, CallbackHandler callbackHandler,
  16. Map<String, ?> sharedState, Map<String, ?> options) {
  17. this.subject = subject;
  18. this.callbackHandler = callbackHandler;
  19. this.sharedState = sharedState;
  20. this.options = options;
  21.  
  22. }
  23.  
  24. @Override
  25. public boolean abort() throws LoginException {
  26. // TODO
  27. return true;
  28. }
  29.  
  30. @Override
  31. public boolean commit() throws LoginException {
  32. // TODO
  33. return false;
  34. }
  35.  
  36. @Override
  37. public boolean login() throws LoginException {
  38. NameCallback nameCB = new NameCallback("Username");
  39. PasswordCallback passwordCB = new PasswordCallback("Password", true);
  40. Callback[] callbacks = new Callback[] { nameCB, passwordCB };
  41. try {
  42. callbackHandler.handle(callbacks);
  43. // Authenticate username/password
  44. username = nameCB.getName();
  45. password = String.valueOf(passwordCB.getPassword());
  46. //
  47. // lookup credentials
  48. //
  49. // TODO...
  50. return true;
  51. } catch (IOException e) {
  52. throw new LoginException(e.getMessage());
  53. } catch (UnsupportedCallbackException e) {
  54. throw new LoginException(e.getMessage());
  55. }
  56. }
  57.  
  58. @Override
  59. public boolean logout() throws LoginException {
  60. logger.info("Logging out '" + username + "'...");
  61. return false;
  62. }
  63. }
  64.  
  65. @Singleton
  66. @Startup
  67. public class PasswordSafeJAASConfiguration extends Configuration {
  68.  
  69. /**
  70. * This method just registers the configuration after the construction.
  71. */
  72. @PostConstruct
  73. void init() {
  74. Configuration.setConfiguration(this);
  75. }
  76.  
  77. @Override
  78. public AppConfigurationEntry[] getAppConfigurationEntry(
  79. String applicationName) {
  80. AppConfigurationEntry[] entries = new AppConfigurationEntry[1];
  81. entries[0] = new AppConfigurationEntry(
  82. PasswordSafeLoginModule.class.getName(),
  83. LoginModuleControlFlag.REQUIRED, new HashMap<String, Object>());
  84. return entries;
  85. }
  86. }
  87.  
  88. <form method="POST" action="j_security_check">
  89. <h:messages />
  90. <h:panelGrid columns="2" columnClasses="rightAlign,leftAlign">
  91. <h:outputText value="Email address:" />
  92. <h:inputText id="j_username" required="true" size="8">
  93. <f:validator validatorId="emailAddressValidator" />
  94. <f:validateLength minimum="5" maximum="128" />
  95. </h:inputText>
  96. <h:outputText value="Password:" />
  97. <h:inputText id="j_password" required="true" size="8" />
  98. </h:panelGrid>
  99. <h:commandButton value="Login..." />
  100. </form>
  101.  
  102. Caused by: javax.security.auth.login.LoginException: java.lang.NullPointerException
  103. at com.sun.enterprise.security.auth.login.common.ServerLoginCallbackHandler.handle(ServerLoginCallbackHandler.java:109)
  104. at javax.security.auth.login.LoginContext$SecureCallbackHandler$1.run(LoginContext.java:955)
  105. at java.security.AccessController.doPrivileged(Native Method)
  106. at javax.security.auth.login.LoginContext$SecureCallbackHandler.handle(LoginContext.java:951)
  107. at com.puresol.passwordsafe.jaas.PasswordSafeLoginModule.login(PasswordSafeLoginModule.java:70)
  108. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  109. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  110. [...]
  111.  
  112. FacesContext context = FacesContext.getCurrentInstance();
  113. HttpServletRequest request = (HttpServletRequest) context
  114. .getExternalContext().getRequest();
  115. request.login(login.getEmail(), login.getPassword());
  116.  
  117. Caused by: javax.security.auth.login.LoginException: java.lang.NullPointerException
  118. at com.sun.enterprise.security.auth.login.common.ServerLoginCallbackHandler.handle(ServerLoginCallbackHandler.java:109)
  119. at javax.security.auth.login.LoginContext$SecureCallbackHandler$1.run(LoginContext.java:955)
  120. at java.security.AccessController.doPrivileged(Native Method)
  121. at javax.security.auth.login.LoginContext$SecureCallbackHandler.handle(LoginContext.java:951)
  122. at com.puresol.passwordsafe.jaas.PasswordSafeLoginModule.login(PasswordSafeLoginModule.java:70)
  123. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  124. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  125. [...]
Advertisement
Add Comment
Please, Sign In to add comment