下面列出了org.springframework.boot.autoconfigure.security.oauth2.resource.PrincipalExtractor#org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private OAuth2ClientAuthenticationProcessingFilter ssoFilter(ClientResources client, String path) {
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(client.getResource().getUserInfoUri(),
client.getClient().getClientId());
tokenServices.setRestTemplate(oAuth2RestTemplate);
OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter =
new OAuth2ClientAuthenticationProcessingFilter(path);
oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);
oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
oAuth2ClientAuthenticationFilter.setAuthenticationSuccessHandler(new SimpleUrlAuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
// TODO switch to tokens or find a way to return to last page on client
this.setDefaultTargetUrl("/");
super.onAuthenticationSuccess(request, response, authentication);
}
});
return oAuth2ClientAuthenticationFilter;
}
public static Filter wechat(AuthorizationCodeResourceDetails client, ResourceServerProperties resourceServerProperties, String path, OAuth2ClientContext oauth2ClientContext) {
OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = new OAuth2ClientAuthenticationProcessingFilter(path);
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client, oauth2ClientContext);
AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider();
accessTokenProvider.setAuthorizationRequestEnhancer((request, resource, form, headers) -> {
form.set("appid", resource.getClientId());
form.set("secret", resource.getClientSecret());
form.set("scope", "snsapi_userinfo");
form.set("response_type", "code");
form.set("#wechat_redirect", "");
});
accessTokenProvider.setMessageConverters(converters());
oAuth2RestTemplate.setAccessTokenProvider(accessTokenProvider);
oAuth2RestTemplate.setRetryBadAccessTokens(true);
oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(), client.getClientId());
tokenServices.setRestTemplate(oAuth2RestTemplate);
oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
return oAuth2ClientAuthenticationFilter;
}
public static Filter general(AuthorizationCodeResourceDetails client, ResourceServerProperties resourceServerProperties, String path, OAuth2ClientContext oauth2ClientContext) {
OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = new OAuth2ClientAuthenticationProcessingFilter(path){
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
FilterChain chain, Authentication authResult) throws IOException, ServletException {
super.successfulAuthentication(request, response, chain, authResult);
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
log.warn(new Gson().toJson(authResult));
log.warn(new Gson().toJson(accessToken));
}
};
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client, oauth2ClientContext);
oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(), client.getClientId());
tokenServices.setRestTemplate(oAuth2RestTemplate);
oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
return oAuth2ClientAuthenticationFilter;
}
private Filter oauthFilter() {
logger.debug("Setup SSO filter for oauth");
OAuth2ClientAuthenticationProcessingFilter oauth2Filter = new OAuth2ClientAuthenticationProcessingFilter(
"/login/oauth");
OAuth2RestTemplate auth2RestTemplate = new OAuth2RestTemplate(oauth2(), oauth2ClientContext);
oauth2Filter.setRestTemplate(auth2RestTemplate);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(
oauth2Resource().getUserInfoUri(),
oauth2().getClientId());
tokenServices.setRestTemplate(auth2RestTemplate);
oauth2Filter.setTokenServices(new MyUserInfoTokenServices(
oauth2Resource().getUserInfoUri(),
oauth2().getClientId()));
return oauth2Filter;
}
@Test
public void clientConfigured() throws Exception {
this.context = new SpringApplicationBuilder(ClientConfiguration.class)
.properties("spring.config.name=test", "server.port=0",
"spring.cloud.gateway.enabled=false",
"security.oauth2.resource.userInfoUri:https://example.com",
"security.oauth2.client.clientId=foo")
.run();
RequestContextHolder.setRequestAttributes(
new ServletRequestAttributes(new MockHttpServletRequest()));
OAuth2ClientContext client = this.context.getBean(OAuth2ClientContext.class);
assertThat(client.getAccessToken()).isNull();
UserInfoTokenServices services = context.getBean(UserInfoTokenServices.class);
OAuth2RestTemplate template = (OAuth2RestTemplate) ReflectionTestUtils
.getField(services, "restTemplate");
MockRestServiceServer server = MockRestServiceServer.createServer(template);
server.expect(requestTo("https://example.com"))
.andRespond(withSuccess("{\"id\":\"user\"}", MediaType.APPLICATION_JSON));
services.loadAuthentication("FOO");
assertThat(client.getAccessToken().getValue()).isEqualTo("FOO");
server.verify();
}
private Filter ssoFilter() {
OAuth2ClientAuthenticationProcessingFilter facebookFilter = new OAuth2ClientAuthenticationProcessingFilter("/login");
OAuth2RestTemplate facebookTemplate = new OAuth2RestTemplate(facebook(), oauth2ClientContext);
facebookFilter.setRestTemplate(facebookTemplate);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(facebookResource().getUserInfoUri(), facebook().getClientId());
tokenServices.setRestTemplate(facebookTemplate);
facebookFilter.setTokenServices(tokenServices);
SavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler = new SavedRequestAwareAuthenticationSuccessHandler();
authenticationSuccessHandler.setUseReferer(true);
authenticationSuccessHandler.setTargetUrlParameter("continue");
facebookFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
return facebookFilter;
}