2016-02-03 107 views
0

我有这样的情况。Spring Boot MongoDB连接bean

我使用Spring 1.3.2开机,我已经在我的电脑安装 MongoDB的我已经加入依赖

org.springframework.boot:spring-boot-starter-data-mongodb 

当我运行我的Web应用程序,数据库连接会自动开始工作。

我没有配置一个东西。

现在我想连接的Spring Security这样的:

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

我的问题是春季启动数据源是什么默认的bean的名字,我可以重写呢?

回答

2

如果你打算使用Mongodb为您的用户资料存储,即用户名密码,等等,那么你不能使用jdbcAuthentication()。相反,你可以为了使用UserDetailsService达到相同的:

@Configuration 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired private MongoTemplate template; 

    @Override 
    @Autowired 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
       .userDetailsService((String username) -> { 
        User user = template.findOne(Query.query(Criteria.where("username").is(username)), User.class, "users"); 

        if (user == null) throw new UsernameNotFoundException("Invalid User"); 

        return new UserDetails(...); 
       }); 
    } 
} 

在prceeding样品,我假设你有一个users集合与username场。如果对于给定的用户名确实存在一个用户,则应该返回对应于该用户的UserDetails的实现。否则,你应该抛出一个UsernameNotFoundException

您还可以处理用户身份验证的其他选项,但jdbcAuthentication()是假表,因为你使用的是NoSQL数据存储用于存储用户的细节和JDBC是处理所有与talkings关系数据库的抽象。