如何使用DaoAuthenticationProvider以编程方式使用Spring Security对用户进行身份validation

我想知道我在这里做错了什么来validation用户。 我有一个应用程序,其中用户通过几个步骤来激活他们的帐户,并且在这样做时,我想绕过登录表单并将它们直接带到他们的仪表板。

这是我的自动登录function:

protected void automatedLogin(String username, String password, HttpServletRequest request) { try { // Must be called from request filtered by Spring Security, otherwise SecurityContextHolder is not updated CustomUserDetailsService udService = new CustomUserDetailsService(userDAO, request); UserDetails uDetails = udService.loadUserByUsername(username); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(uDetails, password); token.setDetails(new WebAuthenticationDetails(request)); DaoAuthenticationProvider authenticator = new DaoAuthenticationProvider(); Authentication authentication = authenticator.authenticate(token); SecurityContextHolder.getContext().setAuthentication(authentication); } catch (Exception e) { e.printStackTrace(); SecurityContextHolder.getContext().setAuthentication(null); } } 

我必须使用DaoAuthenticationProvider类作为我的身份validation提供程序。 我已经validation我正在获取包含正确凭据,ID,权限角色等的UserDetails模型。

当它调用authenticate方法时,我会在DaoAuthenticationProvider类中的某个地方遇到Null指针:

org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:109)org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate上的org.springframework.security.authentication.AuthenticationServiceException(AbstractUserDetailsAuthenticationProvider.java:132 )在com.bosch.actions.BaseController.doAutoLogin(BaseController.java:659)。 。 。 引起:org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:101)中的java.lang.NullPointerException

我真的不确定什么是null,因为我没有可用的源代码。

编辑我能够在这里找到源代码 – https://github.com/SpringSource/spring-security/blob/master/core/src/main/java/org/springframework/security/authentication/dao/DaoAuthenticationProvider.java

通过在对象上显式设置UserDetailsS​​ervice,我能够绕过Null指针:

 authenticator.setUserDetailsService(udService); 

但是,当我知道提供的密码是正确的时,我得到了错误的凭证exception,因为我已经在代码中早先的UserDetails对象中的调试器中看到了它。

org.springframework.security.authentication.BadCredentialsException:在org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider)的org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:87)中的凭据错误。 Java的:149)

我能够通过将Spring bean定义中定义的所有属性拼凑在一起并在DaoAuthenticationProvider对象上以编程方式设置它来获得身份validation。 回顾这看起来似乎是一个愚蠢的问题,但我希望它可以帮助某人!

更正代码:

 protected void automatedLogin(String username, String password, HttpServletRequest request) { try { // Must be called from request filtered by Spring Security, otherwise SecurityContextHolder is not updated CustomUserDetailsService udService = new CustomUserDetailsService(userDAO, request); CustomMd5PasswordEncoder passEncoder = new CustomMd5PasswordEncoder(); ReflectionSaltSource saltSource = new ReflectionSaltSource(); saltSource.setUserPropertyToUse("salt"); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); token.setDetails(new WebAuthenticationDetails(request)); DaoAuthenticationProvider authenticator = new DaoAuthenticationProvider(); authenticator.setUserDetailsService(udService); authenticator.setPasswordEncoder(passEncoder); authenticator.setSaltSource(saltSource); Authentication authentication = authenticator.authenticate(token); SecurityContextHolder.getContext().setAuthentication(authentication); } catch (Exception e) { e.printStackTrace(); SecurityContextHolder.getContext().setAuthentication(null); } }