protected Realm iniRealmFromLocation(String iniLocation) { Ini ini = Ini.fromResourcePath(iniLocation); return new IniRealm( ini ); } }
/** * Creates a {@code Realm} from the Ini instance containing account data. * * @param ini the Ini instance from which to acquire the account data. * @return a new Realm instance reflecting the account data discovered in the {@code Ini}. */ protected Realm createRealm(Ini ini) { //IniRealm realm = new IniRealm(ini); changed to support SHIRO-322 IniRealm realm = new IniRealm(); realm.setName(INI_REALM_NAME); realm.setIni(ini); //added for SHIRO-322 return realm; }
Ini ini = getIni(); String resourcePath = getResourcePath(); ini = Ini.fromResourcePath(resourcePath); if (!CollectionUtils.isEmpty(ini)) { setIni(ini); processDefinitions(ini);
@Test public void testIniFileWithoutUsers() { IniRealm realm = new IniRealm(); realm.setResourcePath("classpath:org/apache/shiro/realm/text/IniRealmTest.noUsers.ini"); realm.init(); assertTrue(realm.roleExists("admin")); } }
@Test public void testIniFile() { IniRealm realm = new IniRealm(); realm.setResourcePath("classpath:org/apache/shiro/realm/text/IniRealmTest.simple.ini"); realm.init(); assertTrue(realm.roleExists("admin")); UsernamePasswordToken token = new UsernamePasswordToken("user1", "user1"); AuthenticationInfo info = realm.getAuthenticationInfo(token); assertNotNull(info); assertTrue(realm.hasRole(info.getPrincipals(), "admin")); }
/** * Creates a {@code Realm} from the Ini instance containing account data. * * @param ini the Ini instance from which to acquire the account data. * @return a new Realm instance reflecting the account data discovered in the {@code Ini}. */ protected Realm createRealm(Ini ini) { IniRealm realm = new IniRealm(ini); realm.setName(INI_REALM_NAME); return realm; } }
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); } } }
/** * This constructor will immediately process the definitions in the {@code Ini} argument. If you need to perform * additional configuration before processing (e.g. setting a permissionResolver, etc), do not call this * constructor. Instead, do the following: * <ol> * <li>Call the default no-arg constructor</li> * <li>Set the Ini instance you wish to use via {@code #setIni}</li> * <li>Set any other configuration properties</li> * <li>Call {@link #init()}</li> * </ol> * * @param ini the Ini instance which will be inspected to create accounts, groups and permissions for this realm. */ public IniRealm(Ini ini) { this(); processDefinitions(ini); }
@Override protected void onInit() { // This is an in-memory realm only - no need for an additional cache when we're already // as memory-efficient as we can be. String resourcePath = getResourcePath(); if (CollectionUtils.isEmpty(this.users) && CollectionUtils.isEmpty(this.roles)) { //no account data manually populated - try the resource path: if (StringUtils.hasText(resourcePath)) { log.debug("Resource path {} defined. Creating INI instance.", resourcePath); Ini ini = Ini.fromResourcePath(resourcePath); processDefinitions(ini); } else { throw new IllegalStateException("No resource path was specified. Cannot load account data."); } } else { if (StringUtils.hasText(resourcePath)) { log.warn("Users or Roles are already populated. Resource path property will be ignored."); } } }
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); } } }
/** * This constructor will immediately process the definitions in the {@code Ini} resolved from the specified * {@code resourcePath}. If you need to perform additional configuration before processing (e.g. setting a * permissionResolver, etc), do not call this constructor. Instead, do the following: * <ol> * <li>Call the default no-arg constructor</li> * <li>Set the Ini instance you wish to use via {@code #setIni}</li> * <li>Set any other configuration properties</li> * <li>Call {@link #init()}</li> * </ol> * * @param resourcePath the resource path of the Ini config which will be inspected to create accounts, groups and * permissions for this realm. */ public IniRealm(String resourcePath) { this(); Ini ini = Ini.fromResourcePath(resourcePath); this.ini = ini; this.resourcePath = resourcePath; processDefinitions(ini); }
@Test public void testNullIni() { IniRealm realm = new IniRealm((Ini) null); }
/** * Creates a {@code Realm} from the Ini instance containing account data. * * @param ini the Ini instance from which to acquire the account data. * @return a new Realm instance reflecting the account data discovered in the {@code Ini}. */ protected Realm createRealm(Ini ini) { //IniRealm realm = new IniRealm(ini); changed to support SHIRO-322 IniRealm realm = new IniRealm(); realm.setName(INI_REALM_NAME); realm.setIni(ini); //added for SHIRO-322 return realm; }
Ini ini = getIni(); String resourcePath = getResourcePath(); ini = Ini.fromResourcePath(resourcePath); if (!CollectionUtils.isEmpty(ini)) { setIni(ini); processDefinitions(ini);
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); } } }
/** * This constructor will immediately process the definitions in the {@code Ini} argument. If you need to perform * additional configuration before processing (e.g. setting a permissionResolver, etc), do not call this * constructor. Instead, do the following: * <ol> * <li>Call the default no-arg constructor</li> * <li>Set the Ini instance you wish to use via {@code #setIni}</li> * <li>Set any other configuration properties</li> * <li>Call {@link #init()}</li> * </ol> * * @param ini the Ini instance which will be inspected to create accounts, groups and permissions for this realm. */ public IniRealm(Ini ini) { this(); processDefinitions(ini); }
protected static SecurityManager createTestSecurityManager() { Ini ini = new Ini(); ini.setSectionProperty("users", "test", "test"); return new DefaultSecurityManager(new IniRealm(ini)); }
@Override public Realm createRealm(Injector injector) { Ini ini = new Ini(); if (users != null && !users.isEmpty()) { ini.addSection("users").putAll(users); } if (roles != null && !roles.isEmpty()) { ini.addSection("roles").putAll(roles); } IniRealm realm = new IniRealm(ini); realm.setIni(ini); if (name != null) { realm.setName(name); } return realm; } }
/** * This constructor will immediately process the definitions in the {@code Ini} resolved from the specified * {@code resourcePath}. If you need to perform additional configuration before processing (e.g. setting a * permissionResolver, etc), do not call this constructor. Instead, do the following: * <ol> * <li>Call the default no-arg constructor</li> * <li>Set the Ini instance you wish to use via {@code #setIni}</li> * <li>Set any other configuration properties</li> * <li>Call {@link #init()}</li> * </ol> * * @param resourcePath the resource path of the Ini config which will be inspected to create accounts, groups and * permissions for this realm. */ public IniRealm(String resourcePath) { this(); Ini ini = Ini.fromResourcePath(resourcePath); this.ini = ini; this.resourcePath = resourcePath; processDefinitions(ini); }