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

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 快手联盟怎么快速的跑出建立模型
  • ¥20 ros环境下运行orb-slam一直报错
  • ¥15 关于腾讯云服务器中,PHP页面无法打开的问题
  • ¥60 如何把照片修复成原始拍摄图
  • ¥80 Exited too quickly (process log may have details)
  • ¥15 爬知乎登录之后内容加载不出来
  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息
  • ¥15 函数r关于两个分量y,z方向上的图像,分开画r随y的图像,r随z的图像