Java tutorial
/* * * Springstrap * * @author Jan Philipp Knller <info@pksoftware.de> * * Homepage: http://ui5strap.com/springstrap * * Copyright (c) 2013-2014 Jan Philipp Knller <info@pksoftware.de> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * Released under Apache2 license: http://www.apache.org/licenses/LICENSE-2.0.txt * */ package de.pksoftware.springstrap.core.config; import static java.util.Arrays.asList; import static org.springframework.security.oauth2.common.AuthenticationScheme.form; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext; import org.springframework.security.oauth2.client.OAuth2ClientContext; import org.springframework.security.oauth2.client.OAuth2RestOperations; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; import org.springframework.security.oauth2.client.token.AccessTokenRequest; import org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest; import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client; @Configuration @EnableOAuth2Client public class GoogleLoginConfigBase { public static final String USER_INFO_URI = "https://www.googleapis.com/oauth2/v2/userinfo"; @Value("${google.oauth2.clientId}") private String clientId; @Value("${google.oauth2.clientSecret}") private String clientSecret; // @Autowired // private OAuth2ClientContext oAuth2ClientContext; @Bean protected OAuth2ProtectedResourceDetails googleOAuth2DetailsBean() { AuthorizationCodeResourceDetails googleOAuth2Details = new AuthorizationCodeResourceDetails(); googleOAuth2Details.setAuthenticationScheme(form); googleOAuth2Details.setClientAuthenticationScheme(form); googleOAuth2Details.setClientId(clientId); googleOAuth2Details.setClientSecret(clientSecret); googleOAuth2Details.setUserAuthorizationUri("https://accounts.google.com/o/oauth2/auth"); googleOAuth2Details.setAccessTokenUri("https://www.googleapis.com/oauth2/v3/token"); googleOAuth2Details.setScope(asList("openid")); return googleOAuth2Details; } @Resource @Qualifier("accessTokenRequest") private AccessTokenRequest accessTokenRequest; @Bean @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES) public OAuth2RestOperations googleOAuth2RestTemplate(OAuth2ClientContext oAuth2ClientContext) { return new OAuth2RestTemplate(googleOAuth2DetailsBean(), new DefaultOAuth2ClientContext(accessTokenRequest)); } }