org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices Java Examples

The following examples show how to use org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringSecurityConfiguration.java    From crnk-example with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: OAuth2Util.java    From DAFramework with MIT License 6 votes vote down vote up
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;
}
 
Example #3
Source File: OAuth2Util.java    From DAFramework with MIT License 6 votes vote down vote up
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;
}
 
Example #4
Source File: WebSecurityConfig.java    From mojito with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #5
Source File: ResourceServerTokenRelayAutoConfigurationTests.java    From spring-cloud-security with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #6
Source File: SecurityConfig.java    From movie-db-java-on-azure with MIT License 5 votes vote down vote up
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;
}