shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lhazlew...@apache.org
Subject svn commit: r899768 - in /incubator/shiro/trunk/core/src: main/java/org/apache/shiro/config/IniSecurityManagerFactory.java test/java/org/apache/shiro/config/IniSecurityManagerFactoryTest.java
Date Fri, 15 Jan 2010 19:21:35 GMT
Author: lhazlewood
Date: Fri Jan 15 19:21:34 2010
New Revision: 899768

URL: http://svn.apache.org/viewvc?rev=899768&view=rev
Log:
SHIRO-125 - implemented fix by enabling the implicitly created IniRealm to be configurable
in the [main] section.  Added accompanying 'testImplicitIniRealmWithAdditionalRealmConfiguration'
test.

Modified:
    incubator/shiro/trunk/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java
    incubator/shiro/trunk/core/src/test/java/org/apache/shiro/config/IniSecurityManagerFactoryTest.java

Modified: incubator/shiro/trunk/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java?rev=899768&r1=899767&r2=899768&view=diff
==============================================================================
--- incubator/shiro/trunk/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java
(original)
+++ incubator/shiro/trunk/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java
Fri Jan 15 19:21:34 2010
@@ -44,6 +44,7 @@
     public static final String MAIN_SECTION_NAME = "main";
 
     public static final String SECURITY_MANAGER_NAME = "securityManager";
+    public static final String INI_REALM_NAME = "iniRealm";
 
     private static transient final Logger log = LoggerFactory.getLogger(IniSecurityManagerFactory.class);
 
@@ -88,10 +89,13 @@
         return doCreateSecurityManager(ini, mainSection);
     }
 
-    private Map<String, Object> buildMainInstances(Ini.Section main) {
+    private Map<String, Object> buildMainInstances(Ini.Section main, Realm iniRealm)
{
         Map<String, Object> defaults = new LinkedHashMap<String, Object>();
         SecurityManager securityManager = createDefaultInstance();
-        defaults.put("securityManager", securityManager);
+        defaults.put(SECURITY_MANAGER_NAME, securityManager);
+        if (iniRealm != null) {
+            defaults.put(INI_REALM_NAME, iniRealm);
+        }
         return buildInstances(main, defaults);
     }
 
@@ -182,13 +186,21 @@
      * @return a new Realm instance reflecting the account data discovered in the {@code
Ini}.
      */
     protected Realm createRealm(Ini ini) {
-        return new IniRealm(ini);
+        IniRealm realm = new IniRealm(ini);
+        realm.setName(INI_REALM_NAME);
+        return realm;
     }
 
     @SuppressWarnings({"unchecked"})
     protected SecurityManager doCreateSecurityManager(Ini ini, Ini.Section mainSection) {
 
-        Map<String, Object> objects = buildMainInstances(mainSection);
+        Realm iniRealm = null;
+
+        if ( shouldImplicitlyCreateRealm(ini) ) {
+            iniRealm = createRealm(ini);
+        }
+
+        Map<String, Object> objects = buildMainInstances(mainSection, iniRealm);
 
         SecurityManager securityManager = (SecurityManager) objects.get(SECURITY_MANAGER_NAME);
 
@@ -196,11 +208,6 @@
         //initialize the securityManager:
         Collection<Realm> realms = getRealms(objects);
 
-        if (shouldImplicitlyCreateRealm(ini)) {
-            Realm realm = createRealm(ini);
-            realms.add(realm);
-        }
-
         //set them on the SecurityManager
         if (!CollectionUtils.isEmpty(realms)) {
             applyRealmsToSecurityManager(realms, securityManager);

Modified: incubator/shiro/trunk/core/src/test/java/org/apache/shiro/config/IniSecurityManagerFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/test/java/org/apache/shiro/config/IniSecurityManagerFactoryTest.java?rev=899768&r1=899767&r2=899768&view=diff
==============================================================================
--- incubator/shiro/trunk/core/src/test/java/org/apache/shiro/config/IniSecurityManagerFactoryTest.java
(original)
+++ incubator/shiro/trunk/core/src/test/java/org/apache/shiro/config/IniSecurityManagerFactoryTest.java
Fri Jan 15 19:21:34 2010
@@ -18,16 +18,21 @@
  */
 package org.apache.shiro.config;
 
-import static junit.framework.Assert.*;
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.authc.UsernamePasswordToken;
+import org.apache.shiro.crypto.hash.Sha256Hash;
 import org.apache.shiro.mgt.DefaultSecurityManager;
 import org.apache.shiro.mgt.SecurityManager;
 import org.apache.shiro.realm.Realm;
 import org.apache.shiro.realm.text.IniRealm;
 import org.apache.shiro.realm.text.PropertiesRealm;
+import org.apache.shiro.subject.Subject;
 import org.junit.Test;
 
 import java.util.Collection;
 
+import static junit.framework.Assert.*;
+
 /**
  * Unit tests for the {@link IniSecurityManagerFactory} implementation.
  *
@@ -108,5 +113,42 @@
         assertTrue(((IniRealm) realm).accountExists("admin"));
     }
 
+    /**
+     * Test for issue <a href="https://issues.apache.org/jira/browse/SHIRO-125">SHIRO-125</a>.
+     */
+    @Test
+    public void testImplicitIniRealmWithAdditionalRealmConfiguration() {
+
+        Ini ini = new Ini();
+
+        //The users section below should create an implicit 'iniRealm' instance in the
+        //main configuration.  So we should be able to set properties on it immediately
+        //such as the Sha256 credentials matcher:
+        Ini.Section main = ini.addSection("main");
+        main.put("credentialsMatcher", "org.apache.shiro.authc.credential.Sha256CredentialsMatcher");
+        main.put("iniRealm.credentialsMatcher", "$credentialsMatcher");
+
+        //create a users section - user 'admin', with a Sha256-hashed 'admin' password (hex
encoded):
+        Ini.Section users = ini.addSection(IniRealm.USERS_SECTION_NAME);
+        users.put("admin", new Sha256Hash("secret").toString());
+
+        IniSecurityManagerFactory factory = new IniSecurityManagerFactory(ini);
+        SecurityManager sm = factory.getInstance();
+
+        //go ahead and try to log in with the admin user, ensuring the 
+        //iniRealm has a Sha256CredentialsMatcher enabled:
 
+        //try to log-in:
+        Subject subject = new Subject.Builder(sm).buildSubject();
+        //ensure thread clean-up after the login method returns.  Test cases only:
+        subject.execute(new Runnable() {
+            public void run() {
+                //the plain-text 'secret' should be converted to an Sha256 hash first
+                //by the CredentialsMatcher.  This should return quietly if
+                //this test case is valid:
+                SecurityUtils.getSubject().login(new UsernamePasswordToken("admin", "secret"));
+            }
+        });
+        assertTrue(subject.getPrincipal().equals("admin"));
+    }
 }



Mime
View raw message