0
我是Apache Shiro和LDAP的新手。 我想使用Apache shiro創建一個簡單的LDAP身份驗證。身份驗證正常,但我無法向用戶添加角色。下面是shiro.ini文件我使用:如何通過LDAP身份驗證使用Apache Shiro添加角色授權
[main]
realm = org.apache.shiro.realm.ldap.JndiLdapRealm
realm.contextFactory.url = ldap://localhost:389
contextFactory = org.apache.shiro.realm.ldap.JndiLdapContextFactory
contextFactory.systemUsername = cn=Manager,dc=maxcrc,dc=com
contextFactory.systemPassword = secret
[roles]
People = *
role = *
Administrator = *
及以下的Java類文件:
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import java.util.ArrayList;
import java.util.List;
import javax.naming.NamingException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.ldap.JndiLdapRealm;
import org.apache.shiro.realm.ldap.LdapContextFactory;
import org.apache.shiro.subject.PrincipalCollection;
public class LDAPTest extends JndiLdapRealm
{
public static final String userName = "uid=aarippa,ou=People,dc=maxcrc,dc=com";
//public static final String userName = "uid=arjunarippa";
public static final String password = "SomePassword";
public static void main(String[] args)
{
Factory<SecurityManager> factory = new IniSecurityManagerFactory("N:\\workspace\\LdapAuthentication\\src\\auth.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
System.out.println("userName is : " +userName);
System.out.println("password is : " +password);
//UsernamePasswordToken token = new UsernamePasswordToken("cn=Panji Pratomo,ou=people,dc=maxcrc,dc=com", "SomePassword");
UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
Subject currentUser = SecurityUtils.getSubject();
//System.out.println(currentUser);
try
{
currentUser.login(token);
System.out.println("We've authenticated! :)");
}
catch (AuthenticationException e)
{
System.out.println("We did not authenticate :(");
e.printStackTrace();
}
if (currentUser.hasRole("people"))
{
System.out.println("We have the role! :)");
}
else
{
System.out.println("We do not have the role :(");
}
if (currentUser.isPermitted("foo.blah"))
{
System.out.println("We're authorized! :)");
}
else
{
System.out.println("We are not authorized :(");
}
}
}
我無法理解如何將角色添加到用戶。身份驗證工作正常,但得到的錯誤消息爲「我們沒有角色:(」和「我們沒有授權:(」 目前我使用的是OpenLDAP服務器,下面是一個.LDIF條目,我做了服務器:
dn: uid=aarippa,ou=people,dc=maxcrc,dc=com
objectclass: inetOrgPerson
cn: Arjun Arippa
cn: A Arippa
cn: Aarippa
sn: fahmi
uid: aarippa
userpassword: SomePassword
carlicense: HISCAR 123
homephone: 555-111-2222
mail: [email protected]
mail: [email protected]
mail: [email protected]
description: tukang ngulik ga jelas
ou: SOA
任何人都可以請讓我知道,如果我加入了正確的角色做了正確的事情,如果我錯了糾正我,我失去的東西的方法寫
感謝。? , Arjun
最初我雖然可以使用LDAP領域。但看到您的評論後,我嘗試使用AD Realm(因爲我仍然不確定要求是使用LDAPRealm還是ADRealm)。所以我正在嘗試使用ADRealm進行身份驗證和角色授權。然而,我收到一個錯誤: '線程中的異常'main'org.apache.shiro.authz.AuthorizationException:嘗試檢索用戶授權時的LDAP命名錯誤[uid = aarippa,ou = people,dc = maxcrc ,DC = COM]。 \t在org.apache.shiro.realm.ldap.AbstractLdapRealm.doGetAuthorizationInfo(AbstractLdapRealm.java:210)' – Arjun
下面是我在OpenLDAP服務器條目: 'DN:UID = aarippa,OU =人,DC = maxcrc, DC = COM 對象類:爲inetOrgPerson CN:阿瓊Arippa CN:甲Arippa CN:Aarippa SN:法赫米 UID:aarippa 的userPassword:SomePassword carlicense:HISCAR 123 HOMEPHONE:555-111 -2222 郵箱:[email protected] 郵箱:[email protected] 郵箱:[email protected] 描述:土炕ngulik GA jelas OU:SOA ' – Arjun
和下面是我進入在shiro.ini文件提出: 'activeDirectoryRealm = org.apache.shiro.realm.activedirectory.ActiveDirectoryRealm activeDirectoryRealm.searchBase = 「OU =人,DC = maxcrc,DC = COM」 activeDirectoryRealm.systemUsername =經理 activeDirectoryRealm .systemPassword = secret activeDirectoryRealm.url = ldap:// localhost:389 activeDirectoryRealm.groupRolesMap =「 ou = People,dc = maxcrc,dc = com「:」sysadmin「 securityManager。realm = $ activeDirectoryRealm activeDirectoryRealm.authorizationCachingEnabled = false' – Arjun