java - 在 Spring Security OAuth2 中注入(inject)自定义 userDetailsS​​ervice 的问题

标签 java spring spring-security spring-security-oauth2

我正在使用 Spring Security OAuth2 2.0.7.RELEASE。因为我使用 ORM 连接到我的数据库并且默认 JdbcUserDetailsManager 使用 jdbc 我想实现我自己的 UserDetailsS​​ervice,这是

@Service
public class UserService
    implements UserDetailsService {

    @Override
    public UserDetailsService loadUserByUsername(String username) throws UsernameNotFoundException {
        // I tested this logic and works fine so i avoid this lines
        return userDetailsService;
    }
}

此外,我修改了权限架构如下:

mysql> describe authorities;
+--------------+---------------------+------+-----+---------+----------------+
| Field        | Type                | Null | Key | Default | Extra          |
+--------------+---------------------+------+-----+---------+----------------+
| authority_id | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| user_id      | bigint(20) unsigned | NO   | MUL | NULL    |                |
| authority    | varchar(256)        | NO   |     | NULL    |                |
+--------------+---------------------+------+-----+---------+----------------+

然后我像这样注入(inject)我的自定义 userDetailsS​​ervice:

@Configuration
@Import(OAuth2SupportConfig.class)
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends
        AuthorizationServerConfigurerAdapter {

  ...    

  @Autowired
  private UserDetailsService userDetailsService

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer    endpoints)
            throws Exception {
        endpoints.authenticationManager(authenticationManager)
                .tokenStore(tokenStore).tokenServices(tokenService);
        endpoints.userDetailsService(userDetailsService); // Inject custom
        endpoints.authorizationCodeServices(authorizationCodeServices);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients)
            throws Exception {
        clients.jdbc(dataSource);
    }
}

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class AuthenticationManagerConfiguration
    extends GlobalAuthenticationConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserDetailsService userService;

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
 auth.jdbcAuthentication().dataSource(this.dataSource).and().userDetailsService(this.userService);// Inject custom
    }
}

如果我使用 grant_type=password 发送/oauth/token 请求,那么我会得到这个错误

POST /oauth/token HTTP/1.1
Host: localhost:8080
Authorization: Basic aW5kaXJhOnNlY3JldA==
Cache-Control: no-cache
Postman-Token: c89baf37-8ad2-4270-5251-9715bfab470a
Content-Type: application/x-www-form-urlencoded

grant_type=password&username=user&password=pass

(其中对clientId和clientSecret进行了编码)

{
  "error": "unauthorized",
  "error_description": "PreparedStatementCallback; bad SQL grammar [select username,authority from authorities where username = ?]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'username' in 'field list'"
}

显然仍在使用默认的 JdbcDaoImpl。事实上,当我开始调试时,我发现是按照以下步骤进行的:

  1. 验证客户端(好的,因为我没有修改 oauth_client_details 表)
  2. 使用我的自定义 userDetailsS​​ervice 对用户进行身份验证(好的,用户表已修改,但我的自定义 userDetailsS​​ervice 支持更改)
  3. 使用默认的 userDetailsS​​ervice(ERROR) 验证用户

我不知道为什么会这样。对我来说这听起来像是一个错误。 有没有发现什么问题?

最佳答案

您正在使用 auth.jdbcAuthentication().dataSource(this.dataSource).and().userDetailsS​​ervice(‌ this.userService);//注入(inject)自定义 我在这里创建了两个身份验证managers - 一个具有默认的 JdbcDaoImpldataSource 指向 this.dataSource 另一个具有您的自定义 userService。尝试只放置 auth.userDetailsS​​ervice(this.userService)(我希望 userService 已经在内部 Autowiring 了 jdbc)。

这里的重点是.and()是用来给认证管理器添加不同的认证配置的,不是配置jdbcAuthentication()的。

关于java - 在 Spring Security OAuth2 中注入(inject)自定义 userDetailsS​​ervice 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31683166/

相关文章:

java - Javabeans和JSP 2.0

java - 将 @index 添加到字段后,对象化过滤器不起作用

java - 在 maven 中看不到 spring-boot-starter-data-mongodb-reactive 和 spring-boot-starter-webflux 依赖项

spring - Weblogic,Spring 找不到 bean

java - Spring Condition 无法从属性文件中读取值

java - 重命名 Spring csrf token 变量

没有管理器的 Grails Spring Security Ldap

java - 为什么在 Windows 上调用 MinGW 编译的函数(不加载库)时出现 UnsatisfiedLinkError?

java - 写入 ZIP 文件的文件仅在运行时可用 [Java]

java - spring security上的用户名随机未设置