将Spring Security与HSQLDB集成

时间:2015-12-06 19:00:11

标签: spring-mvc spring-security spring-boot hsqldb

我努力连接我的HSQLDB和Spring Security配置。我使用的是Spring Data JPA,而且我已经放了一些数据。 如何配置此方法

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

}

连接我的hsqldb并检索用户名,密码和角色? 这是github

我想通了,必须添加的代码是:

 @Bean
public DataSource dataSource(){
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL)
            .build();
    return db;
}

和连接:

 @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, DataSource dataSource) throws Exception {      auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("select u.username, u.password, 1 from User u where u.username = ?")
            .authoritiesByUsernameQuery("SELECT u.username, role.name FROM User u JOIN user_roles ON u.id = user_roles.users_Id JOIN Role on user_roles.roles_Id = role.id WHERE u.username = ? ").passwordEncoder(new BCryptPasswordEncoder());

}

1 个答案:

答案 0 :(得分:0)

@Autowire
private UserDetailsService userDetailsService;

实现UserDetailsS​​ervice类并覆盖loadUserByUsername()方法,您将使用存储库方法从数据库加载用户身份验证详细信息(因为您使用的是spring数据jpa)

@Autowire
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .userDetailsService(userDetailsService);
    }