首页 > 其他分享 >Spring security的SecurityConfig配置时 userDetailsService报错如何解决?

Spring security的SecurityConfig配置时 userDetailsService报错如何解决?

时间:2024-08-30 13:50:31浏览次数:5  
标签:userDetailsService UserDetailsService Spring IUsersService springframework 报错 im

文章目录

报错信息

在这里插入图片描述

‘userDetailsService(T)’ in ‘org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder’ cannot be applied to ‘(com.user_service.service.IUsersService)’

原因

这个错误是因为在 SecurityConfig 配置中,AuthenticationManagerBuilderuserDetailsService 方法期待的是实现了 UserDetailsService 接口的类,而你可能传递的是 IUsersService,而它并没有实现 UserDetailsService 接口。

解决方案

要解决这个问题,你有以下几种方式:

1. 实现 UserDetailsService 接口

首先,你需要让 IUsersService 实现 UserDetailsService 接口,以便它能够被 AuthenticationManagerBuilder 使用。

修改 IUsersService 接口和实现类

你可以扩展 IUsersService 接口,使其实现 UserDetailsService,例如:

import org.springframework.security.core.userdetails.UserDetailsService;

public interface IUsersService extends IService<Users>, UserDetailsService {
    // 其他方法声明
}

然后在 UsersServiceImpl 类中实现 UserDetailsServiceloadUserByUsername 方法:

import com.user_service.entity.Users;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Users user = this.getOne(new QueryWrapper<Users>().eq("username", username));
        if (user == null) {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }
        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(),
                new ArrayList<>());
    }
}

这样,IUsersService 就实现了 UserDetailsService,可以在 SecurityConfig 中使用。

2. 修改 SecurityConfig

SecurityConfig 中,现在可以注入 IUsersService,并将其作为 userDetailsService 使用:

import com.user_service.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Autowired
    private IUsersService usersService;

    @Bean
    public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
        return http.getSharedObject(AuthenticationManagerBuilder.class)
                .userDetailsService(usersService) // 这里传入 usersService
                .passwordEncoder(passwordEncoder())
                .and()
                .build();
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.csrf(AbstractHttpConfigurer::disable)
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/auth/login", "/auth/register").permitAll()
                .anyRequest().authenticated()
            )
            .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
            .addFilterBefore(new JwtAuthenticationFilter(jwtTokenUtil, signatureUtil, usersService), UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

3. 其他注意事项

  • 如果你使用了自定义的 UserDetails 实现,确保在 UsersServiceImpl 中的 loadUserByUsername 方法返回了正确的 UserDetails 实例。
  • 如果用户的角色或权限信息也存储在 Users 实体中,你需要在 loadUserByUsername 方法中将这些信息转换为 UserDetails 对象。

通过这些修改,你应该能够将 IUsersService 作为 UserDetailsService 使用,并解决上述错误。

仅为个人知识分享及开发中遇到的问题总结,
希望对你有所帮助,若有问题欢迎指正~

标签:userDetailsService,UserDetailsService,Spring,IUsersService,springframework,报错,im
From: https://blog.csdn.net/qq_42631788/article/details/141712990

相关文章

  • Spring EL表达式使用详解
    SpringEL表达式使用详解什么是SpringEL表达式SpringEL表达式是Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL,JSTL表达式语言。Spring开发中我们会经常涉及到调用各种资源的情况,包含普通文件、网址、正则表达式、系统变量、其他Bean的一些属性、配置文件、集......
  • 基于SpringBoot+Vue足球俱乐部管理后台的设计和实现(源码+文档+部署讲解)
    博主介绍:全网粉丝10W+,CSDN博客专家、全栈领域优质创作者,3年JAVA全栈开发经验,专注JAVA技术、系统定制、远程指导,致力于企业数字化转型。研究方向:SpringBoot、Vue.JS、MyBatisPlus、Redis、SpringSecurity、MySQL、小程序、Android、Uniapp等。博主说明:本文项目编号......
  • 基于SpringBoot+Vue师生健康管理系统的设计和实现(源码+文档+部署讲解)
    博主介绍:全网粉丝10W+,CSDN博客专家、全栈领域优质创作者,3年JAVA全栈开发经验,专注JAVA技术、系统定制、远程指导,致力于企业数字化转型。研究方向:SpringBoot、Vue.JS、MyBatisPlus、Redis、SpringSecurity、MySQL、小程序、Android、Uniapp等。博主说明:本文项目编号......
  • Springmvc中的处理器、拦截器、过滤器的执行时机
    Springmvc中的处理器、拦截器、过滤器的执行时机测试案例:publicclassMyFilterimplementsFilter{@Overridepublicvoidinit(FilterConfigfilterConfig)throwsServletException{System.out.println("--->>MyFilter.init");}@Override......
  • spring boot 以请求来调用Ollama大模型,不使用spring ai
    之前有一版使用springai的,但是其实Ollama有一套api可以直接调用。api地址是https://github.com/ollama/ollama/blob/main/docs/api.md我们聊天其实调用的就是这个(其余的我就不赘述了,大家自行研究)调用http请求这方面,大家可自行编写,请求的方式五花八门,网上的库......
  • @Import注解 -【Spring底层原理】
    通过在配置类上使用@Import注解,将User给注入进容器中,运行启动类,可以看到容器中有User对象:image-20210226164625069【2】导入ImportSelector的实现类导入ImportSelector的实现类需要实现ImportSelector类,自定义逻辑返回需要导入的组件,返回的字符串数组即是要注入的组件,添加修改......
  • SpringBoot记录日志
    @Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public@interfaceLog{//自定义操作日志记录注解publicStringtitle();//模块名称publicOperatorTypeoperatorType()defaultOperatorType.MAN......
  • spring boot 根据实体类生成表
    在开发的过程中,经常会遇到数据库表结构发生变化或者新增表的情况,而spring体系下提供的springdatajpa可以帮助我们很方便的处理这种情况,只需要简单的配置既可!以下便是实现路径以及其中需要注意的事项。添加依赖<dependency><groupId>org.springframework.boo......
  • Vue的那些报错
    【Vue-cli】npminstall时报错:npmERRCouldnotresolvedependencynpmERRpeernpmERR!codeERESOLVEnpmERR!ERESOLVEunabletoresolvedependencytreenpmERR!npmERR!Whileresolving:[email protected]!Found:[email protected]!node_modules/es......
  • 【GaussDB】应用报错 socket is not closed; Urgent packet sent to backend successf
    数据库原理差异在Oracle中连接会话默认永不超时,Mysql中连接会话默认8小时超时,而在GaussDB中,默认30min超时问题排查确认探活是否生效方式一:查询审计日志,可以看到会话退出方式为超时退出:selecttime,type,result,client_conninfo,detail_infofrompg_query_audit('2023-06-06......