de.pksoftware.springstrap.core.config.FacebookLoginConfigBase.java Source code

Java tutorial

Introduction

Here is the source code for de.pksoftware.springstrap.core.config.FacebookLoginConfigBase.java

Source

/*
 * 
 * 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 java.net.URI;

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.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 FacebookLoginConfigBase {

    public static final String USER_INFO_URI = "https://graph.facebook.com/me";

    private String clientId = "284721529457";

    private String clientSecret = "135ab86949c27f615c486182798dcdaa";

    //@Autowired
    //private OAuth2ClientContext oAuth2ClientContext;

    @Bean
    protected OAuth2ProtectedResourceDetails facebookOAuth2DetailsBean() {
        AuthorizationCodeResourceDetails facebookOAuth2Details = new AuthorizationCodeResourceDetails();
        facebookOAuth2Details.setAuthenticationScheme(form);
        facebookOAuth2Details.setClientAuthenticationScheme(form);
        facebookOAuth2Details.setClientId(clientId);
        facebookOAuth2Details.setClientSecret(clientSecret);
        facebookOAuth2Details.setUserAuthorizationUri("http://www.facebook.com/dialog/oauth");
        facebookOAuth2Details.setAccessTokenUri("https://graph.facebook.com/oauth/access_token");
        facebookOAuth2Details.setScope(asList("email"));
        facebookOAuth2Details.setUseCurrentUri(true);
        return facebookOAuth2Details;
    }

    @Resource
    @Qualifier("accessTokenRequest")
    private AccessTokenRequest accessTokenRequest;

    @Bean
    @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
    public OAuth2RestOperations facebookOAuth2RestTemplate() {
        return new OAuth2RestTemplate(facebookOAuth2DetailsBean(),
                new DefaultOAuth2ClientContext(accessTokenRequest));
    }
}