Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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 Spring身份验证提供程序忽略我的详细信息_Java_Spring - Fatal编程技术网

Java Spring身份验证提供程序忽略我的详细信息

Java Spring身份验证提供程序忽略我的详细信息,java,spring,Java,Spring,我想知道,为什么我从来没有使用getDetails从身份验证对象获取我的用户对象。我仅使用getPrincipal获取用户名 在调试我的项目时,我看到了一种我没有预料到的行为 我使用弹簧3.2.4.1释放 security-app-context.xml包含此配置 <authentication-manager alias="authenticationManager"> <authentication-provider user-service-ref="userDe

我想知道,为什么我从来没有使用getDetails从身份验证对象获取我的用户对象。我仅使用getPrincipal获取用户名

在调试我的项目时,我看到了一种我没有预料到的行为

我使用弹簧3.2.4.1释放

security-app-context.xml包含此配置

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="userDetailsService">
        <password-encoder ref="passwordEncoder" />
    </authentication-provider>
</authentication-manager>
可以看到,详细信息是使用我的服务中的当前用户数据设置的

从一点调试它,当它返回模型时,我们返回

org.springframework.security.authentication.dao.DaoAuthenticationProvider line 101
org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider line 132
在第二个类中,第190行中有一个方法createSuccessAuthentication,它最终被执行

protected Authentication createSuccessAuthentication(Object principal, Authentication authentication,
        UserDetails user) {
    // Ensure we return the original credentials the user supplied,
    // so subsequent attempts are successful even with encoded passwords.
    // Also ensure we return the original getDetails(), so that future
    // authentication events after cache expiry contain the details
    UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(principal,
            authentication.getCredentials(), authoritiesMapper.mapAuthorities(user.getAuthorities()));
    result.setDetails(authentication.getDetails());

    return result;
}
在第198行中,它向结果中添加了详细信息(身份验证),但它不是从提供的UserDetails中获取,而是从不包含详细信息的身份验证中获取


这是配置问题,是我的错还是Spring中的错误?

getPrincipal()方法返回UserDetails的实现,而不是getDetails()方法。

在进行身份验证时,框架是否调用了loadUserByUsername()实现?实际上,您的实现在凭证错误的情况下会引发异常:在这种情况下,最好定义一个自定义身份验证提供程序。如本文()所述,您的服务必须只检索数据,而不执行身份验证。我开始用那个方法调试。此外,my UserDetailsService实现了标准接口,该接口会抛出UsernameNotFoundException,因此,如果找不到用户名,为什么不应该抛出它?UserDetailsModel类中是否有toString()方法?通常,当从身份验证对象调用getPrincipal()时,应该返回UserDetails对象。看,我不想一串东西。我需要已经从数据存储中请求的用户数据。不需要,再做一次……对不起,我在前面的评论中不清楚:我的意思是服务中构建的UserDetails对象应该由getPrincipal()方法返回,而不是getDetails()方法(请参阅前面评论中指向spring文档的链接)。这就是调用getDetails()时得到null的原因。我询问toString()只是为了确认(或不是)您获取此用户名信息的方式:我仍然不清楚,也许您可以将代码放在检索身份验证对象的位置?
protected Authentication createSuccessAuthentication(Object principal, Authentication authentication,
        UserDetails user) {
    // Ensure we return the original credentials the user supplied,
    // so subsequent attempts are successful even with encoded passwords.
    // Also ensure we return the original getDetails(), so that future
    // authentication events after cache expiry contain the details
    UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(principal,
            authentication.getCredentials(), authoritiesMapper.mapAuthorities(user.getAuthorities()));
    result.setDetails(authentication.getDetails());

    return result;
}