16
16
17
17
package org .springframework .boot .autoconfigure .social ;
18
18
19
+ import java .util .List ;
20
+
21
+ import org .springframework .beans .factory .annotation .Autowired ;
19
22
import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
20
23
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
24
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
21
25
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
22
26
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
27
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingClass ;
23
28
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
24
29
import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication ;
25
30
import org .springframework .boot .autoconfigure .web .WebMvcAutoConfiguration ;
26
31
import org .springframework .context .annotation .Bean ;
27
32
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 ;
28
36
import org .springframework .social .UserIdSource ;
29
37
import org .springframework .social .config .annotation .EnableSocial ;
30
38
import org .springframework .social .config .annotation .SocialConfigurerAdapter ;
31
39
import org .springframework .social .connect .ConnectionFactoryLocator ;
32
40
import org .springframework .social .connect .ConnectionRepository ;
41
+ import org .springframework .social .connect .UsersConnectionRepository ;
33
42
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 ;
34
50
import org .springframework .web .servlet .ViewResolver ;
35
51
import org .springframework .web .servlet .view .BeanNameViewResolver ;
36
52
@@ -51,23 +67,61 @@ public class SocialWebAutoConfiguration {
51
67
@ ConditionalOnWebApplication
52
68
protected static class SocialAutoConfigurationAdapter extends SocialConfigurerAdapter {
53
69
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
+
54
79
@ Bean
55
80
@ ConditionalOnMissingBean (ConnectController .class )
56
81
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 ;
60
92
}
61
93
62
94
@ Bean
63
95
@ ConditionalOnMissingBean (BeanNameViewResolver .class )
64
96
@ ConditionalOnProperty (prefix = "spring.social." , value = "auto-connection-views" )
65
97
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 ;
69
115
}
70
116
117
+ }
118
+
119
+ @ Configuration
120
+ @ EnableSocial
121
+ @ ConditionalOnWebApplication
122
+ @ ConditionalOnMissingClass (SecurityContextHolder .class )
123
+ protected static class AnonymousUserIdSourceConfig extends SocialConfigurerAdapter {
124
+
71
125
@ Override
72
126
public UserIdSource getUserIdSource () {
73
127
return new UserIdSource () {
@@ -80,4 +134,30 @@ public String getUserId() {
80
134
81
135
}
82
136
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
+ }
83
163
}
0 commit comments