Skip to content

Commit f006b12

Browse files
Craig Wallswilkinsona
authored andcommitted
Add Spring Social autoconfiguration
1 parent 5807c87 commit f006b12

File tree

22 files changed

+920
-1
lines changed

22 files changed

+920
-1
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,36 @@
221221
<artifactId>spring-mobile-device</artifactId>
222222
<optional>true</optional>
223223
</dependency>
224+
<dependency>
225+
<groupId>org.springframework.social</groupId>
226+
<artifactId>spring-social-config</artifactId>
227+
<optional>true</optional>
228+
</dependency>
229+
<dependency>
230+
<groupId>org.springframework.social</groupId>
231+
<artifactId>spring-social-core</artifactId>
232+
<optional>true</optional>
233+
</dependency>
234+
<dependency>
235+
<groupId>org.springframework.social</groupId>
236+
<artifactId>spring-social-web</artifactId>
237+
<optional>true</optional>
238+
</dependency>
239+
<dependency>
240+
<groupId>org.springframework.social</groupId>
241+
<artifactId>spring-social-facebook</artifactId>
242+
<optional>true</optional>
243+
</dependency>
244+
<dependency>
245+
<groupId>org.springframework.social</groupId>
246+
<artifactId>spring-social-twitter</artifactId>
247+
<optional>true</optional>
248+
</dependency>
249+
<dependency>
250+
<groupId>org.springframework.social</groupId>
251+
<artifactId>spring-social-linkedin</artifactId>
252+
<optional>true</optional>
253+
</dependency>
224254
<dependency>
225255
<groupId>org.thymeleaf</groupId>
226256
<artifactId>thymeleaf</artifactId>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.social;
18+
19+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
20+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
25+
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
26+
import org.springframework.boot.bind.RelaxedPropertyResolver;
27+
import org.springframework.context.EnvironmentAware;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Scope;
31+
import org.springframework.context.annotation.ScopedProxyMode;
32+
import org.springframework.core.env.Environment;
33+
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
34+
import org.springframework.social.config.annotation.EnableSocial;
35+
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
36+
import org.springframework.social.connect.Connection;
37+
import org.springframework.social.connect.ConnectionRepository;
38+
import org.springframework.social.connect.web.GenericConnectionStatusView;
39+
import org.springframework.social.facebook.api.Facebook;
40+
import org.springframework.social.facebook.api.impl.FacebookTemplate;
41+
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
42+
import org.springframework.web.servlet.View;
43+
44+
/**
45+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Social connectivity
46+
* with Facebook.
47+
*
48+
* @author Craig Walls
49+
*/
50+
@Configuration
51+
@ConditionalOnClass({ FacebookConnectionFactory.class })
52+
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
53+
public class FacebookAutoConfiguration {
54+
55+
@Configuration
56+
@EnableSocial
57+
@ConditionalOnWebApplication
58+
protected static class FacebookAutoConfigurationAdapter extends SocialConfigurerAdapter implements EnvironmentAware {
59+
60+
private String appId;
61+
private String appSecret;
62+
63+
@Override
64+
public void setEnvironment(Environment env) {
65+
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env, "spring.social.");
66+
this.appId = propertyResolver.getRequiredProperty("facebook.appId");
67+
this.appSecret = propertyResolver.getRequiredProperty("facebook.appSecret");
68+
}
69+
70+
@Override
71+
public void addConnectionFactories(ConnectionFactoryConfigurer config, Environment env) {
72+
config.addConnectionFactory(new FacebookConnectionFactory(appId, appSecret));
73+
}
74+
75+
@Bean
76+
@ConditionalOnMissingBean(Facebook.class)
77+
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
78+
public Facebook facebook(ConnectionRepository repository) {
79+
Connection<Facebook> connection = repository.findPrimaryConnection(Facebook.class);
80+
return connection != null ? connection.getApi() : new FacebookTemplate();
81+
}
82+
83+
@Bean(name={"connect/facebookConnect", "connect/facebookConnected"})
84+
@ConditionalOnExpression("${spring.social.auto_connection_views:false}")
85+
public View facebookConnectView() {
86+
return new GenericConnectionStatusView("facebook", "Facebook");
87+
}
88+
89+
}
90+
91+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.social;
18+
19+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
20+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
25+
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
26+
import org.springframework.boot.bind.RelaxedPropertyResolver;
27+
import org.springframework.context.EnvironmentAware;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Scope;
31+
import org.springframework.context.annotation.ScopedProxyMode;
32+
import org.springframework.core.env.Environment;
33+
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
34+
import org.springframework.social.config.annotation.EnableSocial;
35+
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
36+
import org.springframework.social.connect.Connection;
37+
import org.springframework.social.connect.ConnectionRepository;
38+
import org.springframework.social.connect.web.GenericConnectionStatusView;
39+
import org.springframework.social.linkedin.api.LinkedIn;
40+
import org.springframework.social.linkedin.connect.LinkedInConnectionFactory;
41+
import org.springframework.web.servlet.View;
42+
43+
/**
44+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Social connectivity
45+
* with LinkedIn.
46+
*
47+
* @author Craig Walls
48+
*/
49+
@Configuration
50+
@ConditionalOnClass({ LinkedInConnectionFactory.class })
51+
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
52+
public class LinkedInAutoConfiguration {
53+
54+
@Configuration
55+
@EnableSocial
56+
@ConditionalOnWebApplication
57+
protected static class LinkedInAutoConfigurationAdapter extends SocialConfigurerAdapter implements EnvironmentAware {
58+
59+
private String appId;
60+
private String appSecret;
61+
62+
@Override
63+
public void setEnvironment(Environment env) {
64+
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env, "spring.social.");
65+
this.appId = propertyResolver.getRequiredProperty("linkedin.appId");
66+
this.appSecret = propertyResolver.getRequiredProperty("linkedin.appSecret");
67+
}
68+
69+
@Override
70+
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
71+
cfConfig.addConnectionFactory(new LinkedInConnectionFactory(appId, appSecret));
72+
}
73+
74+
@Bean
75+
@ConditionalOnMissingBean(LinkedInConnectionFactory.class)
76+
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
77+
public LinkedIn linkedin(ConnectionRepository repository) {
78+
Connection<LinkedIn> connection = repository.findPrimaryConnection(LinkedIn.class);
79+
return connection != null ? connection.getApi() : null;
80+
}
81+
82+
@Bean(name={"connect/linkedinConnect", "connect/linkedinConnected"})
83+
@ConditionalOnExpression("${spring.social.auto_connection_views:false}")
84+
public View linkedInConnectView() {
85+
return new GenericConnectionStatusView("linkedin", "LinkedIn");
86+
}
87+
88+
}
89+
90+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.social;
18+
19+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
20+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
25+
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.social.UserIdSource;
29+
import org.springframework.social.config.annotation.EnableSocial;
30+
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
31+
import org.springframework.social.connect.ConnectionFactoryLocator;
32+
import org.springframework.social.connect.ConnectionRepository;
33+
import org.springframework.social.connect.web.ConnectController;
34+
import org.springframework.web.servlet.ViewResolver;
35+
import org.springframework.web.servlet.view.BeanNameViewResolver;
36+
37+
/**
38+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Social's web connection support.
39+
*
40+
* @author Craig Walls
41+
*/
42+
@Configuration
43+
@ConditionalOnClass({ ConnectController.class })
44+
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
45+
public class SocialWebAutoConfiguration {
46+
47+
@Configuration
48+
@EnableSocial
49+
@ConditionalOnWebApplication
50+
protected static class SocialAutoConfigurationAdapter extends SocialConfigurerAdapter {
51+
@Bean
52+
@ConditionalOnMissingBean(ConnectController.class)
53+
public ConnectController connectController(
54+
ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
55+
return new ConnectController(connectionFactoryLocator, connectionRepository);
56+
}
57+
58+
@Bean
59+
@ConditionalOnMissingBean(BeanNameViewResolver.class)
60+
@ConditionalOnExpression("${spring.social.auto_connection_views:false}")
61+
public ViewResolver beanNameViewResolver() {
62+
BeanNameViewResolver bnvr = new BeanNameViewResolver();
63+
bnvr.setOrder(Integer.MIN_VALUE);
64+
return bnvr;
65+
}
66+
67+
@Override
68+
public UserIdSource getUserIdSource() {
69+
return new UserIdSource() {
70+
@Override
71+
public String getUserId() {
72+
return "anonymous";
73+
}
74+
};
75+
}
76+
}
77+
78+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.social;
18+
19+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
20+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
25+
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
26+
import org.springframework.boot.bind.RelaxedPropertyResolver;
27+
import org.springframework.context.EnvironmentAware;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Scope;
31+
import org.springframework.context.annotation.ScopedProxyMode;
32+
import org.springframework.core.env.Environment;
33+
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
34+
import org.springframework.social.config.annotation.EnableSocial;
35+
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
36+
import org.springframework.social.connect.Connection;
37+
import org.springframework.social.connect.ConnectionRepository;
38+
import org.springframework.social.connect.web.GenericConnectionStatusView;
39+
import org.springframework.social.twitter.api.Twitter;
40+
import org.springframework.social.twitter.api.impl.TwitterTemplate;
41+
import org.springframework.social.twitter.connect.TwitterConnectionFactory;
42+
import org.springframework.web.servlet.View;
43+
44+
/**
45+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Social connectivity
46+
* with Twitter.
47+
*
48+
* @author Craig Walls
49+
*/
50+
@Configuration
51+
@ConditionalOnClass({ TwitterConnectionFactory.class })
52+
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
53+
public class TwitterAutoConfiguration {
54+
55+
@Configuration
56+
@EnableSocial
57+
@ConditionalOnWebApplication
58+
protected static class TwitterAutoConfigurationAdapter extends SocialConfigurerAdapter implements EnvironmentAware {
59+
60+
private String appId;
61+
private String appSecret;
62+
63+
@Override
64+
public void setEnvironment(Environment env) {
65+
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env, "spring.social.");
66+
this.appId = propertyResolver.getRequiredProperty("twitter.appId");
67+
this.appSecret = propertyResolver.getRequiredProperty("twitter.appSecret");
68+
}
69+
70+
@Override
71+
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
72+
cfConfig.addConnectionFactory(new TwitterConnectionFactory(appId, appSecret));
73+
}
74+
75+
@Bean
76+
@ConditionalOnMissingBean(TwitterConnectionFactory.class)
77+
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
78+
public Twitter twitter(ConnectionRepository repository) {
79+
Connection<Twitter> connection = repository.findPrimaryConnection(Twitter.class);
80+
return connection != null ? connection.getApi() : new TwitterTemplate(appId, appSecret);
81+
}
82+
83+
@Bean(name={"connect/twitterConnect", "connect/twitterConnected"})
84+
@ConditionalOnExpression("${spring.social.auto_connection_views:false}")
85+
public View twitterConnectView() {
86+
return new GenericConnectionStatusView("twitter", "Twitter");
87+
}
88+
89+
}
90+
91+
}

0 commit comments

Comments
 (0)