我有一个问题。在Struts中,我有一个处理用户身份验证的Action,即我获取了用户的凭据并使用DAO来验证用户凭据。我想在Spring中保持相同的设置。我正在使用Spring 3.0.3 RELEASE。
我的问题是,我已经阅读过Spring Security,并在那里指定了JDBC后端“Validation”提供程序。我想知道,如果用户点击“登录”它会将凭据提交给我的控制器以检查有效身份验证,该怎么办?
我想这样做的原因是我有一个处理用户身份验证和授权的服务。
提前致谢。
PS 如何在Spring中使某些控制器安全?
PPS 我是Spring的新手
答案 0 :(得分:5)
您可以创建一个实现org.springframework.security.authentication.AuthenticationProvider
的自定义身份验证提供程序,如下所示
package com.bzone.example;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
public class CustomAuthenticationProvider implements AuthenticationProvider{
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
// TODO call custom service or do whatever you want
return null;
}
@Override
public boolean supports(Class<? extends Object> authentication) {
// copied it from AbstractUserDetailsAuthenticationProvider
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
}
还有一个步骤是配置spring security以使用此自定义身份验证提供程序
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
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-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<!-- HTTP security configurations -->
<http auto-config="true" use-expressions="true">
<form-login login-processing-url="/static/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t"/>
<logout logout-url="/static/j_spring_security_logout"/>
<!-- Configure these elements to secure URIs in your application -->
<intercept-url pattern="/member/**" access="isAuthenticated()" />
<intercept-url pattern="/resources/**" access="permitAll" />
<intercept-url pattern="/static/**" access="permitAll" />
<intercept-url pattern="/**" access="permitAll" />
</http>
<!-- Configure Authentication mechanism -->
<authentication-manager alias="authenticationManager">
<authentication-provider ref="com.bzone.example.CustomAuthenticationProvider" />
</authentication-manager>
</beans:beans>
答案 1 :(得分:3)
通常,Spring Security使用您的代码作为策略(身份验证提供程序,用户详细信息服务等)在其自己的代码中处理身份验证。但您可以在自己的代码中处理身份验证。
在您的操作代码中,当用户凭据正确时,您将:
Authentication
(您可以使用UsernamePasswordAuthenticationToken
作为方便的实现)。SecurityContextHolder.getContext().setAuthentication(auth);
AuthenticationEventPublisher.publishAuthenticationSuccess(...)
广播身份验证成功事件(您可以从上下文自动装配它或明确创建DefaultAuthenticationEventPublisher
。)SavedRequestAwareAuthenticationSuccessHandler.onAuthenticationSuccess(...)
将用户重定向到受保护资源。您还需要提供AuthenticationEntryPoint
:
<bean id = "aep" class = "org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<!-- Your login page -->
<property name = "loginFormUrl" value = "/login" />
</bean>
<security:http entry-point-ref="aep">
...
</http>
但是,如果您在Spring中实际上是新手,那么最好避免这种大规模的自定义并使用常规的Spring Security架构。
答案 2 :(得分:2)
您可以为Spring Security编写自己的验证机制。它必须包含以下部分: