我想使用 Spring Security(我使用 xml 配置)将 Spring Social facebook 集成到我的应用程序中。我所需要的只是将 facebook 帐户与我的应用程序帐户连接起来。在简单的例子中,我发现了这个:
<bean id="connectionRepository" factory-method="createConnectionRepository"
factory-bean="usersConnectionRepository" scope="request">
<constructor-arg value="#{request.userPrincipal.name}" />
<aop:scoped-proxy proxy-target-class="false" />
</bean>
因此,据我了解,这种方法开始发挥作用:
public ConnectionRepository createConnectionRepository(String userId) {
if (userId == null) {
throw new IllegalArgumentException("userId cannot be null");
}
return new JdbcConnectionRepository(userId, jdbcTemplate, connectionFactoryLocator, textEncryptor, tablePrefix);
}
它从#{request.userPrincipal.name}
resives "userId
"。所以,我的问题是:如何传递“userId
”
如果我想使用 SecurityContextHolder.getContext().getAuthentication().getPrincipal()
获取此“userId
”,请使用此方法。
我看到的唯一方法是创建我的 JdbcUsersConnectionRepository
实现并重新定义 createConnectionRepository(String userId)
方法。但也许有更优雅的解决方案。
最佳答案
还有一种方式:
<bean id="connectionRepository" factory-method="createConnectionRepository" factory-bean="usersConnectionRepository"
scope="request">
<constructor-arg value="#{authenticationService.getAuthenticatedUsername()}" />
<aop:scoped-proxy proxy-target-class="false" />
</bean>
@Service("authenticationService")
public class AuthenticationService {
public String getAuthenticatedUsername() {
return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
}
你也可以在 SPeL 中完成它(我不喜欢这种依赖关系):
<bean id="connectionRepository" factory-method="createConnectionRepository" factory-bean="usersConnectionRepository"
scope="request">
<constructor-arg value="#{T(org.springframework.security.core.context.SecurityContextHolder).getContext().getAuthentication().getPrincipal()}" />
<aop:scoped-proxy proxy-target-class="false" />
</bean>
关于facebook - Spring Social facebook + Spring Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15919315/