Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用DaoAuthenticationProvider以编程方式使用Spring Security对用户进行身份验证_Java_Spring_Authentication_Spring Security_Dao - Fatal编程技术网

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

Java 如何使用DaoAuthenticationProvider以编程方式使用Spring Security对用户进行身份验证,java,spring,authentication,spring-security,dao,Java,Spring,Authentication,Spring Security,Dao,我想知道我在验证用户身份时做错了什么。我有一个应用程序,用户通过几个步骤来激活他们的帐户,这样做后,我想绕过登录表单,直接将他们带到仪表板 以下是我的自动登录功能: protected void automatedLogin(String username, String password, HttpServletRequest request) { try { // Must be called from request filtered by Spr

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

以下是我的自动登录功能:

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

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

org.springframework.security.authentication.AuthenticationServiceException 在 org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:109) 在 org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132) 在 com.bosch.actions.BaseController.doAutoLogin(BaseController.java:659) . . . 原因:java.lang.NullPointerException位于 org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:101)

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

编辑 我在这里找到了源代码-

我可以通过显式设置对象上的UserDetailsService绕过空指针:

authenticator.setUserDetailsService(udService);
但现在,当我知道提供的密码正确时,就会出现坏凭据异常,因为我在代码前面设置的UserDetails对象的调试器中看到了它

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


通过将Springbean定义中定义的所有属性拼凑在一起,并在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);
        }

    }

Spring Security是开源的,您确实有可用的源代码。您可能遇到了问题,因为DaoAuthenticationProvider被设计为一个spring管理的bean。很高兴您找到了一个修复程序,但老实说,spring安全并不是这样使用的。你自己做了很多工作,这是不必要的。这不是应用程序中Spring Security的正常用法。这是一个一次性实例,我需要用户登录,而不需要他们在表单中提供凭据。每隔一段时间,他们将通过表单登录,SpringSecurityBean将完成这项工作。经过解释,如果是你否决了答案,我希望你改变主意,因为我认为这不公平地损害了我在StackOverflow的声誉。请放心,我的朋友,我没有否决你。我不认为答案是错误的,我只是建议SpringSecurity不应该这样使用。还有P.S.投反对票你也会降低我的声誉…:)此外,以这种方式进行编程身份验证在功能测试套件中也很有用,这就是我来到这里的原因。不要假设上下文。