Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zti;
- import java.io.*;
- import java.util.*;
- import java.security.Principal;
- import javax.security.auth.Subject;
- import javax.security.auth.callback.*;
- import javax.security.auth.spi.LoginModule;
- import javax.security.auth.login.LoginException;
- public class MyLoginModule implements LoginModule {
- private Subject subject;
- private MyPrincipal entity;
- private CallbackHandler callbackhandler;
- private static final int NOT = 0, OK = 1, COMMIT = 2;
- private int status;
- public void initialize(Subject subject, CallbackHandler callbackhandler, Map state, Map options) {
- status = NOT;
- entity = null;
- this.subject = subject;
- this.callbackhandler = callbackhandler;
- }
- public boolean login() throws LoginException {
- if(callbackhandler == null) {
- throw new LoginException();
- }
- Callback callbacks[] = new Callback[1];
- callbacks[0] = new NameCallback("Podaj nazwę użytkownika?");
- String username = null;
- try {
- callbackhandler.handle(callbacks);
- username = ((NameCallback)callbacks[0]).getName();
- } catch(java.io.IOException ioe) {
- throw new LoginException(ioe.toString());
- } catch(UnsupportedCallbackException ce) {
- throw new LoginException("Error: "+ce.getCallback().toString());
- }
- if(username.equals("admin")) {
- entity = new MyPrincipal(username);
- status = OK;
- return true;
- } else
- return false;
- }
- public boolean commit() throws LoginException {
- if(status == NOT || subject == null) {
- return false;
- } else {
- Set entities = subject.getPrincipals();
- if(!entities.contains(entity)) {
- entities.add(entity);
- }
- status = COMMIT;
- return true;
- }
- }
- public boolean abort() throws LoginException {
- if((subject != null) && (entity != null)) {
- Set entities = subject.getPrincipals();
- if(entities.contains(entity)) {
- entities.remove(entity);
- }
- }
- subject = null;
- entity = null;
- status = NOT;
- return true;
- }
- public boolean logout() throws LoginException {
- subject.getPrincipals().remove(entity);
- status = NOT;
- subject = null;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement