ludc
2024-02-29 8ef9e366be48dc5e8e52617ea8ed48b37a0e1f74
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
28
29
30
31
32
package com.vci.ubcs.admin.config;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.User;
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;
 
@Service
public class CustomUserDetailsService implements UserDetailsService {
 
    @Value("${spring.security.user.name:admin}")
    private String USERNAME;
 
    @Value("${spring.security.user.password:admin}")
    private String PWD;
 
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 这里为了示例,创建一个简单的用户
        if (USERNAME.equals(username)) {
            return User.builder()
                .username(username)
                .password(PWD) // 使用{noop}表示明文密码,实际生产环境中应该使用加密密码
                .roles("ADMIN")
                .build();
        } else {
            throw new UsernameNotFoundException("User not found");
        }
    }
}