首页 技术 正文
技术 2022年11月15日
0 收藏 757 点赞 3,793 浏览 1605 个字

编写一个类自定义实现 UserDetailsService 接口

@Service("customUserDetailService")
public class CustomUserDetailService implements UserDetailsService { @Autowired
private UserRepository userRepository; @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { System.out.println("在 CustomUserDetailService 传入的 username => " + username); com.liwei.entity.User user = userRepository.findByUserName(username);
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
User securityUser = new User(user.getUserName(), user.getPassword(), authorities);
return securityUser;
}
}

指定装配 UserDetailsService

@Autowired
@Qualifier("customUserDetailService")
private UserDetailsService userDetailsService;

配置 userDetailsService

auth.userDetailsService(userDetailsService)

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(new PasswordEncoder() {
/**
* 这个方法的注释我不知道应该怎样写,含义是提供一个加密的算法?
*
* @param rawPassword
* @return
*/
@Override
public String encode(CharSequence rawPassword) {
return encoder.encode(rawPassword.toString());
} /**
* 提供一个匹配的算法
*
* @param rawPassword 用户输入的密码
* @param encodedPassword "数据库"中的密码,可以理解为安全数据源的密码
* @return
*/
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
return encoder.matches(rawPassword, encodedPassword);
}
});
//.withUser("liwei").password("c019306df0757d86de9a14c1033fb80d84fa77f13edc4ff985dacac612043657a0246bd8e6b3ebab").roles("USER").and()
//.withUser("zhouguang").password("2f4353cc3b8bc0fbde0a6aad1a438dec110c3362b33e0804e95e6f3368e80625fdd5dd2aacdcdf32").roles("USER", "ADMIN");
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,957
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,480
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,327
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,110
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,742
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,776