2016-09-15 125 views
0

我正在使用Shiro使用ActiveDirectoryRealm對Active Directory進行身份驗證。這部分工作正常,我可以登錄。如何在沒有系統用戶的情況下搜索Shiro中的AD組?

但是,我無法搜索角色/組。

我懷疑這是因爲我沒有配置systemUsername/systemPassword。我也沒有這個選擇。

如果我使用像LdapAdmin這樣的應用程序,我必須將我的電子郵件和密碼放在身份驗證字段中才能連接和瀏覽。

LdapAdmin Connection Properties

當我使用Spring Security我沒有提供任何這種 「systemUser」。我猜測它使用我提供的相同的用戶名/密碼憑據登錄。

如何配置Shiro執行相同操作?

請參閱下面的粗略版本,瞭解我的shiro.ini的外觀。

adRealm = org.apache.shiro.realm.activedirectory.ActiveDirectoryRealm 
adRealm.url = ldap://my.ad.url:389 
[email protected] 
adRealm.systemUsername= 
adRealm.systemPassword= 
adRealm.searchBase = "OU=org,DC=example,DC=com" 
adRealm.groupRolesMap = "CN=admins":"admin" 

回答

0

目前它不支持這一點。

可以改進當前的實現,以在檢查身份驗證時查詢角色信息。 (這是境界能夠訪問用戶的憑據上止點)

+0

是否可以進行子分類/覆蓋以提供此功能? – opticyclic

+1

絕對,看看這個[線程](http://shiro-user.582556.n2.nabble.com/How-to-set-a-custom-principal-object-td1090270.html)。它並不能解決你確切的問題,但使用這種方法是解決問題的一種方法。 就你而言,你可以在登錄時創建一個自定義主體,查詢角色並在你的主體上調用某種setRoles()方法。然後重寫'getAuthroizationInfo()'並用您的委託人的自定義'getRoles()'的結果構建'AuthenticationInfo'對象。 –

0

正如評論所說,我做了(延伸ActiveDirectoryRealm)日誌中搜索的角色,然後直接訪問它們以後,而不是自定義領域試圖使用系統用戶進行搜索。

凸顯代碼如下,完整代碼在GitHub

/** 
    * This is called during the log in process. 
    * Authenticate but also store the roles/groups on a custom principal 
    */ 
    @Override 
    protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken token, LdapContextFactory ldapContextFactory) throws NamingException { 
    SimpleAuthenticationInfo authenticationInfo = (SimpleAuthenticationInfo)super.queryForAuthenticationInfo(token, ldapContextFactory); 
    PrincipalCollection principals = authenticationInfo.getPrincipals(); 

    UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken)token; 
    String username = usernamePasswordToken.getUsername(); 
    String userPrincipalName = getUserPrincipalName(token); 

    Set<String> roleNames; 
    // Binds using the username and password provided by the user. 
    LdapContext ldapContext = null; 
    try { 
     ldapContext = ldapContextFactory.getLdapContext(userPrincipalName, (usernamePasswordToken.getPassword())); 
     roleNames = getRoleNamesForUser(username, ldapContext); 
    } finally { 
     LdapUtils.closeContext(ldapContext); 
    } 

    List<UserPrincipal> customPrincipals = getCustomPrincipals(userPrincipalName, roleNames); 

    //Merge the custom principals and the main principals 
    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(customPrincipals, CustomActiveDirectoryRealm.class.getSimpleName()); 
    principalCollection.addAll(principals); 

    authenticationInfo.setPrincipals(principalCollection); 

    return authenticationInfo; 
    } 


    /** 
    * This is called during checks for hasRole. 
    * Use the roles that we found on login 
    */ 
    @Override 
    protected AuthorizationInfo queryForAuthorizationInfo(PrincipalCollection principals, LdapContextFactory ldapContextFactory) throws NamingException { 
    UserPrincipal availablePrincipal = (UserPrincipal)getAvailablePrincipal(principals); 
    Set<String> roleNames = availablePrincipal.getRoleNames(); 

    return buildAuthorizationInfo(roleNames); 
    } 
相關問題