我有一个 SpringMVC Web 应用程序,它需要通过发送用户名和密码来使用 Spring Security 对 RESTful Web 服务进行身份验证。当用户登录时,需要将 cookie 设置到用户的浏览器,并在后续调用中使用 cookie 与另一个 RESTful Web 服务验证用户 session 。
我到处找,但我一直没能找到一个很好的例子来说明如何做到这一点,我所有的尝试都是徒劳的。
这是我的想法:
我可以声明两个身份验证提供程序,第一个检查 cookie,如果由于任何原因失败,它会转到第二个检查用户名和密码的身份验证提供程序(如果该请求中没有用户名和密码,也会失败) .
这两个服务每次都返回用户的权限,spring security是“无状态”的。
另一方面,我质疑自己这种方法是否正确,因为很难找到具有相同问题的示例或其他人。这种方法有错吗?
我之所以要这样做而不仅仅是 JDBC 身份验证,是因为我的整个 Web 应用程序都是无状态的,并且始终通过包装“请愿队列”的 RESTful Web 服务访问数据库,我想尊重用户身份验证和验证也是。
到目前为止我尝试了什么?我可以粘贴很长很长的 springSecurity-context.xml,但我现在只列出它们:
提前致谢!
顺便说一句,我使用的是 Spring Security 3.1.4 和 Spring MVC 3.2.3
编辑:由于@coder 回答,我能够做到
以下是我所做的一些事情,我将尝试记录所有这些并将其发布在此处或不久后的博客文章中:
<http use-expressions="true" create-session="stateless" entry-point-ref="loginUrlAuthenticationEntryPoint"
authentication-manager-ref="customAuthenticationManager">
<custom-filter ref="restAuthenticationFilter" position="FORM_LOGIN_FILTER" />
<custom-filter ref="restPreAuthFilter" position="PRE_AUTH_FILTER" />
<intercept-url pattern="/signin/**" access="permitAll" />
<intercept-url pattern="/img/**" access="permitAll" />
<intercept-url pattern="/css/**" access="permitAll" />
<intercept-url pattern="/js/**" access="permitAll" />
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
</http>
<authentication-manager id="authManager" alias="authManager">
<authentication-provider ref="preauthAuthProvider" />
</authentication-manager>
<beans:bean id="restPreAuthFilter" class="com.company.CustomPreAuthenticatedFilter">
<beans:property name="cookieName" value="SessionCookie" />
<beans:property name="checkForPrincipalChanges" value="true" />
<beans:property name="authenticationManager" ref="authManager" />
</beans:bean>
<beans:bean id="preauthAuthProvider"
class="com.company.CustomPreAuthProvider">
<beans:property name="preAuthenticatedUserDetailsService">
<beans:bean id="userDetailsServiceWrapper"
class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
<beans:property name="userDetailsService" ref="userDetailsService" />
</beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="userDetailsService" class="com.company.CustomUserDetailsService" />
<beans:bean id="loginUrlAuthenticationEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<beans:constructor-arg value="/signin" />
</beans:bean>
<beans:bean id="customAuthenticationManager"
class="com.company.CustomAuthenticationManager" />
<beans:bean id="restAuthenticationFilter"
class="com.company.CustomFormLoginFilter">
<beans:property name="filterProcessesUrl" value="/signin/authenticate" />
<beans:property name="authenticationManager" ref="customAuthenticationManager" />
<beans:property name="authenticationFailureHandler">
<beans:bean
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<beans:property name="defaultFailureUrl" value="/login?login_error=t" />
</beans:bean>
</beans:property>
</beans:bean>
自定义实现是这样的:
// Here, the idea is to write authenticate method and return a new UsernamePasswordAuthenticationToken
public class CustomAuthenticationManager implements AuthenticationManager { ... }
// Write attemptAuthentication method and return UsernamePasswordAuthenticationToken
public class CustomFormLoginFilter extends UsernamePasswordAuthenticationFilter { ... }
// Write getPreAuthenticatedPrincipal and getPreAuthenticatedCredentials methods and return cookieName and cookieValue respectively
public class CustomPreAuthenticatedFilter extends AbstractPreAuthenticatedProcessingFilter { ... }
// Write authenticate method and return Authentication auth = new UsernamePasswordAuthenticationToken(name, token, grantedAuths); (or null if can't be pre-authenticated)
public class CustomPreAuthProvider extends PreAuthenticatedAuthenticationProvider{ ... }
// Write loadUserByUsername method and return a new UserDetails user = new User("hectorg87", "123456", Collections.singletonList(new GrantedAuthorityImpl("ROLE_USER")));
public class CustomUserDetailsService implements UserDetailsService { ... }
最佳答案
AbstractPreAuthenticatedProcessingFilter。
getPreAuthenticatedPrincipal() 方法可以检查cookie是否存在
如果它存在,则返回 cookie 名称是主体和 cookie 值
证书。
如果 cookie 存在,pre auth 过滤器将在 springContext 中设置经过身份验证的用户,并且不会调用您的 username./password 过滤器,如果 cookie 缺失/无效,则身份验证入口点将触发使用用户名/密码的身份验证
希望能帮助到你
关于Spring MVC + Spring Security 使用一个休息的 web 服务登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18205436/