LDAP認証を使用したSpringBoot REST API

リーM

そこで、LDAP認証を使用するRESTAPIを構築しようとしています。基本的に、ログインエンドポイントが消費されたときに、httpBasic認証を使用して資格情報を検出し、LDAPサーバーに対してそれらの資格情報を使用するようにします。また、ユーザーの役割を考慮に入れて、エンドポイントを保護し、適切な役割を持つ特定のユーザーのみがそのエンドポイントにアクセスできるようにします。

これを行うことは可能ですか?これまでの私の読書では、これを実装する方法を明確に述べているチュートリアルや記事を見たことがありません。

*更新: httpBasicを使用して資格情報を受け入れることができるように構成することができました。ここで、LDAPグループ(マネージャー、開発者など)に基づくユーザーロールを使用して特定のエンドポイントのアクセス許可を設定する方法を知りたいです。

ConnorVanElswyk

これは実際にはSpringBootでかなり簡潔に達成できます。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.so</groupId>
    <artifactId>rest-ldap</artifactId>
    <version>1.0.1</version>
    <name>rest-ldap</name>
    <description>SO REST LDAP Solution</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

App.java

package com.so;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableWebSecurity
@RestController
@SpringBootApplication
public class App extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .httpBasic()
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .csrf()
                .disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .ldapAuthentication()
                .contextSource()
                .url("ldap://ldap-server.com:3268")
                .managerDn("CN=MGR_USERNAME")
                .managerPassword("MGR_PASSWORD")
                .and()
                .userSearchFilter("CN={0}");
    }

    @RequestMapping
    public Authentication getAuth() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

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

}

リクエストを送信 http://USERNAME:PASSWORD@localhost:8080

(成功した)応答を受信する

HTTP/1.1 200
Set-Cookie: JSESSIONID=COOKIE-VALUE; Path=/; HttpOnly
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 01 Jan 1970 00:00:00 GMT
{
  "authorities": [],
  "details": {
    "remoteAddress": "127.0.0.1",
    "sessionId": null
  },
  "authenticated": true,
  "principal": {
    "dn": "cn=USERNAME",
    "password": null,
    "username": "USERNAME",
    "authorities": [],
    "accountNonExpired": true,
    "accountNonLocked": true,
    "credentialsNonExpired": true,
    "enabled": true,
    "timeBeforeExpiration": 2147483647,
    "graceLoginsRemaining": 2147483647
  },
  "credentials": null,
  "name": "USERNAME"
}

楽しい!

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SAMLを使用したREST API認証

分類Dev

Spring RestTemplateを使用したREST APIの基本認証

分類Dev

Azure ADv2.0認証を使用したSharepointOnline REST API

分類Dev

C#認証を使用したAzure Devops REST API

分類Dev

springrestTemplate を使用した POST REST API の認証

分類Dev

D&B APIバージョン2、PHPを使用したREST認証

分類Dev

WP REST API2.0プラグインを使用した基本認証では認証できません

分類Dev

PowerShellを介したREST-API基本認証と呼び出し-WebRequest

分類Dev

ES 7.4.1-認証[Rest API]

分類Dev

Algolia Places REST API が認証しない

分類Dev

スリムな3Rest APIを使用してgoogle-api-php-clientを認証する方法は?

分類Dev

Spring Kafkaを使用したSpring Boot Rest API

分類Dev

Javaを使用したREST / XML API

分類Dev

Curlを使用したAzureStorage Table API REST

分類Dev

PayPal REST API-API認証情報を取得する方法は?

分類Dev

Google CloudEndpointsを使用したAPI認証

分類Dev

外部APIを使用した認証

分類Dev

Spring Boot REST API using LDAP authentication

分類Dev

生体認証APIを使用した顔認証の使用方法

分類Dev

Goを使用してRemote Config REST APIのサービスアカウントを認証する

分類Dev

Django REST APIを使用してAndroidユーザーのPOSTリクエストを認証する方法は?

分類Dev

HP ALM Rest API QCSession411認証

分類Dev

HP ALM Rest API 401認証エラー?

分類Dev

Twitter REST API –「tweet_mode = extended」パラメーターを使用すると「認証できませんでした」エラー

分類Dev

Rails 4 + Rspec:REST APIの親コントローラーを使用したカスタム認証のDRYテスト?

分類Dev

メールとパスワードを使用してREST API経由で認証する[Firebase]

分類Dev

メールとパスワードを使用してREST API経由で認証する[Firebase]

分類Dev

Rails REST APIアプリでDeviseを使用して認証するにはどうすればよいですか?

分類Dev

Androidでandroidx生体認証APIを使用した顔認証

Related 関連記事

  1. 1

    SAMLを使用したREST API認証

  2. 2

    Spring RestTemplateを使用したREST APIの基本認証

  3. 3

    Azure ADv2.0認証を使用したSharepointOnline REST API

  4. 4

    C#認証を使用したAzure Devops REST API

  5. 5

    springrestTemplate を使用した POST REST API の認証

  6. 6

    D&B APIバージョン2、PHPを使用したREST認証

  7. 7

    WP REST API2.0プラグインを使用した基本認証では認証できません

  8. 8

    PowerShellを介したREST-API基本認証と呼び出し-WebRequest

  9. 9

    ES 7.4.1-認証[Rest API]

  10. 10

    Algolia Places REST API が認証しない

  11. 11

    スリムな3Rest APIを使用してgoogle-api-php-clientを認証する方法は?

  12. 12

    Spring Kafkaを使用したSpring Boot Rest API

  13. 13

    Javaを使用したREST / XML API

  14. 14

    Curlを使用したAzureStorage Table API REST

  15. 15

    PayPal REST API-API認証情報を取得する方法は?

  16. 16

    Google CloudEndpointsを使用したAPI認証

  17. 17

    外部APIを使用した認証

  18. 18

    Spring Boot REST API using LDAP authentication

  19. 19

    生体認証APIを使用した顔認証の使用方法

  20. 20

    Goを使用してRemote Config REST APIのサービスアカウントを認証する

  21. 21

    Django REST APIを使用してAndroidユーザーのPOSTリクエストを認証する方法は?

  22. 22

    HP ALM Rest API QCSession411認証

  23. 23

    HP ALM Rest API 401認証エラー?

  24. 24

    Twitter REST API –「tweet_mode = extended」パラメーターを使用すると「認証できませんでした」エラー

  25. 25

    Rails 4 + Rspec:REST APIの親コントローラーを使用したカスタム認証のDRYテスト?

  26. 26

    メールとパスワードを使用してREST API経由で認証する[Firebase]

  27. 27

    メールとパスワードを使用してREST API経由で認証する[Firebase]

  28. 28

    Rails REST APIアプリでDeviseを使用して認証するにはどうすればよいですか?

  29. 29

    Androidでandroidx生体認証APIを使用した顔認証

ホットタグ

アーカイブ