具有额外登录字段的自定义AuthenticationProvider

时间:2016-01-28 11:21:57

标签: java spring spring-security

我有一个customAuthenticationProvider,我想用3个参数验证用户:用户名,密码和tokenPin。 但目前我对这个提供商有一点问题:

@Component 
public class CustomAuthenticationProvider implements AuthenticationProvider {


@Autowired
private UserService userService;


@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    String username = authentication.getName();
    String password = authentication.getCredentials().toString();
    String pin = ????

    Authentication auth = null;

    User user = userService.findByUsernameAndPassword(username, password);

    if (user != null) {
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        auth = new UsernamePasswordAuthenticationToken(username, password, grantedAuths);
    }


    return auth;
}

@Override
public boolean supports(Class<?> authentication) {
    return authentication.equals(UsernamePasswordAuthenticationToken.class);
}

}

0 个答案:

没有答案