【发布时间】:2019-06-01 11:35:31
【问题描述】:
我正在尝试为当前登录的用户添加一些自定义数据,所以我发现我可以实现自己的 UserDetailsService 并将其插入 Spring,但它从未被调用,我总是将 Principal 作为用户名字符串。
我已经实现了 UserDetailsService:
@Service
public class UserDetailServiceImpl implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
...
}
}
我对 UserDetails 的实现:
import org.springframework.security.core.userdetails.User;
public class LoggedInUser extends User {
...
}
并尝试通过多种方式在 config (SecurityConfiguration) 中进行设置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider customAuthProvider;
@Autowired
private UserDetailsService userDetailServiceImpl;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthProvider);
auth.userDetailsService(userDetailServiceImpl).passwordEncoder(passwordService.getPasswordEncoder());
}
...
}
或
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthProvider);
auth.userDetailsService(userDetailServiceImpl).passwordEncoder(passwordService.getPasswordEncoder());
}
或
@Override
@Bean
public UserDetailsService userDetailsService() {
return new UserDetailServiceImpl();
}
没有任何效果...我尝试了多种方式检索用户信息:
- 在带有 @AuthenticationPrincipal 的控制器中我得到 null
-
在服务中(我收到无效的转换错误):
身份验证身份验证 = SecurityContextHolder.getContext().getAuthentication(); (LoggedInUser) authentication.getPrincipal()
知道为什么它不起作用吗?我的 impl 类是否在其他地方被默认覆盖?我试图查看日志(logging.level.org.springframework.security=TRACE)但没有运气:/ 我可以登录,效果很好,只是主体数据总是只有用户名字符串,而不是我的班级。
【问题讨论】:
-
您是否保护了您的端点?
-
什么是
CustomAuthenticationProvider?显示您的代码。 -
可能会弄错,但我认为您的 userDetailsService 没有被调用,因为您定义了自定义身份验证提供程序。它完成了包括寻找用户在内的所有工作。
-
@rbiggy 您能否分享您的代码,尤其是您的安全配置?我在同一个问题上苦苦挣扎:CustomAuthenticationProvider 不起作用(authenticate() 方法没有被调用)并且 UserDetailsService 也没有被调用..
标签: java spring spring-boot spring-security userdetailsservice