Java Spring Boot 2和OAuth2客户端凭据不支持的授权类型
我想使用我的组件中包含的Spring Security的资源服务器和授权服务器来保护我的应用程序。 所需的流程包括仅使用客户端凭据授权类型,并将客户端id和客户端机密一起作为base64头传递,在点击Java Spring Boot 2和OAuth2客户端凭据不支持的授权类型,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,我想使用我的组件中包含的Spring Security的资源服务器和授权服务器来保护我的应用程序。 所需的流程包括仅使用客户端凭据授权类型,并将客户端id和客户端机密一起作为base64头传递,在点击oauth/tokenendpoint之后,应该为进一步的请求返回令牌。我还在POSTrequest参数中包括grant_type:client凭证 目前,我收到错误消息: “访问此资源需要完全身份验证”。 奇怪的是,尽管我进行了配置,Spring仍然会生成控制台日志中可以看到的随机安全密码 这是我
oauth/token
endpoint之后,应该为进一步的请求返回令牌。我还在POST
request参数中包括grant_type:client凭证
目前,我收到错误消息:
“访问此资源需要完全身份验证”
。
奇怪的是,尽管我进行了配置,Spring仍然会生成控制台日志中可以看到的随机安全密码
这是我第一次使用Spring Security,所以我可能错过了什么
以下是我的配置:
授权服务器配置:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends
AuthorizationServerConfigurerAdapter {
@Override
public void configure(final AuthorizationServerSecurityConfigurer security) {
security
.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("some-client")
.authorizedGrantTypes("client-credentials")
.authorities("ROLE_CLIENT")
.scopes("read", "write", "trust")
.accessTokenValiditySeconds(3600)
.secret("somePass")
.refreshTokenValiditySeconds(24*3600);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.tokenStore(tokenStore())
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
}
ResourceServerConfig:
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(final HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/oauth/token", "/oauth/authorize", "/oauth/confirm_access").permitAll()
.antMatchers("/**").authenticated()
.and().httpBasic().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
}
}
我使用的是Spring Boot 2.0.1.RELEASE和Spring Security OAuth2.0.14.RELEASE。
在我的例子中,如果使用InMemoryTokenStore,它将与一个实例一起工作,如果要创建多个应用程序实例,最好的替代方法是什么?当您将用户存储在内存中时,您以纯文本形式提供密码,然后当您试图从
DelegatingPasswordEncoder
检索编码器以验证密码时,它找不到密码
您可以尝试以下更改,以便通过添加{noop}
@Override
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("some-client")
.authorizedGrantTypes("client-credentials")
.authorities("ROLE_CLIENT")
.scopes("read", "write", "trust")
.accessTokenValiditySeconds(3600)
.secret("{noop}somePass") //change here
.refreshTokenValiditySeconds(24*3600);
}
也可以在WebSecurity配置文件中更改用户的密码。您的项目中是否有corsfilter?不,我的项目中没有corsfilter