在 Spring Security OAuth2 中注入自定义 userDetailsS​​ervice 时出现问题

2023-11-30

我正在使用 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    |                |
+--------------+---------------------+------+-----+---------+----------------+

然后我像这样注入我的自定义 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 对用户进行身份验证(错误)

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


您正在使用auth.jdbcAuthentication().dataSource(this.dataSource).and().userDetailsService(‌​this.userService);// Inject custom我在这里创建了两个身份验证管理器 - 一个是默认的JdbcDaoImpl and dataSource指向this.dataSource另一个按照您的习惯userService。尝试只放auth.userDetailsService(this.userService)(我希望 userService 内部已经有 jdbc 自动连接)。

这里的重点是.and()用于向认证管理器添加不同的认证配置,而不是配置jdbcAuthentication() one.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Spring Security OAuth2 中注入自定义 userDetailsS​​ervice 时出现问题 的相关文章

随机推荐