Spring Security OAuth2中注入自定义userDetailsS​​ervice的问题

问题描述

您正在使用auth.jdbcAuthentication().dataSource(this.dataSource).and().userDetailsService(‌​this.userService);// Inject custom,我在这里正在创建两个auth管理器- 一个具有默认值JdbcDaoImpldataSource指向auth管理器this.dataSource,另一个具有您的customuserService。尝试仅放置auth.userDetailsService(this.userService)(我希望userService已经在内部自动连接了jdbc)。

这里的要点.and()是用于向身份验证管理器添加不同的身份验证配置,而不是配置jdbcAuthentication()一个。

解决方法

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

  1. @Service
  2. public class UserService
  3. implements UserDetailsService {
  4. @Override
  5. public UserDetailsService loadUserByUsername(String username) throws UsernameNotFoundException {
  6. // I tested this logic and works fine so i avoid this lines
  7. return userDetailsService;
  8. }
  9. }

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

  1. mysql> describe authorities;
  2. +--------------+---------------------+------+-----+---------+----------------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +--------------+---------------------+------+-----+---------+----------------+
  5. | authority_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
  6. | user_id | bigint(20) unsigned | NO | MUL | NULL | |
  7. | authority | varchar(256) | NO | | NULL | |
  8. +--------------+---------------------+------+-----+---------+----------------+

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

  1. @Configuration
  2. @Import(OAuth2SupportConfig.class)
  3. @EnableAuthorizationServer
  4. public class OAuth2AuthorizationServerConfig extends
  5. AuthorizationServerConfigurerAdapter {
  6. ...
  7. @Autowired
  8. private UserDetailsService userDetailsService
  9. @Override
  10. public void configure(AuthorizationServerEndpointsConfigurer endpoints)
  11. throws Exception {
  12. endpoints.authenticationManager(authenticationManager)
  13. .tokenStore(tokenStore).tokenServices(tokenService);
  14. endpoints.userDetailsService(userDetailsService); // Inject custom
  15. endpoints.authorizationCodeServices(authorizationCodeServices);
  16. }
  17. @Override
  18. public void configure(ClientDetailsServiceConfigurer clients)
  19. throws Exception {
  20. clients.jdbc(dataSource);
  21. }
  22. }
  23. @Configuration
  24. @Order(Ordered.HIGHEST_PRECEDENCE)
  25. public class AuthenticationManagerConfiguration
  26. extends GlobalAuthenticationConfigurerAdapter {
  27. @Autowired
  28. private DataSource dataSource;
  29. @Autowired
  30. private UserDetailsService userService;
  31. @Override
  32. public void init(AuthenticationManagerBuilder auth) throws Exception {
  33. auth.jdbcAuthentication().dataSource(this.dataSource).and().userDetailsService(this.userService);// Inject custom
  34. }
  35. }

如果我使用Grant_type = password发送/ oauth / token请求,则会出现此错误

  1. POST /oauth/token HTTP/1.1
  2. Host: localhost:8080
  3. Authorization: Basic aW5kaXJhOnNlY3JldA==
  4. Cache-Control: no-cache
  5. Postman-Token: c89baf37-8ad2-4270-5251-9715bfab470a
  6. Content-Type: application/x-www-form-urlencoded
  7. grant_type=password&username=user&password=pass

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

  1. {
  2. "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'"
  3. }

显然仍在使用默认的JdbcDaoImpl。实际上,当我开始调试时,我发现遵循以下步骤:

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

我不知道为什么会这样。对我来说,这听起来像个虫子。你有没有发现什么问题?

猜你在找的技术问答相关文章

如何检查配对的蓝牙设备是打印机还是扫描仪(Android)
是否允许实体正文进行HTTP DELETE请求?
如何将ZipInputStream转换为InputStream?
java.util.logging Java 8中的变量
PowerMockito.doReturn返回null
Java中的RESTful调用
Swing / Java:如何正确使用getText和setText字符串
特殊字符和重音字符
Android Studio中的ndk.dir错误
错误“找不到主类”