neo4jによるSpring認証

TWM

私はneo4jで春のアプリケーションに取り組んでいます。データベースのユーザー名とパスワードを使用して認証を追加したいのですが。以下に、mysqlを使用してそれを行う方法に関するコードを示します。neo4jを使用した以下のコードに相当するものは何でしょうか。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Bean
    public PasswordEncoder passwordEncoders(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
                .usersByUsernameQuery("SELECT u.name, u.password, 1 FROM user u WHERE u.name=?")
                .authoritiesByUsernameQuery("SELECT u.name, u.role, 1 FROM user u WHERE u.name=?")
                .dataSource(dataSource)
                .passwordEncoder(passwordEncoders());
    }
//...
}

DataSource用のBeanを作成しようとしましたが、BeanCreationExceptionが発生します。これが私が使おうとしたものです

    @Bean
    public DataSource getDataSource(){
        String NEO4J_URL = System.getenv("NEO4J_URL");
        if (NEO4J_URL==null) NEO4J_URL=System.getProperty("NEO4J_URL","jdbc:neo4j:http://localhost:11010");
            return new DriverManagerDataSource(NEO4J_URL);
    }

またはこれ

@Bean
public DataSource getDataSource(){
    DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
    dataSourceBuilder.driverClassName("org.neo4j.driver");
    dataSourceBuilder.url("bolt://localhost:11010");
    dataSourceBuilder.username("neo4j");
    dataSourceBuilder.password("0000");
    return dataSourceBuilder.build();
}
クリストフ・ウィレムセン

AuthenticationProvider指定された資格情報を持つユーザーを取得するためにグラフをクエリできるカスタムセキュリティを登録する必要があります。

このようなプロバイダーは次のようになります。

package com.ikwattro.demo.neo4jauth.security;

import org.neo4j.driver.Driver;
import org.neo4j.driver.Record;
import org.neo4j.driver.Session;
import org.neo4j.driver.types.Node;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class Neo4jAuthenticationProvider implements AuthenticationProvider {

    private final Driver driver;

    public Neo4jAuthenticationProvider(Driver driver) {
        this.driver = driver;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String name = authentication.getName();
        String password = authentication.getCredentials().toString();
        try (Session session = driver.session()) {
            List<Record> results = session.run("MATCH (n:User) WHERE n.username = $name AND n.password = $password RETURN n",
                    Map.of("name", name, "password", password)).list();

            if (results.isEmpty()) {
                return null;
            }

            Node user = results.get(0).get("n").asNode();
            // Possible to add more information from user
            List<GrantedAuthority> authorities = new ArrayList<>();
            authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
            final UserDetails principal = new User(name, password, authorities);

            return new UsernamePasswordAuthenticationToken(principal, password, authorities);
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}

次に、このプロバイダーをセキュリティチェーンのメインAuthenticationProviderとして登録する必要があります。これは、セキュリティ構成で行います。

package com.ikwattro.demo.neo4jauth.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private Neo4jAuthenticationProvider authenticationProvider;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
    }

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

次に、基本認証を使用してアプリケーションにクエリを実行できます

curl --user john:doe localhost:8080/hello

ここで完全に機能するデモを見つけることができます:https//github.com/ikwattro/spring-security-neo4j

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Neo4j距離による空間順序

分類Dev

Neo4j - 関連性による並べ替え

分類Dev

Spring RooでNeo4Jグラフを作成するにはどうすればよいですか?

分類Dev

Springを使用してDocker内でNeo4jに接続する方法は?

分類Dev

Neo4j TimeTree in Spring Data Neo4j 4.0

分類Dev

Neo4j2.2.0-RC01で基本認証を無効にする方法

分類Dev

Spring Securityによる認証

分類Dev

SpringによるRESTful認証

分類Dev

Neo4jで動作するようにApacheShiroを構成する

分類Dev

Neo4jを使用したエンドユーザー認証

分類Dev

Neo4jのリストを確認する

分類Dev

Neo4j、Cypherコマンドによるバルクロード

分類Dev

LuceneがNeo4jでどのように機能するか

分類Dev

浮動小数点数によるNeo4jグループ

分類Dev

性別によるneo4jのタイムスタンプ範囲

分類Dev

neo4j cypher、ノードを検索し、IDによる関係です

分類Dev

Neo4j、Neography:検索をよりスマートにする

分類Dev

RailsとNeo4jによるAPIページネーション

分類Dev

Neo4jのコレクションメンバーによる注文

分類Dev

複数のマージによるneo4jクエリの最適化

分類Dev

Neo4jに `Neo.ClientError.Statement.InvalidType`がある

分類Dev

Neo4j:「NOTCONTAINS」はNeo4jに存在しますか?

分類Dev

REST APIを介してNeo4jでDELETEが成功したかどうかを確認するにはどうすればよいですか?

分類Dev

neo4Jインデックスが使用された回数を確認するにはどうすればよいですか?

分類Dev

Spring data neo4j and query with IN keyword

分類Dev

Cannot configure @Transaction to work with Spring Data Neo4j

分類Dev

Neo4j-正規表現によるCypherEメールの検証

分類Dev

データベースNeo4jにノードが存在するかどうかを確認します

分類Dev

neo4j CREATEUNIQUEは非常に遅いようです

Related 関連記事

  1. 1

    Neo4j距離による空間順序

  2. 2

    Neo4j - 関連性による並べ替え

  3. 3

    Spring RooでNeo4Jグラフを作成するにはどうすればよいですか?

  4. 4

    Springを使用してDocker内でNeo4jに接続する方法は?

  5. 5

    Neo4j TimeTree in Spring Data Neo4j 4.0

  6. 6

    Neo4j2.2.0-RC01で基本認証を無効にする方法

  7. 7

    Spring Securityによる認証

  8. 8

    SpringによるRESTful認証

  9. 9

    Neo4jで動作するようにApacheShiroを構成する

  10. 10

    Neo4jを使用したエンドユーザー認証

  11. 11

    Neo4jのリストを確認する

  12. 12

    Neo4j、Cypherコマンドによるバルクロード

  13. 13

    LuceneがNeo4jでどのように機能するか

  14. 14

    浮動小数点数によるNeo4jグループ

  15. 15

    性別によるneo4jのタイムスタンプ範囲

  16. 16

    neo4j cypher、ノードを検索し、IDによる関係です

  17. 17

    Neo4j、Neography:検索をよりスマートにする

  18. 18

    RailsとNeo4jによるAPIページネーション

  19. 19

    Neo4jのコレクションメンバーによる注文

  20. 20

    複数のマージによるneo4jクエリの最適化

  21. 21

    Neo4jに `Neo.ClientError.Statement.InvalidType`がある

  22. 22

    Neo4j:「NOTCONTAINS」はNeo4jに存在しますか?

  23. 23

    REST APIを介してNeo4jでDELETEが成功したかどうかを確認するにはどうすればよいですか?

  24. 24

    neo4Jインデックスが使用された回数を確認するにはどうすればよいですか?

  25. 25

    Spring data neo4j and query with IN keyword

  26. 26

    Cannot configure @Transaction to work with Spring Data Neo4j

  27. 27

    Neo4j-正規表現によるCypherEメールの検証

  28. 28

    データベースNeo4jにノードが存在するかどうかを確認します

  29. 29

    neo4j CREATEUNIQUEは非常に遅いようです

ホットタグ

アーカイブ