📄 hashuserrealm.java
字号:
* @param userName * @param roleName */ public synchronized void addUserToRole(String userName, String roleName) { HashSet userSet = (HashSet)_roles.get(roleName); if (userSet==null) { userSet=new HashSet(11); _roles.put(roleName,userSet); } userSet.add(userName); } /* -------------------------------------------------------- */ public boolean reauthenticate(Principal user) { return ((User)user).isAuthenticated(); } /* ------------------------------------------------------------ */ /** Check if a user is in a role. * @param user The user, which must be from this realm * @param roleName * @return True if the user can act in the role. */ public synchronized boolean isUserInRole(Principal user, String roleName) { if (user instanceof WrappedUser) return ((WrappedUser)user).isUserInRole(roleName); if (user==null || !(user instanceof User) || ((User)user).getUserRealm()!=this) return false; HashSet userSet = (HashSet)_roles.get(roleName); return userSet!=null && userSet.contains(user.getName()); } /* ------------------------------------------------------------ */ public void logout(Principal user) {} /* ------------------------------------------------------------ */ public String toString() { return "Realm["+_realmName+"]=="+_users.keySet(); } /* ------------------------------------------------------------ */ public void dump(PrintStream out) { out.println(this+":"); out.println(super.toString()); out.println(_roles); } /* ------------------------------------------------------------ */ /** * @return The SSORealm to delegate single sign on requests to. */ public SSORealm getSSORealm() { return _ssoRealm; } /* ------------------------------------------------------------ */ /** Set the SSORealm. * A SSORealm implementation may be set to enable support for SSO. * @param ssoRealm The SSORealm to delegate single sign on requests to. */ public void setSSORealm(SSORealm ssoRealm) { _ssoRealm = ssoRealm; } /* ------------------------------------------------------------ */ public Credential getSingleSignOn(Request request,Response response) { if (_ssoRealm!=null) return _ssoRealm.getSingleSignOn(request,response); return null; } /* ------------------------------------------------------------ */ public void setSingleSignOn(Request request,Response response,Principal principal,Credential credential) { if (_ssoRealm!=null) _ssoRealm.setSingleSignOn(request,response,principal,credential); } /* ------------------------------------------------------------ */ public void clearSingleSignOn(String username) { if (_ssoRealm!=null) _ssoRealm.clearSingleSignOn(username); } /** * @see org.mortbay.component.AbstractLifeCycle#doStart() */ protected void doStart() throws Exception { super.doStart(); if (_scanner!=null) _scanner.stop(); if (getRefreshInterval() > 0) { _scanner = new Scanner(); _scanner.setScanInterval(getRefreshInterval()); List dirList = new ArrayList(1); dirList.add(_configResource.getFile()); _scanner.setScanDirs(dirList); _scanner.setFilenameFilter(new FilenameFilter () { public boolean accept(File dir, String name) { File f = new File(dir,name); try { if (f.compareTo(_configResource.getFile())==0) return true; } catch (IOException e) { return false; } return false; } }); _scanner.addListener(new BulkListener() { public void filesChanged(List filenames) throws Exception { if (filenames==null) return; if (filenames.isEmpty()) return; if (filenames.size()==1 && filenames.get(0).equals(_config)) loadConfig(); } public String toString() { return "HashUserRealm$Scanner"; } }); _scanner.setReportExistingFilesOnStartup(false); _scanner.setRecursive(false); _scanner.start(); } } /** * @see org.mortbay.component.AbstractLifeCycle#doStop() */ protected void doStop() throws Exception { super.doStop(); if (_scanner!=null) _scanner.stop(); _scanner=null; } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private class User implements Principal { List roles=null; /* ------------------------------------------------------------ */ private UserRealm getUserRealm() { return HashUserRealm.this; } public String getName() { return "Anonymous"; } public boolean isAuthenticated() { return false; } public String toString() { return getName(); } } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private class KnownUser extends User { private String _userName; private Credential _cred; /* -------------------------------------------------------- */ KnownUser(String name,Credential credential) { _userName=name; _cred=credential; } /* -------------------------------------------------------- */ boolean authenticate(Object credentials) { return _cred!=null && _cred.check(credentials); } /* ------------------------------------------------------------ */ public String getName() { return _userName; } /* -------------------------------------------------------- */ public boolean isAuthenticated() { return true; } } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private class WrappedUser extends User { private Principal user; private String role; WrappedUser(Principal user, String role) { this.user=user; this.role=role; } Principal getUserPrincipal() { return user; } public String getName() { return "role:"+role; } public boolean isAuthenticated() { return true; } public boolean isUserInRole(String role) { return this.role.equals(role); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -