001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.social; 018 019import java.util.List; 020 021import org.thymeleaf.spring4.resourceresolver.SpringResourceResourceResolver; 022 023import org.springframework.beans.factory.ObjectProvider; 024import org.springframework.boot.autoconfigure.AutoConfigureAfter; 025import org.springframework.boot.autoconfigure.AutoConfigureBefore; 026import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 027import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 028import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 029import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 030import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; 031import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 032import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; 033import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; 034import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; 035import org.springframework.context.annotation.Bean; 036import org.springframework.context.annotation.Configuration; 037import org.springframework.core.Ordered; 038import org.springframework.security.core.Authentication; 039import org.springframework.security.core.context.SecurityContext; 040import org.springframework.security.core.context.SecurityContextHolder; 041import org.springframework.social.UserIdSource; 042import org.springframework.social.config.annotation.EnableSocial; 043import org.springframework.social.config.annotation.SocialConfigurerAdapter; 044import org.springframework.social.connect.ConnectionFactoryLocator; 045import org.springframework.social.connect.ConnectionRepository; 046import org.springframework.social.connect.UsersConnectionRepository; 047import org.springframework.social.connect.web.ConnectController; 048import org.springframework.social.connect.web.ConnectInterceptor; 049import org.springframework.social.connect.web.DisconnectInterceptor; 050import org.springframework.social.connect.web.ProviderSignInController; 051import org.springframework.social.connect.web.ProviderSignInInterceptor; 052import org.springframework.social.connect.web.SignInAdapter; 053import org.springframework.social.connect.web.thymeleaf.SpringSocialDialect; 054import org.springframework.util.Assert; 055import org.springframework.util.CollectionUtils; 056import org.springframework.web.servlet.view.BeanNameViewResolver; 057 058/** 059 * {@link EnableAutoConfiguration Auto-configuration} for Spring Social's web connection 060 * support. 061 * 062 * @author Craig Walls 063 * @since 1.1.0 064 */ 065@Configuration 066@ConditionalOnClass({ ConnectController.class, SocialConfigurerAdapter.class }) 067@ConditionalOnBean({ ConnectionFactoryLocator.class, UsersConnectionRepository.class }) 068@AutoConfigureBefore(ThymeleafAutoConfiguration.class) 069@AutoConfigureAfter(WebMvcAutoConfiguration.class) 070public class SocialWebAutoConfiguration { 071 072 @Configuration 073 @EnableSocial 074 @ConditionalOnWebApplication 075 protected static class SocialAutoConfigurationAdapter 076 extends SocialConfigurerAdapter { 077 078 private final List<ConnectInterceptor<?>> connectInterceptors; 079 080 private final List<DisconnectInterceptor<?>> disconnectInterceptors; 081 082 private final List<ProviderSignInInterceptor<?>> signInInterceptors; 083 084 public SocialAutoConfigurationAdapter( 085 ObjectProvider<List<ConnectInterceptor<?>>> connectInterceptorsProvider, 086 ObjectProvider<List<DisconnectInterceptor<?>>> disconnectInterceptorsProvider, 087 ObjectProvider<List<ProviderSignInInterceptor<?>>> signInInterceptorsProvider) { 088 this.connectInterceptors = connectInterceptorsProvider.getIfAvailable(); 089 this.disconnectInterceptors = disconnectInterceptorsProvider.getIfAvailable(); 090 this.signInInterceptors = signInInterceptorsProvider.getIfAvailable(); 091 } 092 093 @Bean 094 @ConditionalOnMissingBean(ConnectController.class) 095 public ConnectController connectController( 096 ConnectionFactoryLocator factoryLocator, 097 ConnectionRepository repository) { 098 ConnectController controller = new ConnectController(factoryLocator, 099 repository); 100 if (!CollectionUtils.isEmpty(this.connectInterceptors)) { 101 controller.setConnectInterceptors(this.connectInterceptors); 102 } 103 if (!CollectionUtils.isEmpty(this.disconnectInterceptors)) { 104 controller.setDisconnectInterceptors(this.disconnectInterceptors); 105 } 106 return controller; 107 } 108 109 @Bean 110 @ConditionalOnMissingBean 111 @ConditionalOnProperty(prefix = "spring.social", name = "auto-connection-views") 112 public BeanNameViewResolver beanNameViewResolver() { 113 BeanNameViewResolver viewResolver = new BeanNameViewResolver(); 114 viewResolver.setOrder(Ordered.HIGHEST_PRECEDENCE); 115 return viewResolver; 116 } 117 118 @Bean 119 @ConditionalOnBean(SignInAdapter.class) 120 @ConditionalOnMissingBean 121 public ProviderSignInController signInController( 122 ConnectionFactoryLocator factoryLocator, 123 UsersConnectionRepository usersRepository, SignInAdapter signInAdapter) { 124 ProviderSignInController controller = new ProviderSignInController( 125 factoryLocator, usersRepository, signInAdapter); 126 if (!CollectionUtils.isEmpty(this.signInInterceptors)) { 127 controller.setSignInInterceptors(this.signInInterceptors); 128 } 129 return controller; 130 } 131 132 } 133 134 @Configuration 135 @EnableSocial 136 @ConditionalOnWebApplication 137 @ConditionalOnMissingClass("org.springframework.security.core.context.SecurityContextHolder") 138 protected static class AnonymousUserIdSourceConfig extends SocialConfigurerAdapter { 139 140 @Override 141 public UserIdSource getUserIdSource() { 142 return new UserIdSource() { 143 @Override 144 public String getUserId() { 145 return "anonymous"; 146 } 147 }; 148 } 149 150 } 151 152 @Configuration 153 @EnableSocial 154 @ConditionalOnWebApplication 155 @ConditionalOnClass(SecurityContextHolder.class) 156 protected static class AuthenticationUserIdSourceConfig 157 extends SocialConfigurerAdapter { 158 159 @Override 160 public UserIdSource getUserIdSource() { 161 return new SecurityContextUserIdSource(); 162 } 163 164 } 165 166 @Configuration 167 @ConditionalOnClass(SpringResourceResourceResolver.class) 168 protected static class SpringSocialThymeleafConfig { 169 170 @Bean 171 @ConditionalOnMissingBean 172 public SpringSocialDialect springSocialDialect() { 173 return new SpringSocialDialect(); 174 } 175 176 } 177 178 private static class SecurityContextUserIdSource implements UserIdSource { 179 180 @Override 181 public String getUserId() { 182 SecurityContext context = SecurityContextHolder.getContext(); 183 Authentication authentication = context.getAuthentication(); 184 Assert.state(authentication != null, 185 "Unable to get a " + "ConnectionRepository: no user signed in"); 186 return authentication.getName(); 187 } 188 189 } 190 191}