Refine Spring Social auto-configuration

Refine auto-configuration for Spring Social to:

- Only auto-configure FB/Twitter/LinkedIn if the `app-id` property is
  set.
- Only configure ConnectController and ProviderSignInController if
  there is a ConnectionFactoryLocator.
- Auto-configure Spring Social's SpringSocialDialect for Thymeleaf if
  Thymeleaf is present.
- Added several tests around Spring Social auto-configuration.

Fixes gh-1118
This commit is contained in:
Phillip Webb 2014-06-18 13:34:53 -07:00
parent 179e1558f6
commit 46c46dbd0c
10 changed files with 270 additions and 72 deletions

View File

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.social;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -29,6 +30,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionRepository;
@ -46,7 +48,9 @@ import org.springframework.web.servlet.View;
* @since 1.1.0
*/
@Configuration
@ConditionalOnClass({ FacebookConnectionFactory.class })
@ConditionalOnClass({ SocialConfigurerAdapter.class, FacebookConnectionFactory.class })
@ConditionalOnProperty(prefix = "spring.social.facebook.", value = "app-id")
@AutoConfigureBefore(SocialWebAutoConfiguration.class)
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class FacebookAutoConfiguration {

View File

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.social;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -29,6 +30,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionRepository;
@ -45,7 +47,9 @@ import org.springframework.web.servlet.View;
* @since 1.1.0
*/
@Configuration
@ConditionalOnClass({ LinkedInConnectionFactory.class })
@ConditionalOnClass({ SocialConfigurerAdapter.class, LinkedInConnectionFactory.class })
@ConditionalOnProperty(prefix = "spring.social.linkedin.", value = "app-id")
@AutoConfigureBefore(SocialWebAutoConfiguration.class)
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class LinkedInAutoConfiguration {
@ -69,7 +73,7 @@ public class LinkedInAutoConfiguration {
}
@Bean
@ConditionalOnMissingBean(LinkedInConnectionFactory.class)
@ConditionalOnMissingBean(LinkedIn.class)
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public LinkedIn linkedin(ConnectionRepository repository) {
Connection<LinkedIn> connection = repository

View File

@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.social;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
@ -31,7 +30,6 @@ import org.springframework.social.connect.ConnectionFactory;
* @author Phillip Webb
* @since 1.1.0
*/
@ConditionalOnClass(SocialConfigurerAdapter.class)
abstract class SocialAutoConfigurerAdapter extends SocialConfigurerAdapter implements
EnvironmentAware {

View File

@ -20,6 +20,7 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -27,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -45,10 +47,12 @@ import org.springframework.social.connect.web.DisconnectInterceptor;
import org.springframework.social.connect.web.ProviderSignInController;
import org.springframework.social.connect.web.ProviderSignInInterceptor;
import org.springframework.social.connect.web.SignInAdapter;
import org.springframework.social.connect.web.thymeleaf.SpringSocialDialect;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.thymeleaf.spring4.SpringTemplateEngine;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Social's web connection
@ -59,6 +63,8 @@ import org.springframework.web.servlet.view.BeanNameViewResolver;
*/
@Configuration
@ConditionalOnClass({ ConnectController.class, SocialConfigurerAdapter.class })
@ConditionalOnBean({ ConnectionFactoryLocator.class, UsersConnectionRepository.class })
@AutoConfigureBefore(ThymeleafAutoConfiguration.class)
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class SocialWebAutoConfiguration {
@ -119,7 +125,7 @@ public class SocialWebAutoConfiguration {
@Configuration
@EnableSocial
@ConditionalOnWebApplication
@ConditionalOnMissingClass(name="org.springframework.security.core.context.SecurityContextHolder")
@ConditionalOnMissingClass(name = "org.springframework.security.core.context.SecurityContextHolder")
protected static class AnonymousUserIdSourceConfig extends SocialConfigurerAdapter {
@Override
@ -131,7 +137,6 @@ public class SocialWebAutoConfiguration {
}
};
}
}
@Configuration
@ -148,6 +153,18 @@ public class SocialWebAutoConfiguration {
}
@Configuration
@ConditionalOnClass(SpringTemplateEngine.class)
protected static class SpringSocialThymeleafConfig {
@Bean
@ConditionalOnMissingBean
public SpringSocialDialect springSocialDialect() {
return new SpringSocialDialect();
}
}
private static class SecurityContextUserIdSource implements UserIdSource {
@Override

View File

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.social;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -29,6 +30,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionRepository;
@ -46,7 +48,9 @@ import org.springframework.web.servlet.View;
* @since 1.1.0
*/
@Configuration
@ConditionalOnClass({ TwitterConnectionFactory.class })
@ConditionalOnClass({ SocialConfigurerAdapter.class, TwitterConnectionFactory.class })
@ConditionalOnProperty(prefix = "spring.social.twitter.", value = "app-id")
@AutoConfigureBefore(SocialWebAutoConfiguration.class)
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class TwitterAutoConfiguration {
@ -69,7 +73,7 @@ public class TwitterAutoConfiguration {
}
@Bean
@ConditionalOnMissingBean(TwitterConnectionFactory.class)
@ConditionalOnMissingBean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Twitter twitter(ConnectionRepository repository) {
Connection<Twitter> connection = repository

View File

@ -0,0 +1,73 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* 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.
*/
package org.springframework.boot.autoconfigure.social;
import org.junit.After;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.social.UserIdSource;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
/**
* Abstract base class for testing Spring Social auto-configuration.
*
* @author Craig Walls
*/
public class AbstractSocialAutoConfigurationTests {
protected AnnotationConfigWebApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
public AbstractSocialAutoConfigurationTests() {
super();
}
protected void assertConnectionFrameworkBeans() {
assertNotNull(this.context.getBean(UsersConnectionRepository.class));
assertNotNull(this.context.getBean(ConnectionRepository.class));
assertNotNull(this.context.getBean(ConnectionFactoryLocator.class));
assertNotNull(this.context.getBean(UserIdSource.class));
}
protected void assertNoConnectionFrameworkBeans() {
assertMissingBean(UsersConnectionRepository.class);
assertMissingBean(ConnectionRepository.class);
assertMissingBean(ConnectionFactoryLocator.class);
assertMissingBean(UserIdSource.class);
}
protected void assertMissingBean(Class<?> beanClass) {
try {
assertNotNull(this.context.getBean(beanClass));
fail("Unexpected bean in context of type " + beanClass.getName());
}
catch (NoSuchBeanDefinitionException ex) {
}
}
}

View File

@ -16,13 +16,8 @@
package org.springframework.boot.autoconfigure.social;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.social.UserIdSource;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@ -30,19 +25,10 @@ import static org.junit.Assert.assertNotNull;
/**
* Tests for {@link FacebookAutoConfiguration}.
*
*
* @author Craig Walls
*/
public class FacebookAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
public class FacebookAutoConfigurationTests extends AbstractSocialAutoConfigurationTests {
@Test
public void expectedSocialBeansCreated() throws Exception {
@ -51,14 +37,20 @@ public class FacebookAutoConfigurationTests {
"spring.social.facebook.appId:12345");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.social.facebook.appSecret:secret");
this.context.register(SocialWebAutoConfiguration.class);
this.context.register(FacebookAutoConfiguration.class);
this.context.register(SocialWebAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(UsersConnectionRepository.class));
assertNotNull(this.context.getBean(ConnectionRepository.class));
assertNotNull(this.context.getBean(ConnectionFactoryLocator.class));
assertNotNull(this.context.getBean(UserIdSource.class));
assertConnectionFrameworkBeans();
assertNotNull(this.context.getBean(Facebook.class));
}
@Test
public void noFacebookBeanCreatedWhenPropertiesArentSet() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.register(FacebookAutoConfiguration.class);
this.context.register(SocialWebAutoConfiguration.class);
this.context.refresh();
assertNoConnectionFrameworkBeans();
assertMissingBean(Facebook.class);
}
}

View File

@ -16,13 +16,8 @@
package org.springframework.boot.autoconfigure.social;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.social.UserIdSource;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.linkedin.api.LinkedIn;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@ -30,19 +25,10 @@ import static org.junit.Assert.assertNotNull;
/**
* Tests for {@link LinkedInAutoConfiguration}.
*
*
* @author Craig Walls
*/
public class LinkedInAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
public class LinkedInAutoConfigurationTests extends AbstractSocialAutoConfigurationTests {
@Test
public void expectedSocialBeansCreated() throws Exception {
@ -51,14 +37,21 @@ public class LinkedInAutoConfigurationTests {
"spring.social.linkedin.appId:12345");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.social.linkedin.appSecret:secret");
this.context.register(SocialWebAutoConfiguration.class);
this.context.register(LinkedInAutoConfiguration.class);
this.context.register(SocialWebAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(UsersConnectionRepository.class));
assertNotNull(this.context.getBean(ConnectionRepository.class));
assertNotNull(this.context.getBean(ConnectionFactoryLocator.class));
assertNotNull(this.context.getBean(UserIdSource.class));
assertConnectionFrameworkBeans();
assertNotNull(this.context.getBean(LinkedIn.class));
}
@Test
public void noLinkedInBeanCreatedIfPropertiesArentSet() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.register(LinkedInAutoConfiguration.class);
this.context.register(SocialWebAutoConfiguration.class);
this.context.refresh();
assertNoConnectionFrameworkBeans();
assertMissingBean(LinkedIn.class);
}
}

View File

@ -0,0 +1,120 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* 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.
*/
package org.springframework.boot.autoconfigure.social;
import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.linkedin.api.LinkedIn;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.junit.Assert.assertNotNull;
/**
* Tests for Spring Social configuration with multiple API providers.
*
* @author Craig Walls
*/
public class MultiApiAutoConfigurationTests extends AbstractSocialAutoConfigurationTests {
@Test
public void expectTwitterConfigurationOnly() throws Exception {
setupContext("spring.social.twitter.appId:12345",
"spring.social.twitter.appSecret:secret");
assertConnectionFrameworkBeans();
assertNotNull(this.context.getBean(Twitter.class));
assertMissingBean(Facebook.class);
assertMissingBean(LinkedIn.class);
}
@Test
public void expectFacebookConfigurationOnly() throws Exception {
setupContext("spring.social.facebook.appId:12345",
"spring.social.facebook.appSecret:secret");
assertConnectionFrameworkBeans();
assertNotNull(this.context.getBean(Facebook.class));
assertMissingBean(Twitter.class);
assertMissingBean(LinkedIn.class);
}
@Test
public void expectLinkedInConfigurationOnly() throws Exception {
setupContext("spring.social.linkedin.appId:12345",
"spring.social.linkedin.appSecret:secret");
assertConnectionFrameworkBeans();
assertNotNull(this.context.getBean(LinkedIn.class));
assertMissingBean(Twitter.class);
assertMissingBean(Facebook.class);
}
@Test
public void expectFacebookAndLinkedInConfigurationOnly() throws Exception {
setupContext("spring.social.facebook.appId:54321",
"spring.social.facebook.appSecret:shhhhh",
"spring.social.linkedin.appId:12345",
"spring.social.linkedin.appSecret:secret");
assertConnectionFrameworkBeans();
assertNotNull(this.context.getBean(Facebook.class));
assertNotNull(this.context.getBean(LinkedIn.class));
assertMissingBean(Twitter.class);
}
@Test
public void expectFacebookAndTwitterConfigurationOnly() throws Exception {
setupContext("spring.social.facebook.appId:54321",
"spring.social.facebook.appSecret:shhhhh",
"spring.social.twitter.appId:12345",
"spring.social.twitter.appSecret:secret");
assertConnectionFrameworkBeans();
assertNotNull(this.context.getBean(Facebook.class));
assertNotNull(this.context.getBean(Twitter.class));
assertMissingBean(LinkedIn.class);
}
@Test
public void expectLinkedInAndTwitterConfigurationOnly() throws Exception {
setupContext("spring.social.linkedin.appId:54321",
"spring.social.linkedin.appSecret:shhhhh",
"spring.social.twitter.appId:12345",
"spring.social.twitter.appSecret:secret");
assertConnectionFrameworkBeans();
assertNotNull(this.context.getBean(LinkedIn.class));
assertNotNull(this.context.getBean(Twitter.class));
assertMissingBean(Facebook.class);
}
@Test
public void noSocialBeansCreatedWhenPropertiesArentSet() throws Exception {
setupContext();
assertNoConnectionFrameworkBeans();
assertMissingBean(Twitter.class);
assertMissingBean(Facebook.class);
assertMissingBean(LinkedIn.class);
}
private void setupContext(String... environment) {
this.context = new AnnotationConfigWebApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, environment);
this.context.register(TwitterAutoConfiguration.class);
this.context.register(FacebookAutoConfiguration.class);
this.context.register(LinkedInAutoConfiguration.class);
this.context.register(SocialWebAutoConfiguration.class);
this.context.refresh();
}
}

View File

@ -16,13 +16,8 @@
package org.springframework.boot.autoconfigure.social;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.social.UserIdSource;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@ -30,19 +25,10 @@ import static org.junit.Assert.assertNotNull;
/**
* Tests for {@link TwitterAutoConfiguration}.
*
*
* @author Craig Walls
*/
public class TwitterAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
public class TwitterAutoConfigurationTests extends AbstractSocialAutoConfigurationTests {
@Test
public void expectedSocialBeansCreated() throws Exception {
@ -51,14 +37,21 @@ public class TwitterAutoConfigurationTests {
"spring.social.twitter.appId:12345");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.social.twitter.appSecret:secret");
this.context.register(SocialWebAutoConfiguration.class);
this.context.register(TwitterAutoConfiguration.class);
this.context.register(SocialWebAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(UsersConnectionRepository.class));
assertNotNull(this.context.getBean(ConnectionRepository.class));
assertNotNull(this.context.getBean(ConnectionFactoryLocator.class));
assertNotNull(this.context.getBean(UserIdSource.class));
assertConnectionFrameworkBeans();
assertNotNull(this.context.getBean(Twitter.class));
}
@Test
public void noTwitterBeanCreatedWhenPropertiesArentSet() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.register(TwitterAutoConfiguration.class);
this.context.register(SocialWebAutoConfiguration.class);
this.context.refresh();
assertNoConnectionFrameworkBeans();
assertMissingBean(Twitter.class);
}
}