How to disconnect your app from your users facebook account using spring social facebook

Spring social is an umbrella project (one of the Spring Framework projects), which contains a bunch of modules for connecting your spring application with different social media services like facebook, twitter, github, etc.

For a project we are currently developing I need to implement facebook integration for easy registration and login with facebook. There are plenty of documentations and tutorials out there, which describe how to connect users account with facebook using spring-social-facebook project, but practically no (at least I cannot find any) guidance how to disconnect. Disconnecting means removing (revoking) authorization of your app to access your users facebook account.

 

Here is my solution for that:

FacebookTemplate facebookTemplate = new FacebookTemplate(accessToken);
boolean authorized = facebookTemplate.isAuthorized();
if (authorized) {
    facebookTemplate.delete(userId, "permissions");
}

You have to fill accessToken and userId values according to current users context. When the delete method successfully executed (without throwing any exception), your applications authorization will be removed from facebook account of your user.

See official facebook API documentation for more information about de-authorization of your app.

Reading OS environment variables in spring boot application using @Value annotation

There are multiple ways to access system environment varibles in a spring boot application using @Value annotation.

The simplest way is to use normal property access with ${property.name} scheme. Spring boot automatically reads environment variables and system properties, makes it accessible as normal properties. You should only replace underscores with dots if the environment variable contains any. For example if you want to access JAVA_HOME you have to transform it as java.home or JAVA.HOME (it is not case sensitive) .

@Value("${java.home}") 
private String javaHome;

Alternatively you can use SpEL (Spring Execution Language). One of the powerful abilities of SpEL is accessing Collections, Arrays, Lists or Properties. While spring intializes our application it collects informations from execution environment including system environments, system properties, application properties etc. and aggregate them in an Environment object. So we can access this object with SpEL to read an environment variable. Here we need slightly different signature compared to above example. Instead of ${ …} we have to write #{ spel.statement }.

@Value("#{environment['JAVA_HOME']}")
private String javaHome;

The second alternative is quite similar to latest. Here we use systemEnvironment instead of environment.

@Value("#{systemEnvironment['JAVA_HOME']}")
private String javaHome;

The systemEnvironment is a key-value Map, which contains only system environment variables and their values, where environment is a StandardEnvironment object which is a collection of different property sources (bootstrap, systemProperties, systemEnvironment, random, integrationTest, applicationConfig: [classpath:/application.properties], defaultProperties, Management Server).