Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.regex.domain.Customer
at com.regex.service.CustomerService.getCustomer(CustomerService.java:28) ~[classes/:na]
at com.regex.service.impl.UserDetailsServiceImpl.loadUserByUsername(UserDetailsServiceImpl.java:23) ~[classes/:na]
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:93) ~[spring-security-core-5.4.6.jar:5.4.6]
... 57 common frames omitted
package com.regex.service;
import com.regex.domain.Authority;
import com.regex.domain.Customer;
import com.regex.repository.AuthorityRepository;
import com.regex.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
// 对用户数据结合Redis缓存进行业务处理
@Service
public class CustomerService {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private AuthorityRepository authorityRepository;
@Autowired
private RedisTemplate redisTemplate;
// 业务控制:使用唯一用户名查询用户信息
public Customer getCustomer(String username){
Customer customer = null;
Object o = redisTemplate.opsForValue().get("customer_"+username);
//System.out.println(o);
if(o != null){
customer = (Customer) o;
System.out.println(customer);
}else {
customer = customerRepository.findByUsername(username);
if(customer != null){
redisTemplate.opsForValue().set("customer_"+username, customer);
}
}
return customer;
}
// 业务控制:使用为用户名查询权限
public List<Authority> getCustomerAuthority(String username){
List<Authority> authorities = null;
Object o = redisTemplate.opsForValue().get("authorities_"+username);
if(o != null){
authorities = (List<Authority>)o;
}else {
authorities = authorityRepository.findAuthoritiesByUsername(username);
if(authorities.size()>0){
redisTemplate.opsForValue().set("authorities_"+username,authorities);
}
}
return authorities;
}
}
package com.regex.service.impl;
import com.regex.domain.Authority;
import com.regex.domain.Customer;
import com.regex.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.*;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
// 自定义一个UserDetailService接口实现类进行用户认证信息封装
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private CustomerService customerService;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
// 通过业务方法获取用户及权限信息
Customer customer = customerService.getCustomer(s);
List<Authority> authorities = customerService.getCustomerAuthority(s);
// 对用户权限进行封装
List<SimpleGrantedAuthority> list = authorities.stream()
.map(authority -> new SimpleGrantedAuthority(authority.getAuthority()))
.collect(Collectors.toList());
// 返回封装的UserDetails用户详情类
if(customer != null){
UserDetails userDetails =
new User(customer.getUsername(),customer.getPassword(),list);
return userDetails;
}else {
// 如果查询的用户不存在(用户名不存在),必须抛出此异常
throw new UsernameNotFoundException("当前用户不存在!");
}
}
}