首页 > 解决方案 > 如何使用 Spring rest api 和 oauth 安全性从数据库中对用户进行身份验证

问题描述

我想使用 oauth2 实现 Rest api 安全性,并且我已经按照 this 使用静态用户数据实现了这种安全。现在我想更改这种情况以使用 jdbc 对数据库中的数据进行身份验证,但直到现在我无法找到任何有关 jdbc 身份验证的教程。请向我推荐一些我的要求示例,我的要求是 Spring mvc Rest api+OAuth2+jdbc database+java-config 示例。

目前我已经用下面的静态用户进行了测试。

public void globalUserDetails(AuthenticationManagerBuilder auth) throws 
 Exception {
    auth.inMemoryAuthentication()
    .withUser("crmadmin").password("crmpass").roles("ADMIN","USER").and()
    .withUser("crmuser").password("pass123").roles("USER");
}

标签: oauth-2.0spring-jdbcspring-restcontroller

解决方案


我认为您可以将其配置如下:将下面的代码放在扩展 WebSecurityConfigurerAdapter 的类中

@Autowired
private DataSource securityDataSource; // from Bean where you have connection, jdbc driver set up for database containing login information

@覆盖

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

推荐阅读