2014-11-21 30 views
7

我正在构建自己的AuthorizingRealm子类,并且我有一个艰难的时间将它连接到我的SecurityManager书写自定义Shiro区域

我境界的精髓:

public class MyRealm extends AuthorizingRealm { 
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { 
     try { 
      // My custom logic here 

     } catch(Throwable t) { 
      System.out.println(t.getMessage()); 
     } 
     SimpleAuthenticationInfo authn = new SimpleAuthenticationInfo(new MyUser(), "somePassword"); 
     return authn; 
    } 

    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { 
     try { 
      // My custom logic here 
     } catch(Throwable t) { 
      System.out.println(t.getMessage()); 
     } 
     return new SimpleAuthorizationInfo(); 
    } 
} 

然后在我的 'shiro.ini':

# ======================= 
# Shiro INI configuration 
# ======================= 
[main] 
myRealm = com.me.myapp.security.MyRealm 

然后在我的Driver类/ main方法(我使用用于测试) :

public class Driver { 
    public static void main(String[] args) { 
     Driver d = new Driver(); 
     d.test(); 
    } 

    public void test() { 
     Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); 
     SecurityManager securityManager = factory.getInstance(); 
     SecurityUtils.setSecurityManager(securityManager); 

     UsernamePasswordToken token = new UsernamePasswordToken("", ""); 
     token.setRememberMe(true); 

     System.out.println("Shiro props:"); 
     System.out.println(securityManager.getProperties()); 

     Subject currentUser = SecurityUtils.getSubject() 

     try { 
      currentUser.login(token) 

      println "I think this worked!" 
     } catch (UnknownAccountException uae) { 
      println "Exception: ${uae}" 
     } catch (IncorrectCredentialsException ice) { 
      println "Exception: ${ice}" 
     } catch (LockedAccountException lae) { 
      println "Exception: ${lae}" 
     } catch (ExcessiveAttemptsException eae) { 
      println "Exception: ${eae}" 
     } catch (AuthenticationException ae) { 
      println "Exception: ${ae}" 
     } 
    } 
} 

当我运行此我得到:

Shiro props: 
[class:class org.apache.shiro.mgt.DefaultSecurityManager, cacheManager:null, subjectFactory:[email protected], authorizer:[email protected], realms:[[email protected]], subjectDAO:[email protected], rememberMeManager:null, authenticator:[email protected], sessionManager:[email protected]] 
Exception: org.apache.shiro.authc.AuthenticationException: Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - , rememberMe=true]. Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException). 

所以它看起来像它读取我的shiro.ini,因为它提取了正确的领域,但MyRealm不会做任何事情,除了存储应该认证的虚拟用户,不管提供的用户名/密码如何。任何想法,我要去哪里错误?然后securityManager.realms = $myRealm
在Driver类

UsernamePasswordToken token = new UsernamePasswordToken("", "somePassword"); 

,而不是一个空passowrd:

回答

3

你可以看看Stormpath四郎插件在github上的源代码:使用插件here插件here和示例应用程序。

我们已经实施了我们的AuthorizingRealm(与您所需的类似)。你可能有兴趣在考虑看看:

  1. https://github.com/stormpath/stormpath-shiro/blob/master/core/src/main/java/com/stormpath/shiro/realm/ApplicationRealm.java
  2. https://github.com/stormpath/stormpath-shiro-web-sample/blob/master/src/main/webapp/WEB-INF/shiro.ini

顺便说一句,在你shiro.ini你需要补充一点:securityManager.realm = $myRealm

0

添加到您的shiro.ini。

我认为这工作!

+0

感谢@Luca Rasconi,但您的建议不要改变任何东西(与我上面描述的行为相同)。任何其他想法/想法?再次感谢! – smeeb 2014-12-28 09:45:43

0

我没有这个做我自己,但这里有一对夫妇的事情,你可以尝试:

  1. 如果您不需要授权逻辑,考虑继承AuthenticatingRealm代替AuthorizingRealm

  2. 在方法doGetAuthenticationInfo,请考虑使用此代码:

    SimpleAuthenticationInfo authn = new SimpleAuthenticationInfo(token.getPrincipal(),token.getCredentials(),“myRealm”);