Spring Oauth2 授权服务器
Posted
技术标签:
【中文标题】Spring Oauth2 授权服务器【英文标题】:Spring Oauth2 Authorization Server 【发布时间】:2015-03-31 00:40:55 【问题描述】:我在下面设置 Spring 配置:
@EnableAuthorizationServer
@EnableWebSecurity
@Configuration
public class Oauth2Provider extends WebSecurityConfigurerAdapter implements
AuthorizationServerConfigurer
/*
* @Autowired private TokenStore tokenStore;
*/
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception
auth.inMemoryAuthentication().withUser("user").password("password")
.roles("USER").and().withUser("admin").password("password")
.roles("USER", "ADMIN");
@Override
public void configure(AuthorizationServerSecurityConfigurer security)
throws Exception
// TODO Auto-generated method stub
security.allowFormAuthenticationForClients();
@Override
public void configure(ClientDetailsServiceConfigurer clients)
throws Exception
// TODO Auto-generated method stub
clients.inMemory()
.withClient("my-trusted-client")
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT", "ROLE_ANONYMOUS")
.scopes("read", "write", "trust")
.secret("secret")
.accessTokenValiditySeconds(60);
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception
// TODO Auto-generated method stub
Maven 设置如下:
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
我访问: http://localhost:8080/oauth/token 有效载荷 grant_type=password&password=password&username=user&scope=read&client_id=my-trusted-client&client_secret=secret
但我收到以下错误:
error: "unsupported_grant_type"
error_description: "Unsupported grant type: password"
【问题讨论】:
【参考方案1】:要使用密码授予,您需要向授权服务器提供身份验证管理器(在您的示例中使用 TODO 的空方法),以便它可以对用户进行身份验证。如果它是 Spring Boot 应用程序,则始终有一个 AuthenticationManager
可用作 @Autowired
。
【讨论】:
并确保在客户端的 authorizedGrantTypes 中允许“密码” “如果它是一个 Spring Boot 应用程序,总是有一个 AuthenticationManager 可用于@Autowired”,不再是,如果你声明了一个 UserDetailsService,对吧?AuthenticationConfiguration
对象被注入,AuthenticationManager
对象被提供#getAuthenticationManager
方法。以上是关于Spring Oauth2 授权服务器的主要内容,如果未能解决你的问题,请参考以下文章