简体   繁体   English

Spring LDAP Authentication的用户帐户概念

[英]Spring LDAP Authentication's user account concepts

I am developing an authentication function of a Spring web application. 我正在开发Spring Web应用程序的身份验证功能。 The customer already have an existing Active Directory with their staff data. 客户已经有一个现有的Active Directory及其员工数据。 Any staff in the AD can use their exiting username and password to login into my web application, by which the web app should use the given username and password to get the staff data from the AD and then automatically register the staff with the web app by creating a user account record in DB using the data from the AD. 广告中的任何职员都可以使用其退出的用户名和密码登录到我的Web应用程序,Web应用程序应使用给定的用户名和密码从广告中获取职员数据,然后通过以下方式自动向Web应用程序注册职员使用AD中的数据在DB中创建用户帐户记录。

Following is the above-mentioned actions put in sequence. 以下是依次进行的上述动作。

  1. User submit a login form with username and password. 用户提交带有用户名和密码的登录表单。
  2. Web app query the staff data from AD using the given username and password. Web应用程序使用给定的用户名和密码从AD查询人员数据。
  3. Web app create a user account record in DB using the staff data. Web应用程序使用人员数据在DB中创建用户帐户记录。

I am stuck with step 2. as this is the first time I ever use LDAP, my understanding of the topic is very shallow. 我停留在步骤2。因为这是我第一次使用LDAP,所以我对该主题的理解很浅。

Currently I can successfully list all person names in the AD using the following code. 目前,我可以使用以下代码成功列出广告中的所有人员姓名。

@SpringBootApplication
public class Main implements ApplicationRunner {

    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    @Autowired
    private LdapTemplate ldapTemplate;

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("----------------------");
        logger.info(getAllPersonNames().toString());
        logger.info("----------------------");
    }

    private List getAllPersonNames() {
        EqualsFilter filter = new EqualsFilter("objectclass", "person");
        return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(),
                (AttributesMapper) attrs -> attrs.get("cn").get());
    }

    @Bean
    public LdapContextSource contextSource(Environment env) {
        LdapContextSource contextSource = new LdapContextSource();

        contextSource.setUrl("ldap://localhost:5555");
        contextSource.setBase("DC=myorg,DC=com");
        contextSource.setUserDn("username");
        contextSource.setPassword("password");
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate(Environment env) {
        return new LdapTemplate(contextSource(env));
    }

}

This code has a problem. 此代码有问题。 I put the username and password in the LdapContextSource bean which will be used at application startup time. 我将usernamepassword放在LdapContextSource bean中,它将在应用程序启动时使用。 This is not what I want because the username and password have to be given by the user at runtime. 这不是我想要的,因为usernamepassword必须由用户在运行时提供。

But! 但! I found this example and started confused. 我找到了这个例子 ,开始感到困惑。 In the example, there are 2 set of usernames and passwords, one used to setup the LdapContextSource and one provided by the user at runtime. 在示例中,有LdapContextSource用户名和密码,一组用于设置LdapContextSource ,另一组由用户在运行时提供。

So I think I might have some misunderstanding. 所以我想我可能会有一些误会。 Please help clarify whether the username/password set in LdapContextSource should be provided by the user or I should have a separated username/password only use for my application? 请帮助说明是否应该由用户提供在LdapContextSource中设置的用户名/密码,或者我应该将单独的用户名/密码仅用于我的应用程序?

I'm not sure if my answer will clarify the problem but I'll try anyway. 我不确定我的答案是否可以解决问题,但是我还是会尝试的。

I had the same issue when I wanted to integrate Camunda BPMN in an existing application. 我想将Camunda BPMN集成到现有应用程序中时遇到相同的问题。 And as a beginner, it took me some time to realize how LDAP protocol works. 作为一个初学者,我花了一些时间来了解LDAP协议的工作方式。

Check it, it might be useful: 检查它,可能会有用:

https://docs.camunda.org/manual/7.7/user-guide/process-engine/identity-service/ https://docs.camunda.org/manual/7.7/user-guide/process-engine/identity-service/

If anyone find that I'm wrong, please comment bellow and correct me. 如果有人发现我错了,请在下面发表评论并纠正我。

In fact, the hard coded credentials should be the manager's which will check if the given username and password of any user (dynamically within the app) are accepted. 实际上,硬编码的凭据应该是管理员的凭据,它将检查是否接受了给定用户名和密码(在应用程序内部动态)的任何用户名和密码。

The manager's info is also used to collect LDAP group information that can't be read by a normal user. 管理员的信息还用于收集普通用户无法读取的LDAP组信息。

To bypass this issue, I implemented my own authentication class that tries to connect to LDAP with the user's credentials and if it throws an exception, it means that the given information is wrong. 为了绕过这个问题,我实现了自己的身份验证类,该类尝试使用用户的凭据连接到LDAP,如果抛出异常,则意味着给定的信息是错误的。 However, you will lose the ability to provide the user's group info and so on. 但是,您将无法提供用户的组信息等。

I'm sorry I can't provide the code because I don't have it anymore. 对不起,我无法提供代码,因为我已经没有了。

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM