Ldap Auth作为Rest Controller

时间:2019-07-15 12:55:34

标签: spring rest spring-boot spring-security ldap

phpLDAPadmin Schema 我已经配置了一个远程Ldap服务器,我有一个前端,并且预期的行为是:当用户在前端填写登录表单时,我想通过控制器将凭据发送到后端,然后后端应该对我的ldap服务器执行查找并返回标识用户的响应,例如其ID,如果找不到用户,则返回null。

我很难理解这个概念,所有示例都使用本地ldap或重定向到后端的登录表单。我不希望后端使用登录表单或保护某些端点。

1 个答案:

答案 0 :(得分:1)

这就是我在项目中正在做的事情

  

在application.properties文件中

  1. server,protocol = http://
  2. server.host.name =
  3. server.ip =
  4. server.port =
  5. server.url =

然后从RESTController中调用此服务:

  @Service
    public class ldapService
    {
    @Value("${ldap.server.protocol}")
        private String LDAP_SERVER_PROTOCOL;

        @Value("${ldap.server.ip}")
        private String LDAP_SERVER_IP;

        @Value("${ldap.server.port}")
        private int LDAP_SERVER_PORT;

        @Value("${ldap.service.url}")
        private String LDAP_SERVICE_URL;

    public String authenticate(LoginDto loginDto){

            UserCredentials userCredentials = new UserCredentials(loginDto.getUserName(), loginDto.getPassword());

            RestTemplate restTemplate = new RestTemplate();
            HttpEntity<UserCredentials> httpEntity = new HttpEntity<UserCredentials>(userCredentials);

            final String FINAL_URL = LDAP_SERVER_PROTOCOL + LDAP_SERVER_IP + LDAP_SERVER_PORT + LDAP_SERVICE_URL;

            UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(FINAL_URL);


            ResponseEntity<ResponseDto> exchange = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST,
                    httpEntity, ResponseDto.class);

            HttpStatus statusCode = exchange.getStatusCode();
            ResponseDto responseDto = exchange.getBody();

            // check if response OK and is user validated.
            if (statusCode == HttpStatus.OK)
    {
    //switch according to HttpStatus
    }