正则表达式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 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来