【发布时间】: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