【问题标题】:Spring Boot: Send JWT in header (OAuth)Spring Boot:在标头中发送 JWT(OAuth)
【发布时间】:2017-10-07 08:20:07
【问题描述】:

我有一个使用 Feign 客户端的 spring boot 项目,并通过 OAuth 和 JSON Web Tokens 处理授权。授权后,您必须通过 GET 参数发送访问令牌。但是,我不想将其作为 GET 参数发送,而是将其发送到标题中。我找不到办法。有人知道吗?

我的配置:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory()
                .withClient(oAuth2ClientName)
                .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
                .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
                .scopes("read", "write", "trust")
                .secret(oAuth2ClientSecret)
                .accessTokenValiditySeconds(oAuth2AccessTokenValidSecs).
                refreshTokenValiditySeconds(oAuth2RefreshTokenValidSecs);
    }

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

    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey(jwtSigningKey);
        return converter;
    }

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        return defaultTokenServices;
    }
}

我已经用谷歌搜索过了,但我发现的东西是自行设计的,看起来很复杂。

【问题讨论】:

    标签: spring spring-boot oauth jwt


    【解决方案1】:
    1. 作为授权服务,客户端必须通过调用/oauth/token的GET/POST api生成token。除了 clientId、clientSecret、用户名和密码之外,您还必须确定一个 grant_type。无论如何,此调用会生成一个访问令牌作为 JWT 令牌。
    2. 客户端获取 jwt 令牌,从中提取访问令牌并将其作为(这就是您要问的问题)授权承载标头发送到资源服务器。类似于Authorization Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbInJlYWQiLCJ3cml0ZSJdLCJleHAiOjE0OTUxMjE0NzYsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iLCJST0xFX1VTRVIiXSwianRpIjoiNGYyNzQxMmMtNzkyOC00MWE5LTliMjQtN2I4ZmNmMTdiOGRhIiwidGVuYW50IjoidDEiLCJjbGllbnRfaWQiOiJjbGllbnQxIn0.Hwo7T8cAEFVm2NvXQUURiV2uiVz0nHz6RtXbOrFzGaK09TnTJJQmY8VKXsOble7prkveWBqLpWJk9J-9PRCntPW2Tsh5bjQJoFkkfHvT0Vc0TFarbFOh7St567rv5w0mYBNCxD28CM6dv_FHiz5wIoeEUeqQFIqojE3qo-aoT0o1ts-mO-Qmz-Dtla4-wGAYVgs84gQQ_n-U0kZzk_F09iHMgZRAIWq1ot2O6EZ8HHzaHA1gTsq5iWOZyxZAkGO0MTRyZir6vf8PoCHMn2Ge1uePl2NS0-UI5E8ozs2EXyGRHY6p-ZQTGvrUIObf_ZBQGgd37EoDBkrPK65kVqpZfw
    3. 资源服务器必须验证JWT访问令牌,并且资源服务器的配置取决于资源服务器是否与授权服务器绑定,因此它们都存在于同一个spring上下文中。

    【讨论】:

      【解决方案2】:

      如果您想使用最新的 Spring Boot 库,请参阅我关于如何让 client_credentials 在 https://stackoverflow.com/a/65741386/698471 上工作的回复

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-14
        • 2017-05-26
        • 2021-06-20
        • 2015-04-25
        • 2020-02-01
        • 2021-10-21
        • 2018-03-16
        • 2014-09-02
        相关资源
        最近更新 更多