Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. package zti;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.security.Principal;
  6. import javax.security.auth.Subject;
  7. import javax.security.auth.callback.*;
  8. import javax.security.auth.spi.LoginModule;
  9. import javax.security.auth.login.LoginException;
  10.  
  11. public class MyLoginModule implements LoginModule {
  12. private Subject subject;
  13. private MyPrincipal entity;
  14. private CallbackHandler callbackhandler;
  15.  
  16. private static final int NOT = 0, OK = 1, COMMIT = 2;
  17. private int status;
  18.  
  19. public void initialize(Subject subject, CallbackHandler callbackhandler, Map state, Map options) {
  20. status = NOT;
  21. entity = null;
  22. this.subject = subject;
  23. this.callbackhandler = callbackhandler;
  24. }
  25.  
  26. public boolean login() throws LoginException {
  27.  
  28. if(callbackhandler == null) {
  29. throw new LoginException();
  30. }
  31. Callback callbacks[] = new Callback[1];
  32. callbacks[0] = new NameCallback("Podaj nazwę użytkownika?");
  33. String username = null;
  34. try {
  35. callbackhandler.handle(callbacks);
  36. username = ((NameCallback)callbacks[0]).getName();
  37. } catch(java.io.IOException ioe) {
  38. throw new LoginException(ioe.toString());
  39. } catch(UnsupportedCallbackException ce) {
  40. throw new LoginException("Error: "+ce.getCallback().toString());
  41. }
  42. if(username.equals("admin")) {
  43. entity = new MyPrincipal(username);
  44. status = OK;
  45. return true;
  46. } else
  47. return false;
  48. }
  49.  
  50. public boolean commit() throws LoginException {
  51. if(status == NOT || subject == null) {
  52. return false;
  53. } else {
  54. Set entities = subject.getPrincipals();
  55. if(!entities.contains(entity)) {
  56. entities.add(entity);
  57. }
  58. status = COMMIT;
  59. return true;
  60. }
  61. }
  62.  
  63. public boolean abort() throws LoginException {
  64. if((subject != null) && (entity != null)) {
  65. Set entities = subject.getPrincipals();
  66. if(entities.contains(entity)) {
  67. entities.remove(entity);
  68. }
  69. }
  70. subject = null;
  71. entity = null;
  72. status = NOT;
  73. return true;
  74. }
  75.  
  76. public boolean logout() throws LoginException {
  77. subject.getPrincipals().remove(entity);
  78. status = NOT;
  79. subject = null;
  80. return true;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement