2012-03-20 18 views
0

Spring MVCプロジェクトで認証されたLDAPでSpring Security 3を使用しています。 私のプロジェクトを他の環境に展開し、JDKのバージョンを1.6から1.7に変更する必要があるまではうまく動作します。JDK 1.7ではSpringセキュリティ3が動作しません

以下

は私の春のセキュリティ設定ファイルとサンプルコードです:成功ログオン後)

1)セキュリティ・アプリケーションのcontext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans 
    xmlns:s="http://www.springframework.org/schema/security" 
    xmlns="http://www.springframework.org/schema/beans" 
    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-3.0.xsd 
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <s:http use-expressions="true">  
     <s:intercept-url pattern="/auth/**" access="permitAll" /> 
     <s:intercept-url pattern="/css/**" access="permitAll" /> 
     <s:intercept-url pattern="/image/**" access="permitAll" /> 
     <s:intercept-url pattern="/scripts/**" access="permitAll" />   

     <s:intercept-url pattern="/**" access="hasRole('GENERAL_USER')" /> 

     <s:form-login login-page="/auth/login.html" 
         default-target-url="/welcome.html" 
         authentication-failure-url="/auth/login.html?error=1" /> 

     <s:access-denied-handler error-page="/auth/denied.html"/> 

     <s:logout invalidate-session="true" logout-success-url="/auth/logoutSuccess.html"/>       
    </s:http> 

    <s:authentication-manager> 
     <s:authentication-provider ref="ldapAuthProvider" /> 
    </s:authentication-manager> 

    <bean 
     id="contextSource" 
     class="org.springframework.security.ldap.DefaultSpringSecurityContextSource" 
     scope="singleton"> 
     <constructor-arg 
      value="ldap://ldapurl:389/dc=o,dc=a" /> 
      <property name="userDn" value="cn=xxx,cn=users,dc=o,dc=a" /> 
      <property name="password" value="password" /> 
      <property name="baseEnvironmentProperties"> 
       <map> 
        <entry key="java.naming.referral"> 
         <value>follow</value> 
        </entry>      
       </map> 
      </property>   
    </bean> 

    <bean id="userSearch" 
     class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch"> 
     <!-- searchBase, searchFilter, contextSource --> 
     <constructor-arg index="0" value="" /> 
     <constructor-arg index="1" value="(sAMAccountName={0})" /> 
     <constructor-arg index="2" ref="contextSource" /> 
    </bean> 

    <bean id="ldapAuthProvider" 
     class="com.foo.auth.MyLdapAuthenticationProvider">  
     <constructor-arg>  
      <bean  
       class="com.foo.auth.MyLdapAuthenticator"> 
       <constructor-arg ref="contextSource" /> 
       <property name="userSearch"> 
        <ref bean="userSearch" /> 
       </property>    
      </bean> 
     </constructor-arg> 
     <property name="authoritiesPopulator" ref="authoritiesPopulator" /> 
     <property name="userDetailsContextMapper" ref="userDetailsMapper" /> 
    </bean> 

    <bean id="authoritiesPopulator" class="com.foo.auth.MyLdapAuthoritiesPopulator"> 
     <constructor-arg ref="userService" />  
    </bean>  

    <bean id="userService" class="com.foo.auth.MyLdapUserDetailsService"> 
     <constructor-arg ref="userSearch" /> 
     <property name="userDetailsMapper" ref="userDetailsMapper" /> 
    </bean> 
    <bean id="userDetailsMapper" class="com.foo.auth.MyUserDetailsContextMapper">      
    </bean>   
</beans> 

2に、のwelcome.jspにURLをリダイレクトしますwelcome.jsp、私は春のセキュリティtaglibを使用してログオンユーザーのフルネームを取得します。 (テストのために、私は、全体のコンテキスト情報を表示するために主要な使用):

<security:authentication property="principal"></security:authentication>  

際に使用JDK 1.6、主要なショー:

[email protected]:......... 

と私は私のカスタムなprincipal.fullNameようUserDetailの属性を使用することができます。 使用JDK 1.7は、主要なショーとき:

​​

それは私のカスタムUserDetailオブジェクトを取得できません。 JDKk1.7を使用すると、Springコンテキストを正しく取得できません。

この問題は、根本的な原因は、JDKのバージョンの問題で分かったことはほぼ1週間私を取る;-(

LDAPと春のセキュリティはJDK1.7では動作しません?または私は何かのを欠場理由を誰もが知っていますコンフィグ?

は、事前にあなたに感謝!

答えて

0

問題を修正しました。 私MyLdapAuthenticationProviderが間違っプロバイダを拡張するので、それがある。 私はLdapAuthenticationProvider、 にMyLdapAuthenticationProvider拡張クラスを変更し、春のセキュリティは作業fとJDK 1.6と1.7の両方で使用されています。ここで

は私のカスタムLdapAuthenticationProviderです:

public class MyLdapAuthenticationProvider extends LdapAuthenticationProvider { 

private static Logger logger = Logger.getLogger(MyLdapAuthenticationProvider.class);   
private MyLdapAuthenticator authenticator; 
@Autowired 
private MyLdapAuthoritiesPopulator authoritiesPopulator; 
@Autowired 
private MyUserDetailsContextMapper userDetailsContextMapper; 

public MyLdapAuthenticationProvider(LdapAuthenticator authenticator) { 
    super(authenticator); 
    this.authenticator = (MyLdapAuthenticator) authenticator; 
} 

@Override 
protected DirContextOperations doAuthentication(UsernamePasswordAuthenticationToken userToken) { 
    try { 
     DirContextOperations dirCtx = getAuthenticator().authenticate(userToken);    
     return dirCtx; 
    } catch (PasswordPolicyException ppe) { 
     throw new LockedException(this.messages.getMessage(ppe.getStatus().getErrorCode(), ppe.getStatus() 
       .getDefaultMessage())); 
    } catch (UsernameNotFoundException notFound) {    
     throw new BadCredentialsException("User Name Error!"); 
    } catch (NamingException ldapAccessFailure) { 
     throw new AuthenticationServiceException(ldapAccessFailure.getMessage(), ldapAccessFailure); 
    } 
} 

private void setAuthenticator(MyLdapAuthenticator authenticator) { 
    Assert.notNull(authenticator, "An LdapAuthenticator must be supplied"); 
    this.authenticator = authenticator; 
} 

private MyLdapAuthenticator getAuthenticator() { 
    return authenticator; 
} 

public MyUserDetailsContextMapper getUserDetailsContextMapper() { 
    return userDetailsContextMapper; 
} 

public void setUserDetailsContextMapper(MyUserDetailsContextMapper userDetailsContextMapper) { 
    this.userDetailsContextMapper = userDetailsContextMapper; 
} 

public void setAuthoritiesPopulator(MyLdapAuthoritiesPopulator authoritiesPopulator) { 
    this.authoritiesPopulator = authoritiesPopulator; 
} 

public MyLdapAuthoritiesPopulator getAuthoritiesPopulator() { 
    return authoritiesPopulator; 
} 

}