标签:java ldap spring security 企业
<span style="font-size:18px;">先说一下Spring security 是基于spring的一个强大的安全验证模块,它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能。</span>
LDAP是轻量目录访问协议,基于tcp/ip协议,一般为企业的基本信息的访问提供一个统一的访问方式,它存储的数据是以树形结构存储的,因此,访问速度超快,但是相对的存储速度很慢。当然,你肯定也不能使用sql语句了
首先说一下所需要的jar包,当然也有maven配置,网上应该有很多
spring-security-config
spring-security-core
spring-security-ldap
spring-security-taglibs
spring-security-web
好吧,开始要先配置spring-security,由于本身就是基于spring的,配置起来也很简单
首先在web,xml中配置一个security的filter:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后在spring-mvc文件里配置一个bean
<bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
<constructor-arg>
<bean class="org.springframework.security.ldap.authentication.BindAuthenticator">
<constructor-arg ref="contextSource"/>
<property name="userSearch">
<bean id="userSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
<constructor-arg index="0" value=""/>
<constructor-arg index="1" value="(uid={0})"/>
<constructor-arg index="2" ref="contextSource"/>
</bean>
</property>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
<constructor-arg ref="contextSource"/>
<constructor-arg value="" />
<property name="defaultRole" value="ROLE_USER"/>
</bean>
</constructor-arg>
</bean>
同时需要配置ldap数据源:
<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="ldap://192.168.0.1:389/dc=gnetis,dc=com"/>
<property name="userDn" value="cn=Manager,dc=gnetis,dc=com" />
<property name="password" value="admin"/>
</bean>
好的,然后还有一个spring-security.xml需要创建并配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<!-- spring security -->
<http pattern="/login.jsp" security="none"/>
<http pattern="/resources/**" security="none"/>
<!-- 不启用安全验证 -->
<!-- <http pattern="/*" security="none"/> -->
<http auto-config=‘true‘>
<intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login login-page="/login.jsp" login-processing-url="/loginProcess"
authentication-failure-url="/login.jsp?login_error=1"
default-target-url="/home/index" always-use-default-target="true" />
<logout logout-success-url="/login.jsp" delete-cookies="JSESSIONID"/>
<!-- Uncomment to limit the number of sessions a user can have -->
<session-management invalid-session-url="/login.jsp">
<concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
</session-management>
</http>
<authentication-manager>
<authentication-provider ref="ldapAuthProvider"></authentication-provider>
</authentication-manager>
</beans:beans>
一定要注意 xsi:schemaLocation的url地址的填写,否则各种错误。
其中,login.jsp是默认进入页面,home/index是默认页面的路径,
然后将在spring-mvc里配置的bean配置在authentication-manager里面,记得要写login.jsp,如:
版权声明:本文为博主原创文章,未经博主允许不得转载。
Spring security 集成ldap服务,实现统一验证
标签:java ldap spring security 企业
原文地址:http://blog.csdn.net/liushuiwuyizhe/article/details/47750495