知识库--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
- 知识库--Realm实现(52)
- 知识库--Realm(51)
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- Apache Shiro 使用手册(四)Realm 实现
- C# 获取Excel的指定单元格的值
- sql语句基本操作
- 读《从Paxos到Zookeeper 分布式一致性原理与实践》笔记之Zookeeper介绍
- 中央帝国精神之神:思规规思律想之轨迹:道思德律:律人:人治:律法:公法天下。
- AndroidStudio 将项目托管到github上
- 知识库--Realm实现(52)
- ElasticSearch实战 (二)CRUD以及bulk批量操作 api
- MySQL的表分区详解示例
- 采用ClassPathXmlApplicationContext,加载Spring的配置文件
- 传智播客关于线程通讯的讲解
- C语言编程错误及分析记录
- 如何快速的提升自己的技能
- pull解析之简单案例
- ognl语言