【问题标题】:InvalidGrantException, Invalid authorization code when running two (or more) Spring OAuth ServerInvalidGrantException,运行两个(或多个)Spring OAuth Server 时授权码无效
【发布时间】:2016-06-17 19:34:47
【问题描述】:

有两个服务。我正在使用 Netflix 堆栈[Eureka/zuul]。

  1. 聚合器服务
  2. 用户服务 [Spring OAUTH]

当我运行用户服务的一个实例时,一切正常,但是当我在另一台服务器上运行另一个实例时,我收到下面提到的错误并且请求[登录 oauth] 失败。

我想扩展使用 spring oauth 的 USER-SERVICE。

处理错误:InvalidGrantException,无效授权码: Q1j7Hs 06:24:17.253 [http-nio-8081-exec-2] 信息 o.s.s.o.p.e.TokenEndpoint - 处理错误:InvalidGrantException, 无效的授权码:w9uvl1

任何线索或建议将不胜感激。

【问题讨论】:

    标签: java spring spring-security oauth


    【解决方案1】:

    如果您有两个实例,则必须小心,一个客户端始终与同一个实例通信。否则,客户端可能会向实例 A 发出第一个请求,然后再向实例 B 发出请求。

    通常在这种情况下,客户端会连接到负载均衡器,该负载均衡器会转发到您的服务实例。

    比你必须的选择:

    1. 将负载平衡器配置为始终将客户端连接到同一个实例,这是通过粘性会话完成的。
    2. 让实例共享他们的会话,例如在 reddis 商店中 看这里spring session

    【讨论】:

    • 谢谢。我们使用了 redis,因为我们无法使用粘性会话。
    【解决方案2】:

    配置AuthorizationServerConfiguration使用JdbcAuthorizationCodeServices服务在所有认证节点之间通过数据库存储和共享授权码。

    @Configuration
    @EnableAuthorizationServer
    protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter implements EnvironmentAware {
    
        @Inject
        private DataSource dataSource;
    
        @Bean
        public TokenStore tokenStore() {
            return new JdbcTokenStore(dataSource);
        }
    
        // JdbcAuthorizationCodeServices stores authentication codes in a database.
        @Bean
        public JdbcAuthorizationCodeServices jdbcAuthorizationCodeServices() {
            return new JdbcAuthorizationCodeServices(dataSource);
        }
    
        @Inject
        @Qualifier("authenticationManagerBean")
        private AuthenticationManager authenticationManager;
    
        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints
                .tokenStore(tokenStore())
                .reuseRefreshTokens(false)
                .authenticationManager(authenticationManager)
                .authorizationCodeServices(jdbcAuthorizationCodeServices());
        }
    }
    

    并创建oauth_code 表:

    CREATE TABLE oauth_code (
        code VARCHAR(256), authentication LONGVARBINARY
    );
    

    【讨论】:

    • 你好 Michal, 这个解决方案没有解决我的问题。你能帮帮我吗?
    猜你喜欢
    • 2012-07-24
    • 1970-01-01
    • 2016-09-11
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    相关资源
    最近更新 更多