1

我想为 Spring Security OpenID 实现一个新的 OpenIDConsumer。我在一个类中实现了OpenIDConsumer,然后添加了相应的配置applicationContext-security.xml,但是我的代码似乎根本没有执行。

这是来自的相关部分applicationContext-security.xml

<http auto-config="false">
    <intercept-url pattern="/**" access="ROLE_USER"/>
    <openid-login login-page="/auth/login"
            authentication-failure-url="/auth/login?login_error=true">
        <attribute-exchange>
            <openid-attribute name="email" type="http://axschema.org/contact/email" required="true"/>
        </attribute-exchange>
    </openid-login>
</http>
<b:bean id="openIdConsumer" class="sample.OpenIDTestConsumer">
    <b:property name="email" value="email"/>
</b:bean>

现在,类 sample.OpenIDTestConsumer 已初始化,但 Spring Security 不使用它,我认为使用原始类代替OpenID4JavaConsumer

该类sample.OpenIDTestConsumer实现了OpenIDConsumer接口并对其进行了初始化并setEmail设置了方法,但它不执行beginConsumptionendConsumption方法,这就是为什么我认为它只是因为applicationContext-security.xmlbean定义而创建但没有使用的原因。

问题是:如何粘合或设置自定义类作为 OpenIDConsumer 工作而不使用 Spring 实现?

4

2 回答 2

2

默认情况下,Spring Security在使用安全命名空间配置时向OpenID4JavaConsumer注册一个OpenIDAuthenticationFilter 。您不能使用命名空间定义自定义使用者。一个解决方案是使用自定义过滤器并手动配置 OpenIDAuthenticationFilter :applicationContext-security.xml

<http ...>
   ...
   <custom-filter position="OPENID_FILTER" ref="openIdFilter" />
</http>
<b:bean id="openIdFilter" class="org.springframework.security.openid.OpenIDAuthenticationFilter">
   <b:property name="consumer" ref="openidConsumer" />
   <!-- customize your filter (authentication failure url, login-page, … -->
</b:bean>
<b:bean id="openIdConsumer" class="sample.OpenIDTestConsumer">
   <!-- config attribute exchange here -->
   <b:property name="email" value="email"/>
</b:bean>
于 2012-09-06T18:20:11.970 回答
2

另一种解决方案是使用常见问题解答中的提示并使用 BeanPostProcessor。结果可能如下所示:

public class CustomOpenidConsumerBeanPostProcessor implements BeanPostProcessor {
    private OpenIDConsumer openidConsumer;

    public Object postProcessAfterInitialization(Object bean, String name) {
        if (bean instanceof OpenIDCOnsumer) {
           return openidConsumer;
        }
        return bean;
    }

    public Object postProcessBeforeInitialization(Object bean, String name) {
        return bean;
    }

    public void setOpenidConsumer(OpenIDConsumer openidConsumer) {
        this.openidConsumer = openidConsumer;
    }
}

那么您的配置将包括以下内容:

<b:bean class="CustomOpenidConsumerBeanPostProcessor">
    <b:property name="openidConsumer" ref="openIdConsumer"/>
  </b:bean>
<b:bean id="openIdConsumer" class="sample.OpenIDTestConsumer">
   <!-- config attribute exchange here -->
   <b:property name="email" value="email"/>
</b:bean>
于 2012-09-06T18:44:20.760 回答