首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用J2eePreAuthenticatedProcessingFilter和自定义身份验证提供程序?

如何使用J2eePreAuthenticatedProcessingFilter和自定义身份验证提供程序?
EN

Stack Overflow用户
提问于 2012-03-22 22:07:42
回答 1查看 8.6K关注 0票数 6

我希望我的Spring应用程序尝试两种预身份验证方法(Siteminder和Java容器身份验证)。

如果这些过滤器中的任何一个找到用户名,则

  1. --我希望根据我在数据库中看到的内容检查该用户名和分配角色。(我有一个AuthenticationUserDetailsService的实现,它是为我实现的)
  2. (如果没有)--向用户显示一个登录页面。检查他们在表单中输入的凭据与我的用户数据库。

Siteminder的整合正在起作用。登录表单也在工作。我的问题是Java预认证。它从来不起作用。

我的应用程序security.xml security.xml:

代码语言:javascript
复制
<!-- HTTP security configurations -->
<sec:http auto-config="true" use-expressions="true">
    <sec:form-login login-processing-url="/resources/j_spring_security_check" always-use-default-target="true" default-target-url="/" login-page="/login"
        authentication-failure-url="/login?login_error=t" />
    <sec:logout logout-url="/resources/j_spring_security_logout" />
    <sec:access-denied-handler error-page="/accessDenied" />
    <sec:remember-me user-service-ref="customUserDetailsService" token-validity-seconds="86400" key="OptiVLM-VaultBalance" />
    <sec:custom-filter position="PRE_AUTH_FILTER" ref="siteminderFilter"/>
    <sec:custom-filter after="PRE_AUTH_FILTER" ref="jeePreAuthenticatedFilter"/>

    <!-- various intercept-url elements here, skipped for brevity -->
</sec:http>

<!-- Authentication Manager -->
<sec:authentication-manager alias="authenticationManager">
    <!-- J2EE container pre-authentication or Siteminder -->
    <sec:authentication-provider ref="customPreAuthenticatedAuthenticationProvider" />
    <!-- Default provider -->
    <sec:authentication-provider user-service-ref="customUserDetailsService" />
</sec:authentication-manager>

<!-- Siteminder pre-authentication -->
<bean id="siteminderFilter" class="org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter">
    <property name="principalRequestHeader" value="SM_USER" />
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="exceptionIfHeaderMissing" value="false" />
</bean>

<!-- J2EE pre-authentication -->
<bean id="jeePreAuthenticatedFilter" class="org.springframework.security.web.authentication.preauth.j2ee.J2eePreAuthenticatedProcessingFilter">
    <property name="authenticationManager" ref="authenticationManager" />
</bean>

<!-- Custom pre-authentication provider -->
<bean id="customPreAuthenticatedAuthenticationProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
    <property name="preAuthenticatedUserDetailsService" ref="customAuthenticationUserDetailsService" />
</bean>

我在Websphere中启用了Java 2安全性,并以'admin5‘登录。(我的用户数据库中有一个具有这个用户名的用户。)但是,当我访问应用程序时,从来没有调用'customAuthenticationUserDetailsService‘bean来验证用户名。我知道这一点,因为'customAuthenticationUserDetailsService‘做了大量的日志记录,这清楚地显示了它正在做什么。当我使用Siteminder预认证- 'customAuthenticationUserDetailsService‘工作很好时,我会在日志中得到一些跟踪输出。但不是为了J2EE认证..。

我猜其中一件事正在发生:

a) Java预身份验证筛选器没有定位用户名,因此它从不调用身份验证管理器。

( b) Java预身份验证筛选器工作正常,但由于某种原因,身份验证管理器从未调用过我的自定义身份验证提供程序。

顺便说一句,使用'customUserDetailsService‘的默认身份验证提供程序也不起作用。同样,我可以看出这一点,因为日志中没有来自'customUserDetailsService‘的输出。

,你能告诉我这里有什么问题吗?如果不是一个解决方案,那么关于如何处理这个问题的建议将是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-23 22:32:11

好吧,我想出来了。问题是,即使我在Websphere中设置了J2EE并通过了身份验证,但我的web.xml不包含,不包含安全约束。因此,Websphere没有为我的请求提供主体。这显然是故意的。如果您没有访问受保护的URL,则不应该需要预验证信息.

为了克服这一问题,我在我的web.xml中添加了一个安全约束,它允许所有用户访问资源。实际上,这些资源并没有得到保障,但是--现在还存在一个制约因素。

就是这样:

代码语言:javascript
复制
<security-constraint>
    <web-resource-collection>
        <web-resource-name>All areas</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>
</security-constraint>

这将使Websphere填充请求中的用户主体信息。

谢谢拉尔夫对这个问题的评论:request.getUserPrincipal() got null

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9831268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档