使用 spring security ldap 禁用基本身份验证
Posted
技术标签:
【中文标题】使用 spring security ldap 禁用基本身份验证【英文标题】:Disabling basic authentication with spring security ldap 【发布时间】:2013-02-11 20:30:46 【问题描述】:我正在尝试使用登录表单设置基本的 spring-security-ldap 身份验证,但是当我尝试登录时,我仍然得到一个 http-basic 弹出窗口,它不允许登录。
我的security.xml:
<s:http>
<s:intercept-url pattern="/login*" access="ROLE_ANONYMOUS" />
<s:intercept-url pattern="/**/*.html*" access="ROLE_ADMIN,ROLE_USER,ROLE_READONLY" />
<s:form-login login-page="/login.jsp"
authentication-failure-url="/login.jsp?error=true"
login-processing-url="/j_security_check"
default-target-url="/mainMenu.html"
always-use-default-target="true" />
<s:logout />
</s:http>
<s:ldap-server url="$ldap.url"
manager-dn="$ldap.adm_username"
manager-password="$ldap.adm_password"/>
<s:authentication-manager>
<s:ldap-authentication-provider user-search-filter="(cn=0)"
user-search-base="$ldap.user_search_base"
group-search-base="ou=myapp,ou=mysystem,o=ACME"
role-prefix="none"/>
</s:authentication-manager>
来自 web.xml:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<!-- First filter-mapping in file -->
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这是重构工作的一部分,与重构前的部署版本相比,url (server):(port)/(webapproot)/j_security_check
不可用,但重构后可用,并提示基本登录框,其中也是尝试通过登录页面登录时弹出的内容。
当尝试通过登录框登录时,我在服务器日志中得到了这个堆栈跟踪:
[#|2013-02-26T12:41:30.411+0100|WARNING|glassfish3.1|javax.enterprise.system.container.web.com.sun.web.security|_ThreadID=333;_ThreadName=Thread-1;|Exception
com.sun.enterprise.security.auth.login.common.LoginException: Login failed: Failed file login for .
at com.sun.enterprise.security.auth.login.LoginContextDriver.doPasswordLogin(LoginContextDriver.java:394)
at com.sun.enterprise.security.auth.login.LoginContextDriver.login(LoginContextDriver.java:240)
at com.sun.enterprise.security.auth.login.LoginContextDriver.login(LoginContextDriver.java:153)
at com.sun.web.security.RealmAdapter.authenticate(RealmAdapter.java:483)
at com.sun.web.security.RealmAdapter.authenticate(RealmAdapter.java:425)
at org.apache.catalina.authenticator.BasicAuthenticator.authenticate(BasicAuthenticator.java:176)
at org.apache.catalina.authenticator.AuthenticatorBase.processSecurityCheck(AuthenticatorBase.java:909)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:487)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:623)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.security.auth.login.LoginException: Failed file login for .
at com.sun.enterprise.security.auth.login.FileLoginModule.authenticate(FileLoginModule.java:84)
at com.sun.enterprise.security.auth.login.PasswordLoginModule.authenticateUser(PasswordLoginModule.java:117)
at com.sun.appserv.security.AppservPasswordLoginModule.login(AppservPasswordLoginModule.java:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.security.auth.login.LoginContext.invoke(LoginContext.java:784)
at javax.security.auth.login.LoginContext.access$000(LoginContext.java:203)
at javax.security.auth.login.LoginContext$4.run(LoginContext.java:698)
at javax.security.auth.login.LoginContext$4.run(LoginContext.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:695)
at javax.security.auth.login.LoginContext.login(LoginContext.java:594)
at com.sun.enterprise.security.auth.login.LoginContextDriver.doPasswordLogin(LoginContextDriver.java:382)
... 29 more
|#]
如何禁用基本登录,为了对 ldap-server 进行身份验证,我缺少什么?
更新:
我将 glassfish 安全领域更改为 ldap-realm,并将其设置为在旧 glassfish 服务器上。另外,我禁用了 glassfish 中的“安全管理器”。现在,我仍然得到额外的弹出登录框,但堆栈跟踪看起来更好:
[#|2013-02-26T13:47:49.640+0100|WARNING|glassfish3.1|javax.enterprise.system.container.web.com.sun.web.security|_ThreadID=90;_ThreadName=Thread-1;|Exception
com.sun.enterprise.security.auth.login.common.LoginException: Login failed: Access denied on empty password for user .
at com.sun.enterprise.security.auth.login.LoginContextDriver.doPasswordLogin(LoginContextDriver.java:394)
at com.sun.enterprise.security.auth.login.LoginContextDriver.login(LoginContextDriver.java:240)
at com.sun.enterprise.security.auth.login.LoginContextDriver.login(LoginContextDriver.java:153)
at com.sun.web.security.RealmAdapter.authenticate(RealmAdapter.java:483)
at com.sun.web.security.RealmAdapter.authenticate(RealmAdapter.java:425)
at org.apache.catalina.authenticator.BasicAuthenticator.authenticate(BasicAuthenticator.java:176)
at org.apache.catalina.authenticator.AuthenticatorBase.processSecurityCheck(AuthenticatorBase.java:909)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:487)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:623)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.security.auth.login.LoginException: Access denied on empty password for user .
at com.sun.enterprise.security.auth.login.LDAPLoginModule.authenticate(LDAPLoginModule.java:102)
at com.sun.enterprise.security.auth.login.PasswordLoginModule.authenticateUser(PasswordLoginModule.java:117)
at com.sun.appserv.security.AppservPasswordLoginModule.login(AppservPasswordLoginModule.java:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.security.auth.login.LoginContext.invoke(LoginContext.java:784)
at javax.security.auth.login.LoginContext.access$000(LoginContext.java:203)
at javax.security.auth.login.LoginContext$4.run(LoginContext.java:698)
at javax.security.auth.login.LoginContext$4.run(LoginContext.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:695)
at javax.security.auth.login.LoginContext.login(LoginContext.java:594)
at com.sun.enterprise.security.auth.login.LoginContextDriver.doPasswordLogin(LoginContextDriver.java:382)
... 29 more
|#]
但是我如何一起禁用 glassfish 的安全机制,只依赖 spring 呢?
更新 #2:找到罪魁祸首。我在 glassfish 安装中的 default-web.xml 中有这个:
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
删除它,并且没有更多的登录框:) 然后转到下一个问题:p
【问题讨论】:
【参考方案1】:Spring Security 不处理您的基本身份验证。查看堆栈跟踪。没有 Spring Security 过滤器。看起来您的基本身份验证是由 Glassfish 本身处理的。尝试通过 Glassfish 禁用基本身份验证。您在 web.xml 中有 login-config 和 security-constraint 标签吗?如果是真的,那么只需删除它们。
【讨论】:
感谢您的回复,我正在研究 glassfish atm(重构还包括迁移到更新的 glassfish 版本),并且在旧服务器中,我看到针对我的 ldap 设置了一个安全领域-server,它不在新服务器的设置中。我会尝试添加它,看看会发生什么。 web.xml 中有 login-config 和 security-constraint 标签吗? 不在我项目的web.xml中,没有。我必须说我发现很难将 glassfish 的安全领域和配置了 Spring 的安全领域分开,并且尝试从一个非常混乱的项目中学习并不能让它变得更容易。目前我不确定是 spring 设置还是 glassfish 设置提供了身份验证设置.. 我很确定它不是 Spring Security。在 org.springframework.web.filter.DelegatingFilterProxy.doFilter(...) 方法中创建一个刹车点。它是所有 Spring Security 魔法的入口点。我确信它不会被调用。 我想我可能已经找到了(其中一个)问题。在新的 Glassfish 服务器中,启用了“安全管理器”,而在旧版本中则没有。现在尝试在新服务器上禁用它,希望我在尝试登录时不会得到登录框。以上是关于使用 spring security ldap 禁用基本身份验证的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 spring-security-core-ldap 插件在 grails 中实现 LDAP 身份验证?
如何使用带有 LDAP 的 Spring Security 获取用户信息
使用 LDAP 的 Spring Security - 登录后出错
使用 Spring Security 3 进行 LDAP 身份验证