我有一个 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/ http://codersatwork.wordpress.com/2010/02/13/use-spring-security-for-authorization-only-not-for-authentication/
我也看了这个,但我不明白还需要什么其他步骤/配置。
spring-security:无需身份验证的授权 https://stackoverflow.com/questions/1322135/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.AbstractPreAuthenticatedProcessingFilter 和 org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider。
如果您当前的身份验证库导致用户以标准 Java EE 方式进行身份验证(即在 HttpServletRequest 实例上调用 getUserPrincipal() 返回经过身份验证的用户的主体),您需要做的事情应该类似于:
- 实现接口 org.springframework.security.core.userdetails.UserDetailsService ,它检查用户是否存在于应用程序数据库中,如果不存在则抛出 UsernameNotFoundException
-
为 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>
配置 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安全文档 http://docs.spring.io/spring-security/site/docs/3.1.x/reference/preauth.html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)