private Ini.Section getConfigSection(Ini ini) { Ini.Section mainSection = ini.getSection(MAIN_SECTION_NAME); if (CollectionUtils.isEmpty(mainSection)) { //try the default: mainSection = ini.getSection(Ini.DEFAULT_SECTION_NAME); } return mainSection; }
/** * Returns the value of the specified section property, or {@code null} if the section or property do not exist. * * @param sectionName the name of the section to retrieve to acquire the property value * @param propertyName the name of the section property for which to return the value * @return the value of the specified section property, or {@code null} if the section or property do not exist. */ public String getSectionProperty(String sectionName, String propertyName) { Section section = getSection(sectionName); return section != null ? section.get(propertyName) : null; }
/** * Returns {@code true} if the Ini contains account data and a {@code Realm} should be implicitly * {@link #createRealm(Ini) created} to reflect the account data, {@code false} if no realm should be implicitly * created. * * @param ini the Ini instance to inspect for account data resulting in an implicitly created realm. * @return {@code true} if the Ini contains account data and a {@code Realm} should be implicitly * {@link #createRealm(Ini) created} to reflect the account data, {@code false} if no realm should be * implicitly created. */ protected boolean shouldImplicitlyCreateRealm(Ini ini) { return !CollectionUtils.isEmpty(ini) && (!CollectionUtils.isEmpty(ini.getSection(IniRealm.ROLES_SECTION_NAME)) || !CollectionUtils.isEmpty(ini.getSection(IniRealm.USERS_SECTION_NAME))); }
/** * Ensures a section with the specified name exists, adding a new one if it does not yet exist. * * @param sectionName the name of the section to ensure existence * @return the section created if it did not yet exist, or the existing Section that already existed. */ public Section addSection(String sectionName) { String name = cleanName(sectionName); Section section = getSection(name); if (section == null) { section = new Section(name); this.sections.put(name, section); } return section; }
private void processDefinitions(Ini ini) { if (CollectionUtils.isEmpty(ini)) { log.warn("{} defined, but the ini instance is null or empty.", getClass().getSimpleName()); return; } Ini.Section rolesSection = ini.getSection(ROLES_SECTION_NAME); if (!CollectionUtils.isEmpty(rolesSection)) { log.debug("Discovered the [{}] section. Processing...", ROLES_SECTION_NAME); processRoleDefinitions(rolesSection); } Ini.Section usersSection = ini.getSection(USERS_SECTION_NAME); if (!CollectionUtils.isEmpty(usersSection)) { log.debug("Discovered the [{}] section. Processing...", USERS_SECTION_NAME); processUserDefinitions(usersSection); } else { log.info("{} defined, but there is no [{}] section defined. This realm will not be populated with any " + "users and it is assumed that they will be populated programatically. Users must be defined " + "for this Realm instance to be useful.", getClass().getSimpleName(), USERS_SECTION_NAME); } } }
/** * A convenience method that sets the {@link #setFilterChainDefinitionMap(java.util.Map) filterChainDefinitionMap} * property by accepting a {@link java.util.Properties Properties}-compatible string (multi-line key/value pairs). * Each key/value pair must conform to the format defined by the * {@link FilterChainManager#createChain(String,String)} JavaDoc - each property key is an ant URL * path expression and the value is the comma-delimited chain definition. * * @param definitions a {@link java.util.Properties Properties}-compatible string (multi-line key/value pairs) * where each key/value pair represents a single urlPathExpression-commaDelimitedChainDefinition. */ public void setFilterChainDefinitions(String definitions) { Ini ini = new Ini(); ini.load(definitions); //did they explicitly state a 'urls' section? Not necessary, but just in case: Ini.Section section = ini.getSection(IniFilterChainResolverFactory.URLS); if (CollectionUtils.isEmpty(section)) { //no urls section. Since this _is_ a urls chain definition property, just assume the //default section contains only the definitions: section = ini.getSection(Ini.DEFAULT_SECTION_NAME); } setFilterChainDefinitionMap(section); }
Section section = this.getSection(entry.getKey()); if (section == null) { section = addSection(entry.getKey());
/** * Sets a name/value pair for the section with the given {@code sectionName}. If the section does not yet exist, * it will be created. If the {@code sectionName} is null or empty, the name/value pair will be placed in the * default (unnamed, empty string) section. * * @param sectionName the name of the section to add the name/value pair * @param propertyName the name of the property to add * @param propertyValue the property value */ public void setSectionProperty(String sectionName, String propertyName, String propertyValue) { String name = cleanName(sectionName); Section section = getSection(name); if (section == null) { section = addSection(name); } section.put(propertyName, propertyValue); }
protected void buildChains(FilterChainManager manager, Ini ini) { //filters section: Ini.Section section = ini.getSection(FILTERS); if (!CollectionUtils.isEmpty(section)) { String msg = "The [{}] section has been deprecated and will be removed in a future release! Please " + "move all object configuration (filters and all other objects) to the [{}] section."; log.warn(msg, FILTERS, IniSecurityManagerFactory.MAIN_SECTION_NAME); } Map<String, Object> defaults = new LinkedHashMap<String, Object>(); Map<String, Filter> defaultFilters = manager.getFilters(); //now let's see if there are any object defaults in addition to the filters //these can be used to configure the filters: //create a Map of objects to use as the defaults: if (!CollectionUtils.isEmpty(defaultFilters)) { defaults.putAll(defaultFilters); } //User-provided objects must come _after_ the default filters - to allow the user-provided //ones to override the default filters if necessary. Map<String, ?> defaultBeans = getDefaults(); if (!CollectionUtils.isEmpty(defaultBeans)) { defaults.putAll(defaultBeans); } Map<String, Filter> filters = getFilters(section, defaults); //add the filters to the manager: registerFilters(filters, manager); //urls section: section = ini.getSection(URLS); createChains(section, manager); }
protected FilterChainResolver createFilterChainResolver() { FilterChainResolver resolver = null; Ini ini = getIni(); if (!CollectionUtils.isEmpty(ini)) { //only create a resolver if the 'filters' or 'urls' sections are defined: Ini.Section urls = ini.getSection(IniFilterChainResolverFactory.URLS); Ini.Section filters = ini.getSection(IniFilterChainResolverFactory.FILTERS); if (!CollectionUtils.isEmpty(urls) || !CollectionUtils.isEmpty(filters)) { //either the urls section or the filters section was defined. Go ahead and create the resolver: Factory<FilterChainResolver> factory = (Factory<FilterChainResolver>) this.objects.get(FILTER_CHAIN_RESOLVER_NAME); if (factory instanceof IniFactorySupport) { IniFactorySupport iniFactory = (IniFactorySupport) factory; iniFactory.setIni(ini); iniFactory.setDefaults(this.objects); } resolver = factory.getInstance(); } } return resolver; }
protected void applyFilterChainResolver(Ini ini, Map<String, ?> defaults) { if (ini == null || ini.isEmpty()) { //nothing to use to create the resolver, just return //(the AbstractShiroFilter allows a null resolver, in which case the original FilterChain is // always used). return; } //only create a resolver if the 'filters' or 'urls' sections are defined: Ini.Section urls = ini.getSection(IniFilterChainResolverFactory.URLS); Ini.Section filters = ini.getSection(IniFilterChainResolverFactory.FILTERS); if ((urls != null && !urls.isEmpty()) || (filters != null && !filters.isEmpty())) { //either the urls section or the filters section was defined. Go ahead and create the resolver //and set it: IniFilterChainResolverFactory filterChainResolverFactory = new IniFilterChainResolverFactory(ini, defaults); filterChainResolverFactory.setFilterConfig(getFilterConfig()); FilterChainResolver resolver = filterChainResolverFactory.getInstance(); setFilterChainResolver(resolver); } }
protected void configureShiro() { final Ini config = new Ini(); config.addSection("users"); config.getSection("users").put("pierre", "password, creditor"); config.getSection("users").put("stephane", "password, refunder"); config.addSection("roles"); config.getSection("roles").put("creditor", Permission.INVOICE_CAN_CREDIT.toString() + "," + Permission.INVOICE_CAN_ITEM_ADJUST.toString()); config.getSection("roles").put("refunder", Permission.PAYMENT_CAN_REFUND.toString()); // Reset the security manager ThreadContext.unbindSecurityManager(); final Factory<SecurityManager> factory = new IniSecurityManagerFactory(config); final SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); } }
private Ini.Section getConfigSection(Ini ini) { Ini.Section mainSection = ini.getSection(MAIN_SECTION_NAME); if (CollectionUtils.isEmpty(mainSection)) { //try the default: mainSection = ini.getSection(Ini.DEFAULT_SECTION_NAME); } return mainSection; }
@Override public Ini.Section getObject() throws Exception { Ini ini = new Ini(); ini.load(filterChainDefinitions); Ini.Section section = ini.getSection("urls"); if (CollectionUtils.isEmpty(section)) { section = ini.getSection(""); } return section; }
/** * Returns {@code true} if the Ini contains account data and a {@code Realm} should be implicitly * {@link #createRealm(Ini) created} to reflect the account data, {@code false} if no realm should be implicitly * created. * * @param ini the Ini instance to inspect for account data resulting in an implicitly created realm. * @return {@code true} if the Ini contains account data and a {@code Realm} should be implicitly * {@link #createRealm(Ini) created} to reflect the account data, {@code false} if no realm should be * implicitly created. */ protected boolean shouldImplicitlyCreateRealm(Ini ini) { return !CollectionUtils.isEmpty(ini) && (!CollectionUtils.isEmpty(ini.getSection(IniRealm.ROLES_SECTION_NAME)) || !CollectionUtils.isEmpty(ini.getSection(IniRealm.USERS_SECTION_NAME))); }
private void processDefinitions(Ini ini) { if (CollectionUtils.isEmpty(ini)) { log.warn("{} defined, but the ini instance is null or empty.", getClass().getSimpleName()); return; } Ini.Section rolesSection = ini.getSection(ROLES_SECTION_NAME); if (!CollectionUtils.isEmpty(rolesSection)) { log.debug("Discovered the [{}] section. Processing...", ROLES_SECTION_NAME); processRoleDefinitions(rolesSection); } Ini.Section usersSection = ini.getSection(USERS_SECTION_NAME); if (!CollectionUtils.isEmpty(usersSection)) { log.debug("Discovered the [{}] section. Processing...", USERS_SECTION_NAME); processUserDefinitions(usersSection); } else { log.info("{} defined, but there is no [{}] section defined. This realm will not be populated with any " + "users and it is assumed that they will be populated programatically. Users must be defined " + "for this Realm instance to be useful.", getClass().getSimpleName(), USERS_SECTION_NAME); } } }
private SecurityManager createSecurityManager(Ini ini) { Ini.Section mainSection = ini.getSection(MAIN_SECTION_NAME); if (CollectionUtils.isEmpty(mainSection)) { //try the default: mainSection = ini.getSection(Ini.DEFAULT_SECTION_NAME); } return createSecurityManager(ini, mainSection); }
/** * Using the same logic as in {@link IniRealm}. */ public PermissionToRoleMapperFromIni(Ini ini) { Map<String,String> section = ini.getSection(IniRealm.ROLES_SECTION_NAME); this.permissionsByRole = Maps.transformEntries(section, new EntryTransformer<String,String,List<String>>() { @Override public List<String> transformEntry(String key, String value) { return Lists.newArrayList(PermissionUtils.toPermissionStrings(value)); } }); }
/** * Returns the value of the specified section property, or {@code null} if the section or property do not exist. * * @param sectionName the name of the section to retrieve to acquire the property value * @param propertyName the name of the section property for which to return the value * @return the value of the specified section property, or {@code null} if the section or property do not exist. */ public String getSectionProperty(String sectionName, String propertyName) { Section section = getSection(sectionName); return section != null ? section.get(propertyName) : null; }
protected void configureShiro() { final Ini config = new Ini(); config.addSection("users"); config.getSection("users").put("pierre", "password, creditor"); config.getSection("users").put("stephane", "password, refunder"); config.addSection("roles"); config.getSection("roles").put("creditor", Permission.INVOICE_CAN_CREDIT.toString() + "," + Permission.INVOICE_CAN_ITEM_ADJUST.toString()); config.getSection("roles").put("refunder", Permission.PAYMENT_CAN_REFUND.toString()); // Reset the security manager ThreadContext.unbindSecurityManager(); final Factory<SecurityManager> factory = new IniSecurityManagerFactory(config); final SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); } }