我为facebook,linkedin和google创建了社交登录。登录过程工作正常,但我想在我的数据库中存储用户数据,例如emailId,社交提供商,我面临着问题。这是一个春天 - Mybatis项目。
我的 spring_social_security.xml 类包含所有映射
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<bean id="socialAuthenticationProvider" class="org.springframework.social.security.SocialAuthenticationProvider">
<constructor-arg ref="inMemoryUsersConnectionRepository"/>
<constructor-arg ref="socialUserDetailService"/>
</bean>
<!-- social login filter which is a pre authentication filter and works for /auth service url -->
<bean id="socialAuthenticationFilter" class="org.springframework.social.security.SocialAuthenticationFilter">
<constructor-arg name="authManager" ref="authenticationManager"/>
<constructor-arg name="userIdSource" ref="userIdSource"/>
<constructor-arg name="usersConnectionRepository" ref="inMemoryUsersConnectionRepository"/>
<constructor-arg name="authServiceLocator" ref="appSocialAuthenticationServiceRegistry"/>
<property name="authenticationSuccessHandler" ref="successHandler"/>
</bean>
<!-- inmemory connection repository which holds connection repository per local user -->
<bean id="inMemoryUsersConnectionRepository"
class="org.springframework.social.connect.mem.InMemoryUsersConnectionRepository">
<constructor-arg name="connectionFactoryLocator" ref="appSocialAuthenticationServiceRegistry"/>
<property name="connectionSignUp" ref="connectionSignUp"/>
</bean>
<!-- service registry will holds connection factory of each social provider -->
<bean id="appSocialAuthenticationServiceRegistry"
class="com.technoshinelabs.ulearn.registry.AppSocialAuthenticationServiceRegistry">
<constructor-arg>
<list>
<ref bean="facebookAuthenticationService"/>
<ref bean="linkedInAuthenticationService"/>
<ref bean="googleAuthenticationService"/>
</list>
</constructor-arg>
</bean>
<bean id="facebookAuthenticationService"
class="org.springframework.social.facebook.security.FacebookAuthenticationService">
<constructor-arg name="apiKey" value="${facebook.api.key}"/>
<constructor-arg name="appSecret" value="${facebook.api.secret}"/>
</bean>
<bean id="linkedInAuthenticationService"
class="org.springframework.social.linkedin.security.LinkedInAuthenticationService">
<constructor-arg name="apiKey" value="${linkedin.api.key}"/>
<constructor-arg name="appSecret" value="${linkedin.api.secret}"/>
</bean>
<bean id="googleAuthenticationService"
class="org.springframework.social.google.security.GoogleAuthenticationService">
<constructor-arg name="apiKey" value="${google.api.key}"/>
<constructor-arg name="appSecret" value="${google.api.secret}"/>
</bean>
<bean id="userIdSource" class="org.springframework.social.security.AuthenticationNameUserIdSource"/>
<!-- If no local user is associated to a social connection then connection sign up will create a new local user and map it to social user -->
<bean id="connectionSignUp" class="com.technoshinelabs.ulearn.registry.AppConnectionSignUp"/>
AppConnectionSignUp.java 类包含执行应用程序的execute()。
public class AppConnectionSignUp implements ConnectionSignUp {
@Override
public String execute(final Connection<?> connection) {
SignUpForm userDetails = toUserRegistrationObject(connection.getKey().getProviderUserId(),
SecurityUtil.toSocialProvider(connection.getKey().getProviderId()), connection.fetchUserProfile());
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
return user.getUsername();
} catch (UserAlreadyExistAuthenticationException e) {
e.printStackTrace();
}
return null;
}
private SignUpForm toUserRegistrationObject(final String userId, final SocialProvider socialProvider,
final UserProfile userProfile) {
return SignUpForm.getBuilder().addUserId(userId).addFirstName(userProfile.getFirstName())
.addEmail(userProfile.getEmail()).addPassword(userProfile.getName()).addSocialProvider(socialProvider)
.build();
}
}
AppAuthenticationEntryPoint.java
public class AppAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
/**
* @param loginFormUrl URL where the login page can be found. Should either be
* relative to the web-app context path (include a leading {@code /}) or an absolute
* URL.
*/
public AppAuthenticationEntryPoint(final String loginFormUrl) {
super(loginFormUrl);
}
/**
* Performs the redirect (or forward) to the login form URL.
*/
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
// redirect to login page. Use https if forceHttps true
String redirectUrl = buildRedirectUrlToLoginPage(request, response, authException);
System.out.println("redirect Url: "+ redirectUrl);
redirectStrategy.sendRedirect(request, response, redirectUrl);
}
}