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

2024-04-08

我希望我的 Spring 应用程序尝试两种预身份验证方法(站点管理员 and Java EE 容器身份验证).

  1. 如果这些过滤器中的任何一个找到用户名 - 我想根据我的用户数据库检查该用户名,并根据我在数据库中看到的内容分配角色。 (我有一个 AuthenticationUserDetailsS​​ervice 的实现,它为我做到了这一点。)
  2. 如果没有 - 向用户显示登录页面。根据我的用户数据库检查他们在表单中输入的凭据。

Siteminder 集成正在运行。登录表单也可以正常工作。我的问题是 Java EE 预身份验证。它永远不会发挥作用。

我的 applicationContext-security.xml:

<!-- 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”身份登录。 (我的用户数据库中有一个使用此用户名的用户。)但是当我访问应用程序时,永远不会调用“customAuthenticationUserDetailsS​​ervice”bean 来验证用户名。我知道这一点,因为“customAuthenticationUserDetailsS​​ervice”进行了大量的日志记录,清楚地显示了它正在做什么。当我使用 Siteminder 预身份验证时 - 'customAuthenticationUserDetailsS​​ervice' 工作得很好,我在日志中得到一些跟踪输出。但不适用于 J2EE 身份验证...

我的猜测是其中一件事情正在发生:

a) Java EE 预身份验证过滤器未定位用户名,因此它从不调用身份验证管理器

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

顺便说一句,使用“customUserDetailsS​​ervice”的默认身份验证提供程序也不会启动。同样,我可以这么说,因为日志中没有“customUserDetailsS​​ervice”的输出。

您能建议一下这里可能存在什么问题吗?如果不是解决方案,那么我们将不胜感激有关如何解决此问题的建议。


好吧,我明白了。问题是,即使我在 Websphere 中进行了 J2EE 安全设​​置并且已通过身份验证,我的 web.xml 仍包含无安全限制。因此,Websphere 没有为我的请求提供主体。这显然是一个有意为之的功能。如果您不访问受保护的 URL,则不需要预身份验证信息。

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

就是这个:

<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 在请求中填写用户主体信息。

感谢@Ralph 对这个问题的评论:request.getUserPrincipal() 得到 null https://stackoverflow.com/questions/7795012/request-getuserprincipal-got-null

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 J2eePreAuthenticatedProcessingFilter 和自定义身份验证提供程序? 的相关文章

随机推荐