1

在我的 java 服务器应用程序中,尝试使用密码授予流程进行身份验证时出现以下错误:

TokenEndpoint - Handling error: InvalidClientException, Unauthorized grant type: password

我确实允许有问题的用户明确授予:

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
            .withClient("officialclient")
                .authorizedGrantTypes("authorization_code, refresh_token, password")
                .authorities("ROLE_CLIENT")
                .scopes("read", "write")
                .resourceIds(RESOURCE_ID)
                .secret("officialclientsecret")
                .redirectUris("https://www.someurl.com/")
}

我正在使用以下代码来检索访问令牌:

ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();
resourceDetails.setClientAuthenticationScheme(AuthenticationScheme.header);
resourceDetails.setAccessTokenUri("http://localhost:8080/organizer/oauth/token");
resourceDetails.setScope(Arrays.asList("read", "write"));
resourceDetails.setId("resource");
resourceDetails.setClientId("officialclient");
resourceDetails.setClientSecret("officialclientsecret");
resourceDetails.setUsername("Paul");
resourceDetails.setPassword("password");

OAuth2RestTemplate template = new OAuth2RestTemplate(resourceDetails, context);
return template.getAccessToken().getValue();

是否有允许密码授予类型的全局设置?

4

2 回答 2

2

您应该使用Variable Arguments,而不是逗号分隔的字符串值,就像您所做的那样:

.authorizedGrantTypes("authorization_code, refresh_token, password")

将其替换为:

.authorizedGrantTypes("authorization_code", "refresh_token", "password")
于 2016-03-06T17:14:03.823 回答
0

您需要AuthenticationManagerAuthorizationServerEndpointsConfigurer. 授予类型下的更多信息。例子:

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.authenticationManager(authenticationManager);
}

如果你想使用 spring boot 提供的默认管理器进行开发,你可以像这样抓取 bean:

@Component
@EnableAuthorizationServer
public class MyAuthorizationServerConfigurer extends AuthorizationServerConfigurerAdapter {

private final AuthenticationManager authenticationManager;

public MyAuthorizationServerConfigurer(AuthenticationConfiguration authenticationConfiguration) throws Exception {
    this.authenticationManager = authenticationConfiguration.getAuthenticationManager();
}   

}

于 2018-06-07T21:34:20.973 回答