【发布时间】:2019-06-23 04:27:43
【问题描述】:
我正在尝试使用 Spring Boot 和 JWT 来保护 Rest API。现在,我已经能够拼凑配置的各个部分,以获得使用硬编码的用户名和密码生成的令牌。我希望使用我的 User 类和存储库。
我已经能够在这里硬编码一个用户
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user")
.password(passwordEncoder().encode("password"))
.authorities("ROLE_USER");
}
我应该将它指向我的 UserDetailsService 吗?我该怎么做?
@Service
public class UserSecurityService implements UserDetailsService {
private static final Logger LOG = LoggerFactory.getLogger(UserSecurityService.class);
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername (String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (null == user) {
LOG.warn("username not found");
throw new UsernameNotFoundException("Username" + username + "not found");
}
return user;
}
}
【问题讨论】:
标签: mongodb spring-boot spring-security jwt