2011-09-09 59 views
3

我有一些困难,使OpenIDAuthenticationFilter工作,我希望有人能帮助我。Spring安全与OpenIDAuthenticationFilter问题

我的弹簧的security.xml

 
<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:security="http://www.springframework.org/schema/security" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/security 
          http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <security:http pattern="/myapp/auth/login" security="none"/> 

    <security:http entry-point-ref="entryPoint"> 
     <security:intercept-url pattern="/myapp/main/*" access="ROLE_USER"/> 
     <security:logout/> 
     <security:custom-filter position="OPENID_FILTER" ref="openIdAuthFilter"/> 
    </security:http> 

    <bean id="openIdAuthFilter" class="org.springframework.security.openid.OpenIDAuthenticationFilter"> 
    <property name="authenticationManager" ref="authenticationManager"/> 
    <property name="authenticationFailureHandler"> 
     <bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> 
     <property name="defaultFailureUrl" 
      value="/myapp/auth/login?login_error=true"/> 
     </bean> 
    </property> 

    <property name="consumer"> 
     <bean class="org.springframework.security.openid.OpenID4JavaConsumer"> 
     <constructor-arg index="0"> 
      <bean class="org.openid4java.consumer.ConsumerManager"/> 
     </constructor-arg> 
     <constructor-arg index="1"> 
      <list value-type="org.springframework.security.openid.OpenIDAttribute"> 
      <bean class="org.springframework.security.openid.OpenIDAttribute"> 
       <constructor-arg index="0" value="email"/> 
       <constructor-arg index="1" value="http://schema.openid.net/contact/email"/>    
      </bean> 
      <bean class="org.springframework.security.openid.OpenIDAttribute"> 
       <constructor-arg index="0" value="firstName"/> 
       <constructor-arg index="1" value="http://axschema.org/namePerson/first" /> 
      </bean> 
      <bean class="org.springframework.security.openid.OpenIDAttribute"> 
       <constructor-arg index="0" value="lastName"/> 
       <constructor-arg index="1" value="http://axschema.org/namePerson/last" /> 
      </bean> 
      </list> 
     </constructor-arg> 
     </bean> 
    </property> 
    </bean> 


    <security:authentication-manager alias="authenticationManager"> 
     <security:authentication-provider ref="openIdAuthProvider"/> 
    </security:authentication-manager> 
    <bean id="openIdAuthProvider" class="org.springframework.security.openid.OpenIDAuthenticationProvider"> 
    <property name="authenticationUserDetailsService" ref="registeringUserService"/> 
    </bean> 

<!-- 
    A custom UserDetailsService which will allow any user to authenticate and "register" their IDs in an internal map 
    for use if they return to the site. This is the most common usage pattern for sites which use OpenID. 
--> 
    <bean id="registeringUserService" class="org.myapp.openid.service.CustomUserDetailsService" /> 

    <bean id="entryPoint" 
    class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> 
    <property name="loginFormUrl" value="/myapp/auth/login"/> 
    </bean> 
</beans> 

的org.myapp.openid.service.CustomUserDetailsS​​ervice.java

 
public class CustomUserDetailsService implements UserDetailsService, AuthenticationUserDetailsService { 
    private final Map registeredUsers = new HashMap(); 
    private static final List DEFAULT_AUTHORITIES = AuthorityUtils.createAuthorityList("ROLE_USER"); 

    public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException { 
     UserDetails user = registeredUsers.get(id); 

     if (user == null) { 
      throw new UsernameNotFoundException(id); 
     } 

     return user; 
    } 

    /** 
    * Implementation of {@code AuthenticationUserDetailsService} which allows full access to the submitted 
    * {@code Authentication} object. Used by the OpenIDAuthenticationProvider. 
    */ 
    public UserDetails loadUserDetails(OpenIDAuthenticationToken token) { 
     String id = token.getIdentityUrl(); 
     CustomUserDetails user = registeredUsers.get(id); 

     if (user != null) { 
      return user; 
     } 

     String email = null; 
     String firstName = null; 
     String lastName = null; 
     String fullName = null; 

     List attributes = token.getAttributes(); 

     for (OpenIDAttribute attribute : attributes) { 
      if (attribute.getName().equals("email")) { 
       email = attribute.getValues().get(0); 
      } 

      if (attribute.getName().equals("firstname")) { 
       firstName = attribute.getValues().get(0); 
      } 

      if (attribute.getName().equals("lastname")) { 
       lastName = attribute.getValues().get(0); 
      } 

      if (attribute.getName().equals("fullname")) { 
       fullName = attribute.getValues().get(0); 
      } 
     } 

     if (fullName == null) { 
      StringBuilder fullNameBldr = new StringBuilder(); 

      if (firstName != null) { 
       fullNameBldr.append(firstName); 
      } 

      if (lastName != null) { 
       fullNameBldr.append(" ").append(lastName); 
      } 
      fullName = fullNameBldr.toString(); 
     } 
     .... 
    } 
} 
当我调试,方法loadUserByUsername()

,有一个地址返回,但OpenIDAttributes为空(电子邮件,名字和姓氏)。

我想我必须配置spring-security.xml错误,请帮忙。由于

回答

2

只是要属性requierd:

`<beans:bean class="org.springframework.security.openid.OpenIDAttribute"> 
        <beans:constructor-arg index="0" value="email" /> 
        <beans:constructor-arg index="1" value="http://schema.openid.net/contact/email" /> 
        <beans:property name="required" value="true"/> 
</beans:bean>`