【发布时间】:2012-09-10 19:26:52
【问题描述】:
这是我的场景:
- 网络应用为许多应用执行某种 SSO
- 登录后的用户点击链接后,应用会向相应的应用发布用户信息(姓名、密码 [无用]、角色)
- 我正在其中一个应用程序上实现 SpringSecurity 以从其强大功能(会话中的权限、其类提供的方法等)中受益
所以,我需要开发一个 自定义过滤器 - 我猜 - 能够通过自定义 DetailsUserService 从请求中检索用户信息,从数据库中检索,进一步有关用户的信息(电子邮件等),然后根据从请求中检索到的角色对该用户执行身份验证。
我正在查看Pre-Authentication 过滤器,但我不确定它是否是正确的选择。当主体已经在会话中时,似乎应该使用这些对象,由一些先前的身份验证机制提出(对吗?)。
我认为,一旦确定了正确的过滤器,我应该需要执行以下操作:
GrantedAuthority[] ga= new GrantedAuthority[1];
ga[0] = new GrantedAuthorityImpl(myUser.getRole());
SecurityContext sc = SecurityContextHolder.getContext();
Authentication a = new UsernamePasswordAuthenticationToken(userName, userPwd, ga);
a = authenticationManager.authenticate(a);
sc.setAuthentication(a);
这是解决我的问题的正确方向吗?你有什么建议可以帮助我找到缺少的东西吗?
谢谢大家,
卢卡
补充:
嗨,Xearxess!很抱歉再次打扰您,但似乎根据 SpringSecurity 2.0.4 翻译您的代码比我想象的要困难:S 问题是 XML ......我尝试了不同的配置,但我总是遇到命名空间问题,缺少属性等等……
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
<security:http>
<security:intercept-url pattern="/**" access="isAuthenticated()" />
<security:logout logout-url="/logout" logout-success-url="http://milan-ias-vs.usersad.everis.int/DMTest/" invalidate-session="true" />
<security:custom-filter position="PRE_AUTH_FILTER" ref="preAuthenticatedProcessingFilter" />
</security:http>
<bean id="preAuthenticatedProcessingFilter" class="it.novartis.ram.authentication.PreAuthenticatedProcessingFilter">
<custom-filter position="PRE_AUTH_FILTER"/>
<property name="authenticationManager" ref="authenticationManager" />
</bean>
<bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
<property name="preAuthenticatedUserDetailsService">
<bean class="it.novartis.ram.authentication.PreAuthenticatedUserDetailsService" />
</property>
</bean>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="preauthAuthProvider" />
</security:authentication-manager>
</beans>
引用 CUSTOM-FILTER 元素的 2 行是两次不同的尝试,它们都被标记为错误。如何将过滤器的位置指定为属性?
身份验证管理器定义上的身份验证提供程序引用也被标记为错误。我想我也需要像属性一样指定它,对吧?
希望你能给我最后一击;) 再次感谢您,
卢卡
【问题讨论】:
标签: java spring spring-security