我使用Spring OAuth2保护我的REST API并实现了client_credentials,密码授予类型 . 除了ResourceOwnerPassword流与POST主体中的用户凭据之外,它们的工作完全正常 .
使用以下URL的令牌 endpoints 调用是正常的(如果我们将用户凭据作为URL参数发送)HTTP POST:/ oauth / token?grant_type = password&username = 123&password = 456
但是通过以下设置,我的Authentication.getCredentials和Authentication.getPrincipal对象始终为空 . 我正在使用自定义身份验证提供程序,因为我需要针对外部LDAP系统验证用户 .
这是否意味着用户凭据(Usernamd和密码)只能在URL参数中发送到令牌 endpoints (/ oauth / token)?
我的配置是:
授权服务器配置:
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
tokenEnhancerChain.setTokenEnhancers(
Arrays.asList(new CustomTokenEnhancer(), accessTokenConverter));
endpoints
.tokenStore(tokenStore).tokenEnhancer(tokenEnhancerChain)
.userApprovalHandler(userApprovalHandler)
.authenticationManager(userAuthenticationManager);
}
@Bean
AuthenticationManager userAuthenticationManager() {
List<AuthenticationProvider> authenticationProviders = new ArrayList<AuthenticationProvider>();
authenticationProviders.add(new CustomAuthenticationProvider()));
return new ProviderManager(authenticationProviders);
}
定制认证提供商:
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
System.out.println("authentication object........"+authentication);
String userName;
String password;
System.out.println("authentication object credentials........"+authentication.getCredentials());
userName = authentication.getName();
password = authentication.getCredentials().toString();
}
控制台中打印的身份验证对象:
org.springframework.security.authentication.Usernam ePasswordAuthenticationToken @ 7a2162f9:Principal:null;证书:[保护];认证:false;详情:;没有授予任何权力
但是,我确实找到了一个帖子,他们说请求体中的用户凭据与Token endpoints 一起使用..这是关于它的链接..
https://github.com/spring-projects/spring-security-oauth/issues/260
按照上面的说法,我似乎错过了一些东西......
任何帮助/提示将不胜感激..