-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserDetailService.java
27 lines (23 loc) · 1.05 KB
/
UserDetailService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.al._01springsecuritydemo01.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.al._01springsecuritydemo01.entity.User;
@Service
public class UserDetailService implements UserDetailsService {
@Autowired
private UserService userService;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userService.selectUserByUserName(username);
if (user == null) {
throw new UsernameNotFoundException("用户" + user.getUsername() + "不存在");
}
// 设置权限
user.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRoles()));
return user;
}
}