Skip to content

Commit 918536a

Browse files
author
Phillip Webb
committed
Merge pull request #1050 from habuma/master
* pull1050: Add ProviderSignInController for Spring Social
2 parents 8f32b87 + f2351f1 commit 918536a

File tree

1 file changed

+86
-6
lines changed

1 file changed

+86
-6
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,37 @@
1616

1717
package org.springframework.boot.autoconfigure.social;
1818

19+
import java.util.List;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
1922
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2023
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2125
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2226
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
2328
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2429
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2530
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
2631
import org.springframework.context.annotation.Bean;
2732
import org.springframework.context.annotation.Configuration;
33+
import org.springframework.security.core.Authentication;
34+
import org.springframework.security.core.context.SecurityContext;
35+
import org.springframework.security.core.context.SecurityContextHolder;
2836
import org.springframework.social.UserIdSource;
2937
import org.springframework.social.config.annotation.EnableSocial;
3038
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
3139
import org.springframework.social.connect.ConnectionFactoryLocator;
3240
import org.springframework.social.connect.ConnectionRepository;
41+
import org.springframework.social.connect.UsersConnectionRepository;
3342
import org.springframework.social.connect.web.ConnectController;
43+
import org.springframework.social.connect.web.ConnectInterceptor;
44+
import org.springframework.social.connect.web.DisconnectInterceptor;
45+
import org.springframework.social.connect.web.ProviderSignInController;
46+
import org.springframework.social.connect.web.ProviderSignInInterceptor;
47+
import org.springframework.social.connect.web.SignInAdapter;
48+
import org.springframework.util.Assert;
49+
import org.springframework.util.CollectionUtils;
3450
import org.springframework.web.servlet.ViewResolver;
3551
import org.springframework.web.servlet.view.BeanNameViewResolver;
3652

@@ -51,23 +67,61 @@ public class SocialWebAutoConfiguration {
5167
@ConditionalOnWebApplication
5268
protected static class SocialAutoConfigurationAdapter extends SocialConfigurerAdapter {
5369

70+
@Autowired(required = false)
71+
private List<ConnectInterceptor<?>> connectInterceptors;
72+
73+
@Autowired(required = false)
74+
private List<DisconnectInterceptor<?>> disconnectInterceptors;
75+
76+
@Autowired(required = false)
77+
private List<ProviderSignInInterceptor<?>> signInInterceptors;
78+
5479
@Bean
5580
@ConditionalOnMissingBean(ConnectController.class)
5681
public ConnectController connectController(
57-
ConnectionFactoryLocator connectionFactoryLocator,
58-
ConnectionRepository connectionRepository) {
59-
return new ConnectController(connectionFactoryLocator, connectionRepository);
82+
ConnectionFactoryLocator factoryLocator, ConnectionRepository repository) {
83+
ConnectController controller = new ConnectController(factoryLocator,
84+
repository);
85+
if (!CollectionUtils.isEmpty(this.connectInterceptors)) {
86+
controller.setConnectInterceptors(this.connectInterceptors);
87+
}
88+
if (!CollectionUtils.isEmpty(this.disconnectInterceptors)) {
89+
controller.setDisconnectInterceptors(this.disconnectInterceptors);
90+
}
91+
return controller;
6092
}
6193

6294
@Bean
6395
@ConditionalOnMissingBean(BeanNameViewResolver.class)
6496
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
6597
public ViewResolver beanNameViewResolver() {
66-
BeanNameViewResolver bnvr = new BeanNameViewResolver();
67-
bnvr.setOrder(Integer.MIN_VALUE);
68-
return bnvr;
98+
BeanNameViewResolver viewResolver = new BeanNameViewResolver();
99+
viewResolver.setOrder(Integer.MIN_VALUE);
100+
return viewResolver;
101+
}
102+
103+
@Bean
104+
@ConditionalOnBean(SignInAdapter.class)
105+
@ConditionalOnMissingBean(ProviderSignInController.class)
106+
public ProviderSignInController signInController(
107+
ConnectionFactoryLocator factoryLocator,
108+
UsersConnectionRepository usersRepository, SignInAdapter signInAdapter) {
109+
ProviderSignInController controller = new ProviderSignInController(
110+
factoryLocator, usersRepository, signInAdapter);
111+
if (!CollectionUtils.isEmpty(this.signInInterceptors)) {
112+
controller.setSignInInterceptors(this.signInInterceptors);
113+
}
114+
return controller;
69115
}
70116

117+
}
118+
119+
@Configuration
120+
@EnableSocial
121+
@ConditionalOnWebApplication
122+
@ConditionalOnMissingClass(SecurityContextHolder.class)
123+
protected static class AnonymousUserIdSourceConfig extends SocialConfigurerAdapter {
124+
71125
@Override
72126
public UserIdSource getUserIdSource() {
73127
return new UserIdSource() {
@@ -80,4 +134,30 @@ public String getUserId() {
80134

81135
}
82136

137+
@Configuration
138+
@EnableSocial
139+
@ConditionalOnWebApplication
140+
@ConditionalOnClass(SecurityContextHolder.class)
141+
protected static class AuthenticationUserIdSourceConfig extends
142+
SocialConfigurerAdapter {
143+
144+
@Override
145+
public UserIdSource getUserIdSource() {
146+
return new SecurityContextUserIdSource();
147+
}
148+
149+
}
150+
151+
private static class SecurityContextUserIdSource implements UserIdSource {
152+
153+
@Override
154+
public String getUserId() {
155+
SecurityContext context = SecurityContextHolder.getContext();
156+
Authentication authentication = context.getAuthentication();
157+
Assert.state(authentication != null, "Unable to get a "
158+
+ "ConnectionRepository: no user signed in");
159+
return authentication.getName();
160+
}
161+
162+
}
83163
}

0 commit comments

Comments
 (0)