java - Spring OAuth2 : How do I allow password grant type using java configuration?

标签 java spring spring-security spring-oauth2

在我的 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/")
}

我使用以下代码来检索访问 token :

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();

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

最佳答案

您应该使用变量参数,而不是像您那样使用逗号分隔的字符串值:

.authorizedGrantTypes("authorization_code, refresh_token, password")

替换为:

.authorizedGrantTypes("authorization_code", "refresh_token", "password")

关于java - Spring OAuth2 : How do I allow password grant type using java configuration?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35829801/

相关文章:

java - 正确缩放图像的位置Java

java - 如何从 Java Functions Worker 获取 Java 堆?

java - 具有 Spring Security 的 OTP

Java Web 应用程序属性

java - Spring 安全中每个请求的不同 csrf token

java - Keycloak和Spring Boot : session creation policy

java - 不变性和图形模型——如何创建它们?

java - Play 框架 - 保存模型时出现 Mysql 错误

java - 测试类中的接口(interface) getBean

java - Spring Security 不使用自定义 UserDetailsS​​ervice