当前位置: 首页 > 知识库问答 >
问题:

无法获得spring security with ldap的权限

贺刚毅
2023-03-14

我正在尝试用spring Security授权用户。到目前为止,我能够通过ldap对它们进行身份验证,但还无法获得授权。

我还将ldap与Apache Directory Studio连接起来,查询用户信息,可以在用户中找到权限。但spring security ldap配置无法收集它们。

下面是application-config.xml的ldap部分:

<sec:authentication-manager>
  <sec:ldap-authentication-provider 
    user-dn-pattern="uid={0},ou=myComp,o=myComp,dc=myComp,dc=ldap"
    group-search-base="dc=myComp,dc=ldap"
    group-search-filter="uniqueMember={0}"
    role-prefix="none" />

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class LdapTest2 {

    public static void main(String[] args) {
        Hashtable env = new Hashtable(11);
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://myCompldap.mycomp.local:389/");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "uid=myUser,ou=myComp,o=myComp,dc=myComp,dc=ldap");
        env.put(Context.SECURITY_CREDENTIALS, "myPass");

        try {
            LdapContext ctx = new InitialLdapContext(env, null);
            ctx.setRequestControls(null);
            NamingEnumeration<?> namingEnum = ctx.search("uid=myUser,ou=myComp,o=myComp,dc=myComp,dc=ldap", "(objectClass=*)", getSimpleSearchControls());
            while (namingEnum.hasMore ()) {
                SearchResult result = (SearchResult) namingEnum.next ();
                Attributes attrs = result.getAttributes ();
                Attribute lattr = attrs.get("nsrole");

                NamingEnumeration allRoles = lattr.getAll();
                while(allRoles.hasMore()) {
                    Object role = allRoles.next();
                    System.out.println(role);
                }

            } 
            namingEnum.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static SearchControls getSimpleSearchControls() {
        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        searchControls.setTimeLimit(30000);
        String[] attrIDs = {"nsRole"};
        searchControls.setReturningAttributes(attrIDs);
        return searchControls;
    }

}

共有1个答案

彭宜人
2023-03-14

由于用户角色是用户目录条目的一部分,您应该能够使用自定义的UserDetailsContextMapper加载它们。将自定义角色加载代码移动到MapUserFromContext方法。您可以使用nullldapauthoritiespopulator跳过对角色的正常搜索。

您需要显式配置身份验证提供程序:

<bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
   <constructor-arg>
       <bean class="org.springframework.security.ldap.authentication.BindAuthenticator">
           <constructor-arg ref="contextSource"/>
           <property name="userDnPatterns">
             <list><value>uid={0},ou=myComp,o=myComp,dc=myComp,dc=ldap</value></list>
           </property>
       </bean>
   </constructor-arg>
   <constructor-arg>
       <bean class="org.springframework.security.ldap.authentication.NullLdapAuthoritiesPopulator" />
   </constructor-arg>
   <property name="userDetailsContextMapper" ref="yourContextMapperImplementation" />
</bean>
 类似资料:
  • 编辑:此处不允许的消息不再显示。 我正在使用Volley访问google books,api是IcecreamSandwich。即使包括对清单文件的权限,我仍然会得到关于权限的异常。我已经读了一些已回答的问题(这个,这个)。当光标指向时,表示此处不允许使用该标记。我该如何解决问题呢? logcat:

  • 我试图从服务器上拉取一个食谱列表,该服务器期望从本地存储获得授权令牌。我创建了一个本地axiosAnd Auth文件,因为我将从API中访问多个endpoint。我的授权文件看起来像这样... 然后,我将此axios调用与Redux操作一起使用,以便返回的数据更新项目的存储。我尝试过用两种不同的方式调用API。一个异步等待,看起来像这样。。。 另一个没有异步等待,看起来像这样。。。 我也像这样导入

  • 获取访问攻击是网络渗透测试的第二部分。 在本节中,我们将连接到网络,这将使我们能够发起更强大的攻击并获得更准确的信息。 如果网络不使用加密,我们可以连接到它并嗅出未加密的数据。 如果网络是有线的,我们可以使用电缆并连接到它,可能通过更改我们的MAC地址。唯一的问题是攻击目标使用加密,如WEP,WPA,WPA2。 如果我们确实遇到加密数据,我们需要知道解密它的密钥,这在本章将会学习和实践。 如果网络

  • 在尝试执行GET到发布者存储库时,我正在执行GET和无限循环。 出版商: 书: 完整代码可在此处获得: https://github.com/vanyasav/library

  • 为了请求访问令牌,客户端从资源所有者获得授权。授权表现为授权许可的形式,客户端用它请求访问令牌。OAuth定义了四种许可类型:授权码、隐式许可、资源所有者密码凭据和客户端凭据。它还提供了扩展机制定义其他许可类型。

  • 我使用的是keycloak:4.8.3.final UserService(作为service-account)在service-account-roles选项卡中分配了必要的客户端角色 每当有人试图访问InventoryService的APIs时,我都会执行以下操作: 从授权标头获取访问令牌 获取RPT(如本文所述) 检查RPT是否存在所需的权限。 顺便说一句,另一个观察是:当令牌属于用户(而不