猿问

搭建security嵌入式LDAP报错


搭建spring security 的嵌入式LDAP报错如下,提示缺少directoryserivce的类,但是我找了很久,试了pom.ml很多依赖还是没能成功,我的框架是spring+springmvc+security+LDAP

Context initialization failed java.lang.NoClassDefFoundError:org/apache/directory/server/core/DirectoryService

pom.xml相关配置如下

<dependency>
            <groupId>org.apache.geronimo.plugins</groupId>
            <artifactId>directory</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.server</groupId>
            <artifactId>apacheds-service</artifactId>
            <version>2.0.0-M21</version>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.server</groupId>
            <artifactId>apacheds-core</artifactId>
            <version>1.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.server</groupId>
            <artifactId>apacheds-server-jndi</artifactId>
            <version>1.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.server</groupId>
            <artifactId>apacheds-all</artifactId>
            <version>1.5.5</version>
        </dependency>
        <dependency>
            <!-- Required by ApacheDS, but not listed in its POM -->
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>

我的security.xml配置如下,因为是跟着书里的教程做的,里面还有一些其他的配置

?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:security="http://www.springframework.org/schema/security"
       xmlns:="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
        >
    <security:debug />
    <!--对Controller方法拦截的Security框架的配置-->
    <security:global-method-security jsr250-annotations="enabled" secured-annotations="enabled" pre-post-annotations="enabled"/>
    <security:http pattern="/login" security="none" />
    <security:http pattern="/home" security="none"/>
    <security:http pattern="/" security="none" />
    <!--<security:http pattern="/login.do" security="none" />-->
    <security:http pattern="/static/css/favicon.ico" security="none"/>
    <security:http auto-config="true" use-expressions="true" >
        <security:access-denied-handler error-page="/accessDenied.do"/>
        <security:session-management session-fixation-protection="migrateSession">
            <security:concurrency-control max-sessions="10" expired-url= "/login.do?error=expired"/>
        </security:session-management>
        <!--<security:intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />-->
        <security:intercept-url pattern="/login.do" access="permitAll" requires-channel="https" />
        <security:intercept-url pattern="/**" access="hasRole('ROLE_ADMIN') or hasRole('ROLE_USER')" requires-channel="https"/>
        <security:remember-me key="jbcpPetStore" services-ref="ipTokenBasedRememberMeServicesBean"/>
        <security:form-login
                login-page="/login.do"
                username-parameter="username"
                password-parameter="password"
                default-target-url="/header"
                />
        <security:custom-filter ref="requestHeaderFilter" before="FORM_LOGIN_FILTER"/>
        <security:remember-me key="jbcpPetStore" token-validity-seconds="360" user-service-ref="myUserDetailService"/>
        <security:logout invalidate-session="true" logout-success-url="/" logout-url="/logout"/>
        <security:custom-filter ref="ipFilter" before="FILTER_SECURITY_INTERCEPTOR"/>
    </security:http>

    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider ref="signedRequestAuthenticationProvider"/>
        <security:authentication-provider user-service-ref="myUserDetailService">
            <security:password-encoder ref="BCryptEncoder"/>
        </security:authentication-provider>
        <security:ldap-authentication-provider server-ref="ldapLocal" user-search-filter="(uid={0})" group-search-base="ou=Groups"/>
    </security:authentication-manager>

    <security:ldap-server id="ldapLocal" ldif="classpath:JBCPPets.ldif"  root="dc=jbcppets,dc=com"/>

    <bean id="ipTokenBasedRememberMeServicesBean" class="com.ssm.security.IPTokenBasedRememberMeServices" >
        <property name="key" value="jbcpPetStore"/>
        <property name="userDetailsService" ref="myUserDetailService"/>
        <!-- To experiment with changing the checkbox name and cookie name 注意要与jsp页面的名字匹配-->
<!--        <property name="parameter"><value>_remember_me</value></property>
        <property name="cookieName"><value>REMEMBER_ME</value></property>-->
    </bean>

    <bean id="authenticationFilter" class= "org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
        <property name ="authenticationManager" ref="authenticationManager"/>
    </bean>


    <bean id="myUserDetailService" class="com.ssm.security.MyUserDetailService" >
    </bean>

    <bean id="BCryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />

    <bean id="ipFilter" class="com.ssm.security.IPRoleAuthenticationFilter">
        <property name="targetRole" value="ROLE_ADMIN"/>
        <property name="allowedIPAddresses">
            <list>
                <value>0:0:0:0:0:0:0:1</value>
            </list>
        </property>
    </bean>

    <bean id="signedRequestAuthenticationProvider" class="com.ssm.security.SignedUsernamePasswordAuthenticationProvider">
        <property name="passwordEncoder" ref="BCryptEncoder"/>
        <property name="userDetailsService" ref="myUserDetailService"/>
    </bean>
    <bean id="requestHeaderFilter" class="com.ssm.security.RequestHeaderProcessingFilter">
        <property name="authenticationManager" ref="authenticationManager"/>
    </bean>
</beans>

以下是我照做的例子

 配置 LDAP 服务器引用

第一步是在 dogstore-security.xml 中声明嵌入式 LDAP 服务器的引用。LDAP 服务器的声明在<http>元素之外,与<authentication-manager>相同的等级:
<ldap-server ldif="classpath:JBCPPets.ldif" id="ldapLocal" 
root="dc=jb cppets,dc=com"/>
我们从 classpath 中加载 JBCPPets.ldif,并用其为 LDAP 服务器插入数据。这意味着(如
同嵌入式 HSQL 数据库启动那样)我们应该在 WEB-INF/classes 放置 JBCPPets.ldif 文件。root属性用特定的 DN 声明了LDAP 目录的根。这应该与我们使用的 LDIF 文件逻辑根 DN 相对应。
【注意,对于嵌入式的LDAP 服务器,root 是必须的,尽管 XML 模式并没有这样声明。如果它没有指明或指明错误,你会在 Apache DS server 启动的时候看待几个奇怪的错误。】
当我们在 Spring Security 配置文件中声明 LDAP 用户服务和其它配置元素时,会重用这里定义的 bean ID。对于嵌入式的 LDAP 模式来说,<ldap-server>声明的其它属性都是可选的。
启用 LDAP AuthenticationProvider
接下来,我们要配置另一个 AuthenticationProvider,它用 LDAP 来检查用户凭证。简单得添加另一个 AuthenticationProvider 即可,如下:
<authentication-manager alias="authenticationManager">
<!-- Other authentication providers are here -->
<ldap-authentication-provider server-ref="ldapLocal" user-search-filter="(uid={0})"
group-search-base="ou=Groups"
/>
</authentication-manager>

我们稍后将会介绍这些属性——现在,回到应用并运行,使用用户名 ldapguest 和密码
password 进行登录。你应该能够登录进去了!


慕粉2120347094
浏览 1550回答 0
0回答
随时随地看视频慕课网APP

相关分类

Java
我要回答