【发布时间】:2018-09-13 01:02:11
【问题描述】:
我正在考虑让我的应用程序更安全,并了解正在发生的一切。
我使用 Spring Boot 登录。此登录调用 UserDetailsService 实现,如下所示:
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user.getUsername().isEmpty()) {
throw new UsernameNotFoundException(
"No user found with username: "+ username);
}
String role;
if (user.getUserType().contains("Gp")){
role = "ROLE_GP";
}
else if(user.getUserType().contains("Patient")){
role = "ROLE_PATIENT";
}
else {
role = "ROLE_ADMIN";
}
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
return new org.springframework.security.core.userdetails.User
(user.getUsername(),
user.getPassword(), enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked,
getAuthorities(Arrays.asList(role)));
}
private static List<GrantedAuthority> getAuthorities (List<String> roles) {
List<GrantedAuthority> authorities = new ArrayList<>();
for (String role : roles) {
authorities.add(new SimpleGrantedAuthority(role));
}
return authorities;
}
这会为我创建一个包含用户角色的会话,以便我可以配置 WebSecurityConfigurerAdapter 等。
所以我的问题是使用 JWT 添加过滤器到底有什么作用? Spring Security 是否已经默认执行这些操作,因为我所做的已经创建了一个授权和经过身份验证的用户。
谢谢。
【问题讨论】:
-
我在问题中没有看到与 JWT 相关的代码。
-
没有。问题是,JWT 会添加什么?已经回答了。
标签: spring authentication spring-boot spring-security jwt