LDAP 的认证问题 Authentication
2013-08-13 11:11
155 查看
最近一直在ldap开发项目中 对ldap方方面面都接触到很多 之前很多都是基本的操作,增删改查,配事物,连接池,这些只要会用spring的人应该都可以很轻松的弄出来,其实spring-ldap的提供了很好的说明。接下来 说下认证的问题,这个是我今天才弄好的。
可以看到官网的文档上有个相关的认证的方法 在第10章,但是有一个问题,如果你没有按照文档去配置事物的话 他是可以认证通过的,如果你按照文档去配了事物 再去调用验证发放他会报异常 说你找不到 ContextSource 这是因为在第6章的时候 它把跟服务器连接的id改为contextSourceTarget
但是在LDAP中contextSource这个 不只是一个id那么简单 因为在LDAP的世界里 他有一个接口也叫ontextSource 是用来验证使用的 所以你在不配置事物的时候是可以用的。
接下来上两段代码,事物是与jpa集合好的:
这里要结束下为什么要配2个ldapTemplate, 主要原因是在在于class="org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy"spring的这里代理里面还不支持contextSource的验证(源码没做完很坑爹),所以单独写一个"ldapTemplateAuthention"用来单独做验证使用,"ldapTemplate"就做我们正常业务与事物。
下面是认证方法,官网拔下来的0.0:
本文出自 “记下就对了” 博客,请务必保留此出处http://jueshizhanhun.blog.51cto.com/4372226/1272001
可以看到官网的文档上有个相关的认证的方法 在第10章,但是有一个问题,如果你没有按照文档去配置事物的话 他是可以认证通过的,如果你按照文档去配了事物 再去调用验证发放他会报异常 说你找不到 ContextSource 这是因为在第6章的时候 它把跟服务器连接的id改为contextSourceTarget
但是在LDAP中contextSource这个 不只是一个id那么简单 因为在LDAP的世界里 他有一个接口也叫ontextSource 是用来验证使用的 所以你在不配置事物的时候是可以用的。
接下来上两段代码,事物是与jpa集合好的:
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> <property name="url" value="ldap://127.0.0.1:389" /> <property name="userDn" value="cn=Directory Manager " /> <property name="password" value="******" /> </bean> <bean id="contextSourceTarget" class="org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy"> <constructor-arg ref="contextSource" /> </bean> <bean id="ldapTemplateAuthention" class="org.springframework.ldap.core.LdapTemplate"> <constructor-arg ref="contextSource" /> </bean> <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> <constructor-arg ref="contextSourceTarget" /> </bean> <bean id="transactionManager" class="com.smarcloud.control.util.ContextSourceAndJpaTransactionManager"> <property name="contextSource" ref="contextSourceTarget"/> <property name="entityManagerFactory" ref="entityManagerFactory"/><!-- 这个是jpa的 --> </bean> <tx:annotation-driven transaction-manager="transactionManager" />改成这样就可以以确保你的认证方法是可用的了,同时还会有事物。
这里要结束下为什么要配2个ldapTemplate, 主要原因是在在于class="org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy"spring的这里代理里面还不支持contextSource的验证(源码没做完很坑爹),所以单独写一个"ldapTemplateAuthention"用来单独做验证使用,"ldapTemplate"就做我们正常业务与事物。
下面是认证方法,官网拔下来的0.0:
public boolean authentication(String uid){ CollectingAuthenticationErrorCallback errorCallback = new CollectingAuthenticationErrorCallback(); String filter = "(&(objectclass=inetOrgPerson)(uid=" + uid + "))"; boolean result = ldapTemplateAuthention.authenticate("", filter.toString(), "123456", errorCallback); if (!result) { Exception error = errorCallback.getError(); // error is likely of type org.springframework.ldap.AuthenticationException error.printStackTrace(); } return result; }OK,认证成功为true 不成功就是false 还带异常。
本文出自 “记下就对了” 博客,请务必保留此出处http://jueshizhanhun.blog.51cto.com/4372226/1272001
相关文章推荐
- 关于Basic,Digest and NTLM Authentication认证的问题
- Spring Security-Ldap认证(LdapAuthenticationProvider)
- Debugging SSH public key authentication problems-关于ssh公钥认证问题的排查
- Putty使用公钥认证时,报错:Disconnected: No supported authentication methods available(server sent:public key) 问题的解决
- 如何在RHEL6上配置PAM LDAP认证(How to setup LDAP Authentication for PAM on RHEL6 x86_64)
- 关于AD域在通过LDAP认证登录时密码修改后还可使用旧密码正常登录问题解决方法 推荐
- 【Shiro】调用doGetAuthenticationInfo进行认证成功之后,isAuthenticated是false的问题。
- cookie丢失问题(认证失效) Authentication (用户验证信息)也会丢失
- 【Shiro】Apache Shiro架构之身份认证(Authentication)
- 访问需要HTTP Basic Authentication认证的资源的各种语言的实现
- 启动TDS LDAP 服务器遇到的问题总结
- kerberos整合ldap进行服务器认证
- 认证 (authentication) 和授权 (authorization) 的区别
- [转]IE11下Forms身份认证无法保存Cookie的问题
- 问题集锦06:去除Windows身份认证带来的安全隐患
- 解决Cornerston的Authentication provider raised an exception (first credentials)问题
- Apache Shiro身份认证(Authentication)
- 各开发语言LDAP认证指南
- HTTP Basic 认证(Authentication)