我正在使用Spring Security OAuth2 2.0.7.RELEASE。当我使用ORM连接到数据库并且默认JdbcUserDetailsManager使用jdbc时,我想实现自己的UserDetailsService,即
@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 | |
+--------------+---------------------+------+-----+---------+----------------+
然后,我像这样注入我的自定义userDetailsService:
@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。实际上,当我开始调试时,发现执行以下步骤:
对客户端进行身份验证(好的,因为我还没有修改oauth_client_details表)
使用我的自定义userDetailsService对用户进行身份验证(确定,已修改users表,但我的自定义userDetailsService支持更改)
使用默认的userDetailsService(ERROR)对用户进行身份验证
我不知道为什么会这样。对我来说,这听起来像个虫子。
你有没有发现什么问题?
最佳答案
您正在使用auth.jdbcAuthentication().dataSource(this.dataSource).and().userDetailsService(this.userService);// Inject custom
,我在这里创建了两个身份验证管理器-一个默认的JdbcDaoImpl
和dataSource
指向this.dataSource
,另一个使用您的自定义userService
。尝试仅将auth.userDetailsService(this.userService)
放进去(我希望userService已经在内部自动连接了jdbc)。
这里的重点是.and()
用于向身份验证管理器添加不同的身份验证配置,而不是配置jdbcAuthentication()
。
关于java - Spring Security OAuth2中注入(inject)自定义userDetailsService的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31683166/