认证是通过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