知识库--Realm实现(52)

来源:互联网 发布:java编程思想pdf 编辑:程序博客网 时间:2024/05/17 22:27

Here is a SimpleRealm class in following demonstrates how a realm works. This class contains two hard-coded user name and password.

public class SimpleRealm implements Realm {    public SimpleRealm() {        createUserDatabase();    }    private Container container;    private ArrayList users = new ArrayList();    public Container getContainer() {        return container;    }    public void setContainer(Container container) {        this.container = container;    }    public String getInfo() {        return "A simple Realm implementation";    }    public void addPropertyChangeListener(PropertyChangeListener listener) {    }    //主体    public Principal authenticate(String username, String credentials) {        System.out.println("SimpleRealm.authenticate()");        if (username == null || credentials == null) return null;        User user = getUser(username, credentials);        if (user == null) return null;        return new GenericPrincipal(this, user.username, user.password, user.getRoles());    }    public Principal authenticate(String username, byte[] credentials) {        return null;    }    public Principal authenticate(String username, String digest, String nonce, String nc, String cnonce, String qop, String realm, String md5a2) {        return null;    }    public Principal authenticate(X509Certificate certs[]) {        return null;    }    public boolean hasRole(Principal principal, String role) {        if ((principal == null) || (role == null) || !(principal instanceof GenericPrincipal)) return (false);        GenericPrincipal gp = (GenericPrincipal) principal;        if (!(gp.getRealm() == this)) return (false);        boolean result = gp.hasRole(role);        return result;    }    public void removePropertyChangeListener(PropertyChangeListener listener) {    }    private User getUser(String username, String password) {        Iterator iterator = users.iterator();        while (iterator.hasNext()) {            User user = (User) iterator.next();            if (user.username.equals(username) && user.password.equals(password)) return user;        }        return null;    }    private void createUserDatabase() {        User userl = new User("ken", "blackcomb");        user1.addRole("manager");        user1.addRole("programmer");        User user2 = new User("cindy", "bamboo");        user2.addRole("programmer");        users.add(userl);        users.add(user2);    }    class User {        public User(String username, String password) {            this.username = username;            this.password = password;        }        public String username;        public ArrayList roles = new ArrayList();        public String password;        public void addRole(String role) {            roles.add(role);        }        public ArrayList getRoles() {            return roles;        }    }}//实现二    public class SimpleUserDatabaseRealm extends RealmBase {    protected UserDatabase database = null;    protected static final String name = "SimpleUserDatabaseRealm";    protected String resourceName = "UserDatabase";    public Principal authenticate(String username, String credentials) { // Does a user with this username exist?         User user = database.findUser(username);        if (user == null) {            return (null);        } // Do the credentials specified by the user match? boolean validated = false;        if (hasMessageDigest()) { // Hex hashes should be compared case-insensitive             validated = (digest(credentials).equalsIgnoreCase(user.getPassword()));        } else {            validated = (digest(credentials).equals(user.getPassword()));        }        if (!validated) {            return null;        }        ArrayList combined = new ArrayList();        Iterator roles = user.getRoles();        while (roles.hasNext()) {            Role role = (Role) roles.next();            String rolename = role.getRolename();            if (!combined.contains(rolename)) {                combined.add(rolename);            }        }        Iterator groups = user.getGroups();        while (groups.hasNext()) {            Group group = (Group) groups.next();            roles = group.getRoles();            while (roles.hasNext()) {                Role role = (Role) roles.next();                String rolename = role.getRolename();                if (!combined,contains(rolename)){                    combined.add(rolename);                }            }        } return (new GenericPrincipal(this, user.getUsername(), user.getPassword(), combined));    }    protected Principal getPrincipal(String username) {        return (null);    }    protected String getPassword(String username) {        return null;    }    protected String getName() {        return this.name;    }    public void createDatabase(String path) {        database = new MemoryUserDatabase(name);        ((MemoryUserDatabase) database).setPathname(path);        try {            database, open();        } catch (Exception e) {        }    }}
0 0
原创粉丝点击