gpt4 book ai didi

java - 如何使用 DaoAuthenticationProvider 以编程方式使用 Spring Security 对用户进行身份验证

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:34 31 4
gpt4 key购买 nike

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

这是我的自动登录功能的样子:

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 类作为我的身份验证提供程序。我已验证我正在获取包含正确凭据、ID、权限角色等的 UserDetails 模型。

当它调用 authenticate 方法时,我在 DaoAuthenticationProvider 类中的某处遇到了空指针:

org.springframework.security.authentication.AuthenticationServiceException at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:109) at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132) at com.bosch.actions.BaseController.doAutoLogin(BaseController.java:659) . . . Caused by: java.lang.NullPointerException at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:101)

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

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

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

authenticator.setUserDetailsService(udService);

但现在当我知道提供的密码正确时,我得到了错误的凭据异常,因为我已经在代码前面设置的 UserDetails 对象的调试器中看到了它。

org.springframework.security.authentication.BadCredentialsException: Bad credentials at org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:87) at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:149)

最佳答案

我能够通过将 spring bean 定义中定义的所有属性拼凑在一起并以编程方式在 DaoAuthenticationProvider 对象上设置它们来使身份验证工作。回想起来这似乎是一个愚蠢的问题,但我希望它能帮助到别人!

更正后的代码:

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);
}

}

关于java - 如何使用 DaoAuthenticationProvider 以编程方式使用 Spring Security 对用户进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006137/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com