正则表达式1951 2021-04-28 17:27 采纳率: 50%
浏览 2478
已采纳

java.lang.String cannot be cast to ……怎么解决?

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("当前用户不存在!");
        }
    }
}
  • 写回答

8条回答 默认 最新

  • 正则表达式1951 2021-04-28 19:49
    关注

    问题解决了!

    解决的方式是清楚了redis数据库里面的数据,以及重新将数据导进Navicat,之后重启就没有出现这种问题了。

    个人猜测原因可能有以下两种:​​​

    之前打错了CustomerService类中第33行代码:

    redisTemplate.opsForValue().set("customer_"+username, customer);

    错误代码:

    redisTemplate.opsForValue().set("customer_"+username, username);

    但后来我重试了一遍,却没有问题了!!!

    所以还有另一种原因是redis数据库的问题!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭