java - Spring OAuth2 不提供刷新 token

标签 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"}

由于某种原因,没有刷新 token 。我根据spec知道,刷新 token 是可选的;有什么方法可以启用我错过的功能吗?

作为引用,这是我的提供商代码:

@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");
        }
    }
}

最佳答案

客户端需要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");
            }

关于java - Spring OAuth2 不提供刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30851098/

相关文章:

grails - 拦截器可以在认证和 Action 执行之间执行吗?

java - 升级到 Spring 3 后启动缓慢

java - 在查询注释中更新时间戳字段 Spring JPA

java - 使用 Spring Security 和 Redis 进行基于 Cookie 的身份验证,用于具有 Java 配置的 RESTFul api

java - 如何在 Spring 中 Autowiring 业务对象

spring - META-INF/资源在 Spring Boot 中无法与 @EnableWebMVC 正常工作

java - Spring Security ACL 层次结构

带有星号的 Java 7u51/7u55 list 变量

java - 为什么这段代码在java中会耗尽内存,而在c中却不会?

java - “Echo”linux 命令无法通过 Java 代码运行