编写一个类自定义实现 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");
}

Spring Boot 集成 Spring Security 使用自定义的安全数据源的更多相关文章

  1. spring boot集成mybatis-plus插件进行自定义sql方法开发时报nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    spring boot集成mybatis-plus插件进行自定义sql方法开发时报nested exception is org.apache.ibatis.binding.BindingExcept ...

  2. Spring Boot集成Spring Data Reids和Spring Session实现Session共享

    首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...

  3. SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  4. SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  5. Spring Boot 集成 Spring Security 实现权限认证模块

    作者:王帅@CodeSheep   写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...

  6. Spring boot 集成Spring Security

    依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  7. Spring Boot 集成spring security4

    项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...

  8. Spring boot集成spring session实现session共享

    最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...

  9. Spring Boot 集成 Spring Security

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

随机推荐

  1. java中时间与时间戳的相互转换

    package com.test.one; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...

  2. 如何查看SQL Server某个存储过程的执行历史【转】

    db_name(d.database_id) as DBName, s.name as 存储名称, s.type_desc as 存储类型, d.cached_time as SP添加到缓存的时间, ...

  3. Redis集群,备份,哨兵机制

    原文:https://blog.csdn.net/zy345293721/article/details/87536144 1.集群        先来简单了解下redis中提供的集群策略, 虽然re ...

  4. 数据库允许空值(null),往往是悲剧的开始 (转)

    数据库字段允许空值,会遇到一些问题,此处包含的一些知识点,和大家聊一聊. 数据准备: create table user ( id int, name varchar(20), index(id) ) ...

  5. JavaScript的循环结构和经典题目

    一.JS中的循环结构 循环结构的执行步骤1.声明循环变量:2.判断循环条件;3.执行循环体操作:4.更新循环变量:5.然后循环执行2-4,直到条件不成立,跳出循环. while循环()中的表达式,运算 ...

  6. 41. First Missing Positive (JAVA)

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  7. Apache 配置IP站点

    配置临时生效 IP: [root@Nagios-Server extra]# ifconfigeth0:0 192.168.1.126/24 up [root@Nagios-Server extra] ...

  8. media(上传的文件或图片路径配置)

    urls url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}), settings MED ...

  9. PAT Advanced 1027 Colors in Mars (20 分)

    People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...

  10. php内置函数分析array_diff()

    PHP_FUNCTION(array_diff) { zval *args; int argc, i; uint32_t num; HashTable exclude; zval *value; ze ...