【发布时间】:2018-05-28 01:21:34
【问题描述】:
我以为我的授权实现已经完成,但是在尝试检索 UserDetails 对象时,我得到的只是用户名。
我正在使用具有以下详细信息的 oauth。
配置 AuthenticationManager:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
完成后,我可以调试到我的 userDetailsService:
@Service
public class UserServiceImpl implements UserService, UserDetailsService {
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
MyUser persistedUser = userRepository.findByEmail(email);
if (persistedUser == null) {
throw new UsernameNotFoundException(String.format("The email %s doesn't exist", email));
}
List<GrantedAuthority> authorities = new ArrayList<>();
MyUser inMemoryUser = new MyUser(persistedUser.getEmail(), null, persistedUser.getEnabled(), false,
false, false, authorities);
return inMemoryUser;
}
}
这完成得很好,我的客户取回了 JWT。但是我在调试后面的控制器方法时发现了以下问题。
@GetMapping
public @ResponseBody Iterable<Curriculum> getMyCurriculums(@AuthenticationPrincipal MyUser injectedUser) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
MyUser principle = (MyUser) auth.getPrincipal();
return curriculumService.findByUser(principle);
}
在这种情况下,injectUser = null,auth 是一个 OAuth2Authentication,principal 是一个字符串 - 用户名。应该是我的用户
【问题讨论】:
-
而你的问题是...... ???
标签: spring spring-security oauth