📄 securityhelper.java
字号:
/* * SecurityManager.java * * Created on 2007-10-30, 10:30:17 * * To change this template, choose Tools | Templates * and open the template in the editor. */package com.s7turn.jaas;import com.s7turn.sdk.content.ContentException;import com.s7turn.sdk.content.ContentFactory;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.Map;import java.util.HashMap;import java.util.Iterator;import java.util.List;import javax.servlet.FilterConfig;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import com.s7turn.logging.Logger;/** * * @author Long */public class SecurityHelper { //public static final String CONTENT_FACTORY = "com.s7turn.contentFactory"; private static Logger logger = Logger.getLogger( SecurityHelper.class ); private Map<String, PageSecurity> securityInstance = new HashMap<String, PageSecurity>(); private Map<String, PageSecurity> configInstance = new HashMap<String, PageSecurity>(); private Map<String, String> exceptions = new HashMap<String, String>(); private ContentFactory factory; private Map<String, String> contentPattern = new HashMap<String, String>(); private Map<String, String> editContentTemplate = new HashMap<String, String>(); private SecurityProvider securityProvider = null; private static SecurityHelper instance = new SecurityHelper(); private SecurityHelper() { } public static SecurityHelper getInstance() { return instance; } public boolean hasPermission( Identity identity, String page ) throws LoginException { PageSecurity sec = securityInstance.get( page ); if( sec == null ) { Iterator<Map.Entry<String, PageSecurity>> iter = configInstance.entrySet().iterator(); while( iter.hasNext() ) { Map.Entry<String, PageSecurity> entry = iter.next(); PageSecurity sep = entry.getValue(); if( matchPattern( page, sep.getPage() ) ) { List excepts = sep.getExceptPages(); Iterator<String> iterExcept = excepts.iterator(); boolean excepted = false; while( iterExcept.hasNext() ) { String exceptPage = iterExcept.next(); if( matchPattern( page, exceptPage ) ) { ///wait for next page excepted = true; break; } } if( excepted == false ) { sec = new PageSecurity(); sep.copyPermissionTo( sec ); sec.setPage(page); securityInstance.put(page, sec); } } } } if( sec != null ) { if( sec.isLoginRequired() ) { if( identity == null || !identity.isLoggedIn() ) { throw new LoginException( "login-required" ); } Map<String, Boolean> allowUsers = sec.getAllowUser(); //////check the user name match. Boolean allowUser = allowUsers.get( identity.getUserName() ); if( allowUser != null ) { ////process this directly. return allowUser.booleanValue(); } ////process users Iterator<Map.Entry<String, Boolean>> allowIters = allowUsers.entrySet().iterator(); boolean allowed = false; while( allowIters.hasNext() ) { Map.Entry<String, Boolean> mp = allowIters.next(); String user = mp.getKey(); if( matchPattern( identity.getUserName(), user ) ) { if( !mp.getValue().booleanValue() ) { return false; } allowed = mp.getValue().booleanValue(); } } if ( allowed == true ) { return true; } //Boolean allowRole = sec.getAllowRoles().get( ); Map<String, Boolean> allowRoles = sec.getAllowRoles(); Iterator<Map.Entry<String, Boolean>> allowRolesIter = allowRoles.entrySet().iterator(); allowed = false; Boolean bl = allowRoles.get("*"); if( bl != null ) { allowed = bl.booleanValue(); } while( allowRolesIter.hasNext() ) { Map.Entry<String, Boolean> roleSec = allowRolesIter.next(); if( identity.isUserInRole( roleSec.getKey() ) ) { allowed = roleSec.getValue().booleanValue(); } } return allowed; } } return true; } public SecurityProvider getSecurityProvider() { return securityProvider; } public String getExceptionMappedPage(String emp) { return exceptions.get(emp); } public void addPageSecurityConfig(PageSecurity page) { configInstance.put(page.getPage(), page); } public void addPageSecurityConfig( Element element ) { String viewId = element.getAttribute( "view-id" ); String loginRequired = element.getAttribute("login-required"); if( viewId != null && viewId.trim().length() > 0 ) { PageSecurity page = new PageSecurity(); page.setPage( viewId ); page.setLoginRequired( "true".equalsIgnoreCase(loginRequired) ); NodeList allows = element.getElementsByTagName("allow"); if( allows != null ) { for( int i = 0; i < allows.getLength(); i ++ ) { Element elem = (Element) allows.item(i); String user = elem.getAttribute("user"); String role = elem.getAttribute("role"); if( user != null && user.trim().length() > 0 ) { page.addAllowUser(user); } if( role != null && role.trim().length() > 0 ) { page.addAllowRoles(role); } } } NodeList dennieds = element.getElementsByTagName("dennied"); if( dennieds != null ) { for( int i = 0; i < dennieds.getLength(); i ++ ) { Element elem = (Element) dennieds.item(i); String user = elem.getAttribute("user"); String role = elem.getAttribute("role"); if( user != null && user.trim().length() > 0 ) { page.addDenniedUser(user); } if( role != null && role.trim().length() > 0 ) { page.addDenniedRole(role); } } } NodeList excepts = element.getElementsByTagName("except"); if( excepts != null ) { for( int i = 0; i < excepts.getLength(); i ++ ) { Element elem = (Element) excepts.item(i); String expId = elem.getAttribute("view-id"); if( expId == null || expId.trim().length() == 0 ) { expId = elem.getTextContent(); } page.addExceptPages(expId); } } addPageSecurityConfig(page); } } private boolean matchPattern( String org, String pattern )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -