【问题标题】:Spring Social facebook + Spring SecuritySpring Social facebook + Spring Security
【发布时间】:2013-04-01 21:48:34
【问题描述】:

我想使用 Spring Security 将 Spring Social facebook 集成到我的应用程序中(我使用 xml 配置)。我所需要的只是将 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} 中提取“userId”。所以,我的问题:我怎样才能通过“userId” 如果我想使用SecurityContextHolder.getContext().getAuthentication().getPrincipal() 获取此“userId”,请使用此方法。

我看到的唯一方法是创建JdbcUsersConnectionRepository 的实现并重新定义createConnectionRepository(String userId) 方法。但也许有更优雅的解决方案。

【问题讨论】:

    标签: facebook spring spring-security spring-social


    【解决方案1】:

    还有一种方法:

    <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>
    

    【讨论】:

    • 在 Spring Social 1.0.x 中,SpEL 是最好的方法(虽然有点麻烦)。在 Spring Social 1.1.0 中有一个新的基于 XML 的配置命名空间和一个 UserIdSource 接口。您实现 UserIdSource 以查找用户 ID(无论您认为合适)并将其配置为 bean。 XML 配置元素将查找该 UserIdSource 并使用它。
    • FWIW,SpEL 表达式只是字符串,因此不容易测试或不一定是类型安全的,这是他们在这里感觉很笨拙的主要原因。但是由于您选择 XML 进行配置这一事实,您已经决定反对类型安全的配置选项。这让我想知道为什么你不想使用 Java 配置,这是配置 Spring 的更强大和类型安全的选项?
    • 感谢克雷格的澄清。 UserIdSource 接口是个好消息(我们需要等待 1.1.0.RELEASE 才能在生产代码中使用它)。对于你的第二个问题:原因很简单。我们有一个项目已经为 Spring 和 Spring Security(框架 bean)使用基于 xml 的配置。注释仅用于应用程序的 bean。我认为支持团队以一种格式(xml)拥有所有配置内容会更方便。没有办法使用 Spring Security AFAIK 进行 java conf。
    • 谢谢@Maksym,你帮了我很多。
    • 你说得对,在 JavaConfig 中还没有简单的方法来进行 Spring Security 配置。那么我的选择仍然是尽可能多地进行自动配置(组件扫描/自动装配),尽可能多地使用 JavaConfig 来处理必须显式配置的事情,并且只在 JavaConfig 的情况下使用 XML 配置不实用(例如,Spring Security)。也就是说,Spring Security 3.2.0.M1 有一些对 JavaConfig 的初始支持——所以这将在 Spring Security 3.2.0 中提供。
    猜你喜欢
    • 1970-01-01
    • 2015-03-24
    • 2012-01-24
    • 2017-03-03
    • 1970-01-01
    • 2014-03-01
    • 2015-03-26
    • 2016-02-13
    • 2012-09-08
    相关资源
    最近更新 更多