spring security oauth2 client Oauth2Token覆盖

时间:2017-12-03 12:27:32

标签: spring spring-boot spring-security-oauth2

我正在尝试使用spring-security-oauth2实现Oauth2.0客户端。 但是,在获取访问令牌时,某些Oauth2提供商的响应与oauth2的响应不匹配。

通常,oauth 2.0响应如下所示。

{
  "scope": "user",
  "token_type": "Bearer",
  "refresh_token": "RUuOfymhZjiysujVA0JqeBLdyV7yGFuYKoHmRA7FixPuEGVsTr",
  "access_token": "wjFQizYxTekhtg9FUK6gci1wgkdKNxYaBJjnOTyQWO51F861yd",
  "expires_in": 604800
}

但是,我应该做以下反应。

{
"status": 200,
"msg": "",
"data": {
  "scope": "user",
  "token_type": "Bearer",
  "refresh_token": "RUuOfymhZjiysujVA0JqeBLdyV7yGFuYKoHmRA7FixPuEGVsTr",
  "access_token": "wjFQizYxTekhtg9FUK6gci1wgkdKNxYaBJjnOTyQWO51F861yd",
  "expires_in": 604800
  }
}

在spring-security-oauth2中,OAuth2AccessToken按大写反序列化。 所以我也希望在下面的案例中反序列化。 如果有任何解决方案,请帮助我。

我附上了我的配置代码。

@Configuration
@EnableOAuth2Client
public class OAuth2ClientConfig extends WebSecurityConfigurerAdapter {
    private final static Logger LOGGER = LoggerFactory.getLogger(OAuth2ClientConfig.class);
    @Autowired
    OAuth2ClientContext oauth2ClientContext;

//  @Autowired
//  AccountService accountService;

    @Bean
    @ConfigurationProperties("someProvider.client")
    AuthorizationCodeResourceDetails someProvider() {
        return new AuthorizationCodeResourceDetails();
    }

    @Bean
    @ConfigurationProperties("someProvider.resource")
    ResourceServerProperties someProviderResource() {
        return new ResourceServerProperties();
    }

    @Bean
    FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.setOrder(-100);
        return registration;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/edit/**").addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
    }

    private Filter ssoFilter() {
        CompositeFilter filter = new CompositeFilter();
        List<Filter> filters = new ArrayList<>();

        OAuth2ClientAuthenticationProcessingFilter someProvider
            = new OAuth2ClientAuthenticationProcessingFilter("/edit/**");

        OAuth2RestTemplate SomeProviderOauth2RestTemplate = new OAuth2RestTemplate(someProvider(), oauth2ClientContext);
        AuthorizationCodeAccessTokenProvider authorizationCodeAccessTokenProvider = new AuthorizationCodeAccessTokenProvider();
        authorizationCodeAccessTokenProvider.setStateMandatory(false);
        SomeProviderOauth2RestTemplate.setAccessTokenProvider(authorizationCodeAccessTokenProvider);

        SomeProvider.setRestTemplate(SomeProviderOauth2RestTemplate);
        SomeProvider.setTokenServices(new UserTokenService(someProviderResource().getUserInfoUri(), someProvider().getClientId()));
        SomeProvider.setAuthenticationSuccessHandler(new SomeProviderOAuth2SuccessHandler("SomeProvider"));
        filters.add(someProvider);
        filter.setFilters(filters);

        return filter;
    }
}

0 个答案:

没有答案