Springboot Security不同请求路径认证不同表用户

本文介绍了如何在Springboot Security中针对不同请求路径使用不同的AuthenticationManager进行用户认证。通过配置两个独立的AuthenticationManager,实现了针对不同用户类型的服务。内容涉及到UserDetails的定制以及参照ruoyi框架设计思路,允许在特定Controller和请求路径上灵活应用各自的LoginService实现认证功能。
摘要由CSDN通过智能技术生成

认证是通过AuthenticationManager这个接口来实现的,那么就需要配置两个不同用户类型的AuthenticationManager。

package com.ruoyi.framework.config;

import com.ruoyi.framework.web.service.NovelUserDetailsServiceImpl;
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


@Configuration
public class SecurityBeanConfig {
    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @Autowired
    private NovelUserDetailsServiceImpl novelUserDetailsService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    /**
     * 配置两个AuthenticationManager
     * @return
     */

    @Bean
    public AuthenticationManager sysUserAuthenticationManager() {
        DaoAuthenticationProvider sysUserDao = new DaoAuthenticationProvider();
        sysUserDao.setUserDetailsService(userDetailsService);
        sysUserDao.setPasswordEncoder(bCryptPasswordEncoder);
        return new ProviderManager(sysUserDao);
    }

    /**
     * 两个相同的Bean其中有一个需要Primary注解
     * @return
     */
    @Bean
    @Primary
    public AuthenticationManager novelUserAuthenticationManager() {
        DaoAuthenticationProvider novelUserDao = new DaoAuthe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值