gpt4 book ai didi

java - 无需身份验证的Spring安全授权

转载 作者:行者123 更新时间:2023-11-30 09:09:34 24 4
gpt4 key购买 nike

我有一个 Java JSF 2、Spring 3、Hibernate 4 Java EE 应用程序,它使用第三方库对用户进行身份验证。我将所需的 CA 证书导入我的 JVM,将第三个库添加到项目并在 web.xml 中进行配置。图书馆从智能卡中读取用户的详细信息。整个设置工作正常,用户被第三方库带到主页。

这是我对保护应用程序的要求。

  • 再检查一次用户是否存在于应用程序特定的数据库中
  • 从应用程序数据库中获取该用户的角色
  • 保护我的 JSF 页面
  • 保护我的应用程序服务

我查看了此链接,似乎“AuthenticationProcessingFilter”已弃用且不适用于 Spring 3!

http://codersatwork.wordpress.com/2010/02/13/use-spring-security-for-authorization-only-not-for-authentication/

我也看过这个,但我不明白还需要哪些其他步骤/配置。

spring-security: authorization without authentication

如果有人能概述我只需要授权实现 Spring Security 所需的所有项目,我将不胜感激。这是我想出的。

1) 使用 spring 3 security 更新 pom,添加一个过滤器(我应该选择哪个过滤器)

2) 自定义用户详细信息

3) 自定义 DaoAuthenticationProvider

4) 在 application-context.xml 中注册此自定义身份验证提供程序

5) 注册访问决策管理器进行授权

最佳答案

适合此用例的基本 Spring Security 类是 org.springframework.security.web.authentication.preauth.Abs​​tractPreAuthenticatedProcessingFilter 和 org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider。

如果您当前的身份验证库导致用户以标准 Java EE 方式进行身份验证(即调用 HttpServletRequest 实例上的 getUserPrincipal() 返回经过身份验证的用户的委托(delegate)人),您需要做的事情应该类似于:

  1. 实现接口(interface) org.springframework.security.core.userdetails.UserDetailsS​​ervice,它检查用户是否存在于您的应用程序数据库中,如果不存在则抛出 UsernameNotFoundException
  2. 为 Spring Security 添加以下设置:

    <!-- Declare the user details for database check -->
    <bean id="userDetails" class="com.yourpackage.DatabaseUserDetails"/>

    <!-- Default empty auth manager -->
    <security:authentication-manager alias="authenticationManager"/>

    <!-- Use default settings from the jee namespace -->
    <security:http>
    <security:jee mappable-roles="IS_AUTHENTICATED_FULLY" user-service-ref="userDetails"/>
    </security:http>
  3. 配置您的 Spring Security 以根据您的要求执行授权

security:jee 初始化过滤器和身份验证提供程序,并将您的用户服务插入提供程序。

如果您当前的身份验证库不使用 Java EE 机制,您将需要实现自己的 AbstractPreAuthenticatedProcessingFilter 子类,它知道如何识别用户已通过身份验证。

然后您将用您自己的过滤器替换默认的预授权过滤器,因此配置如下所示:

<!-- Declare the user details for database check -->
<bean id="userDetails" class="com.yourpackage.DatabaseUserDetails"/>

<!-- Define provider -->
<bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
<property name="preAuthenticatedUserDetailsService">
<bean id="userDetailsServiceWrapper"
class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
<property name="userDetailsService" ref="userDetails"/>
</bean>
</property>
</bean>

<!-- Define alias for the authentication manager -->
<authentication-manager alias="authenticationManager">
<security:authentication-provider ref="preauthAuthProvider" />
</authentication-manager>

<!-- Declare the custom filter -->
<bean id="authenticationFilter" class="com.yourpackage.AuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager"/>
</bean>

<security:http>
<security:custom-filter ref="authenticationFilter" position="PRE_AUTH_FILTER"/>
</security:http>

您可以在 Spring Security documentation 中找到更多详细信息.

关于java - 无需身份验证的Spring安全授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22993491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com