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"); } } }