![](/img/trans.png)
[英]Spring security OAuth2 Refresh Token - IllegalStateException, UserDetailsService is required
[英]problems injecting custom userDetailsService in Spring Security OAuth2
我正在使用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。 实际上,当我开始调试时,发现执行以下步骤:
我不知道为什么会这样。 对我来说,这听起来像个虫子。 你有没有发现什么问题?
您正在使用auth.jdbcAuthentication().dataSource(this.dataSource).and().userDetailsService(this.userService);// Inject custom
,我在这里创建了两个auth管理器-一个具有默认的JdbcDaoImpl
和dataSource
指向this.dataSource
和另一个带有自定义userService
。 尝试仅将auth.userDetailsService(this.userService)
(我希望userService已将jdbc自动连接到其中)。
这里的重点是.and()
用于向身份验证管理器添加不同的身份验证配置,而不是配置jdbcAuthentication()
。
在2.0.7
当您以授予类型作为password
对/oauth/token
进行POST/GET
请求时,它实际上将是ClientDetailsUserDetailsService
而不是UserDetailsService
。
我有类似的问题,这就是我解决的方法:
public class AppClientDetailsUserDetailsService extends ClientDetailsUserDetailsService {
public AppClientDetailsUserDetailsService(ClientDetailsService clientDetailsService) {
super(clientDetailsService);
}
}
public class AppConsumerDetailsService implements ClientDetailsService {
public ClientDetails loadClientByClientId(String clientId)
throws OAuth2Exception {
//some logic
}
}
<http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="authenticationManager"
entry-point-ref="entryPoint" xmlns="http://www.springframework.org/schema/security"
>
<intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
<anonymous enabled="false" />
<http-basic entry-point-ref="entryPoint" />
<custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" />
</http>
<bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
<property name="authenticationManager" ref="authenticationManager" />
</bean>
authenticationManager
是AppClientDetailsUserDetailsService
的bean,其构造函数参数为AppConsumerDetailsService
。
问题是您正在使用默认的JdbcDaoImpl
。 引起问题的查询是
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY =
"select username,authority " +
"from authorities " +
"where username = ?";
这是默认查询,它通过JdbcDaoImpl
的userName加载用户的所有权限。 因此,如果您仍然想使用默认的JdbcDaoImpl
您可以设置自定义查询,该查询将对您的表执行此操作,并将其作为参数。
我不确定users表的模式是什么,但是类似的东西应该是相似的(如果您以编程方式配置JdbcDaoImpl bean):
String query = "select username,authority " +
"from authorities join users on users.id = authorities.user_id " +
"where users.username = ?";
jdbcDaoImpl.setAuthoritiesByUsernameQuery(query);
或者,如果您从XML创建JdbcDaoImpl:
<bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
<property name="authoritiesByUsernameQuery"
value="select username,authority from authorities
join users on users.id = authorities.user_id
where users.username = ?"/>
</bean>
您可能需要更改其他一些默认查询以适合您的模式,请在JdbcDaoImpl
中对其进行查看。
如果与默认的JdbcDaoImpl
相距太远,您可能还考虑编写自己的UserDetailsService
实现的可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.