Spring Security AuthenticationProvider-扩展身份验证

时间:2018-08-31 13:24:07

标签: java spring spring-mvc spring-security

我们为应用程序提供了自定义类型的用户,因此我们不想使用

@Override
public Authentication authenticate(Authentication authentication) {
    String name = authentication.getName();

在我们的

public EmployeeAuthenticationProvider implements AuthenticationProvider {

但是,因为我们有不同的传入POST参数名称,例如

@Override
public Authentication authenticate(Authentication authentication) {
    String employeeName = authentication.getEmployeeName();
    String employeePassword = authentication.getEmployeePassword();

因为我们正在为应用程序的用户使用一个名为Employee的自定义bean。

是否可以扩展身份验证并在authenticate方法中替换它?我们的EmployeeServiceImplementation应该也实现org.spring.security.core.Authentication还是应该在我们的Employee bean中实现(这会令人沮丧)?我对如何解决此问题感到困惑。

感谢您的帮助。

编辑

根据这篇出色的POST Spring get custom UserDetails in SecurityContextHolder 我现在有

class EmployeeUserDetailsService implements UserDetailsService {

class EmployeeUserDetails extends Employee implements UserDetails, Authentication{

现在我上课

class EmployeeAuthenticationProvider implements AuthenticationProvider {

我想更改方法

 org.springframework.security.authentication.AuthenticationProvider#authenticate(Authentication authentication)

所以我可以使用EmployeeUserDetails代替Authentication吗?

1 个答案:

答案 0 :(得分:0)

您不能更改方法签名,但是可以在authenticate方法返回的Authentication对象中设置EmployeeUserDetails。 您可以使用Authentication对象的setDetails方法来做到这一点