【问题标题】:User authentication with spring security and Neo4j使用 Spring Security 和 Neo4j 进行用户身份验证
【发布时间】:2017-12-21 03:01:47
【问题描述】:

我有一个 sprig 启动应用程序和一个 neo4J 数据库。 我的 application.properties 文件如下所示:

spring.data.neo4j.uri = http://127.0.0.1:7474
spring.data.neo4j.username = neo4j
spring.data.neo4j.password = neo4jpass

应用程序有一个基本用户:

@NodeEntity
public class User {

  @GraphId
  private Long id;

  @Property (name="username")
  private String username;

  @Property (name="password")
  private String password;

  @Property (name="name")
  private String name;

  @Property (name="role")
  private String role;
}

一个简单的用户存储库:

public interface UserRepository extends GraphRepository<User>{
}

我目前的spring安全配置是:

@Configuration
@EnableWebSecurity
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/","/index").permitAll()
            .anyRequest().authenticated()
        .and()
            .authorizeRequests()
            .antMatchers("/css/**”)
            .permitAll()
        .and()
            .authorizeRequests()
            .antMatchers("/resources/**")
            .permitAll();

        http
            .formLogin()
            .loginPage("/login")
            .defaultSuccessUrl("/home")
            .permitAll()
        .and()
            .logout()
            .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }


    @Override
    public void configure(final WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/vendors/**", "/local/**");
    }

使用内存身份验证登录后,我可以创建、读取和删除用户。我想要做的是替换内存中的身份验证,并对数据库中的现有用户进行身份验证。我在这里有什么选择?

【问题讨论】:

  • 创建一个自定义的UserDetailService,它可以通过用户名从数据库中加载一个用户,然后在上面的配置中注入你的数据源来代替inMemoryAuthentication()。
  • @Afridi 我对数据源部分有点困惑。我没有使用任何数据源对象,而是通过 application.properties 中定义的 neo4j datauri 连接到数据库。你的意思是我应该创建一个自定义数据访问对象,如下所示?:docs.spring.io/spring-boot/docs/current/reference/html/…
  • 是的,而且由于你使用的是Spring boot和application.properties文件,所以需要定义dataSource bean,只需在配置文件中@Autowire dataSource对象,然后定义一个自定义的UserDetailService(使用使用来自 Neo4j 数据库的用户名检索用户)。欲了解更多信息,请查看:dzone.com/articles/…

标签: spring-boot spring-security neo4j graph-databases spring-data-neo4j-4


【解决方案1】:

您有一些选择,这取决于您希望您的应用程序有多专业和安全。

  1. 您可以简单地将真实的数据库信息放在您的 application.properties 中,而不是在本地加载的本地/内存中,这样每次加载应用程序时,您都会指向该数据库。
  2. 如果您正在讨论在许多环境(开发、生产等)中部署应用程序,您应该在部署时将这些配置外部化并加载为环境变量(您有很多方法可以做到这一点,具体取决于您将托管你的申请)。使用这种情况,您将执行 @Afridi 在 cmets 上所说的:在部署时刻创建一个 DataSource bean 到 load the environment variables
  3. 您可以使用external configuration files,因此当您运行生成的.jar 文件时,您只需传递额外的参数即可为您的环境使用不同的配置

当然,还有许多其他选项可以做同样的事情,但这些都是您拥有的。我希望我能帮助你,即使是在问题之后这么长时间。

【讨论】:

    猜你喜欢
    • 2011-10-17
    • 2020-04-16
    • 2021-03-30
    • 2014-07-03
    • 2012-11-27
    • 2017-08-14
    • 2011-08-12
    • 2016-07-17
    相关资源
    最近更新 更多