Spring OAuth2没有提供刷新令牌

时间:2015-06-15 17:14:46

标签: java spring spring-security spring-security-oauth2

我正在使用Spring和"密码"运行OAuth提供程序。授权类型。

运行此(提供程序在端口8080上):

curl -u "app:appclientsecret" "http://localhost:8080/oauth/token" --data "grant_type=password&username=marissa&password=koala"

返回:

{"access_token":"56da4d2b-7e66-483e-b88d-c1a58ee5a453","token_type":"bearer","expires_in":43199,"scope":"read"}

由于某种原因,没有刷新令牌。我知道根据spec,刷新令牌是可选的;有没有办法让我错过了?

供参考,这是我的提供商代码:

@SpringBootApplication
public class Provider {
    public static void main(String... args) {
        System.setProperty("server.port", "8080");

        SpringApplication.run(Provider.class, args);
    }

    @Configuration
    @EnableWebSecurity
    static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
        private final UserStoreType type = UserStoreType.IN_MEMORY;

        enum UserStoreType {
            IN_MEMORY,
        }

        @Autowired
        public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
            switch(type) {
                case IN_MEMORY:
                    System.err.println("Setting up user creds..");

                    auth.inMemoryAuthentication()
                            .withUser("marissa").password("koala").roles("USER")
                            .and()
                            .withUser("admin").password("topsecret").roles("USER", "ADMIN");

                    break;
            }
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {}
    }

    @Configuration
    @EnableAuthorizationServer
    static class OAuthConfig extends AuthorizationServerConfigurerAdapter {
        @Autowired
        private AuthenticationManager authenticationManager;

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

        @Override
        public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
            oauthServer.checkTokenAccess("permitAll()");
        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.inMemory()
                    .withClient("resource-serv")
                    .scopes("read")
                    .resourceIds("my-resource")
                    .secret("secret123")
                    .and()
                    .withClient("app")
                    .authorizedGrantTypes("client_credentials", "password")
                    .scopes("read")
                    .resourceIds("my-resource")
                    .secret("appclientsecret");
        }
    }
}

1 个答案:

答案 0 :(得分:8)

客户端需要authorizedGrantType“refresh_token”。

试试这个

  @Override
            public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
                clients.inMemory()
                        .withClient("resource-serv")
                        .scopes("read")
                        .resourceIds("my-resource")
                        .secret("secret123")
                        .and()
                        .withClient("app")
                        .authorizedGrantTypes("client_credentials", "password", "refresh_token")
                        .scopes("read")
                        .resourceIds("my-resource")
                        .secret("appclientsecret");
            }