2016-09-15 3 views
0

Shiroを使用してActive Directoryに対してActiveDirectoryRealmを使用して認証しています。この部分は正常に動作し、ログインできます。システムユーザーなしでShiroのADグループを検索するにはどうすればよいですか?

ただし、ロール/グループを検索することはできません。

systemUsername/systemPasswordが設定されていないと思われます。私はその選択肢もありません。

LdapAdminのようなアプリケーションを使用する場合は、接続と参照を行うためにメールボックスとパスワードを認証フィールドに入れなければなりません。

LdapAdmin Connection Properties

私は春のセキュリティを使用する場合、私はそのような「systemUser」を供給する必要はありません。私はそれが私がログインするために供給したものと同じユーザ名/パスワード資格を使用していると推測しています。

同じようにShiroを設定するにはどうすればよいですか?

私のがどのように見えるかについては、以下を参照してください。

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); 
    } 
関連する問題