我正在启动一个新的 spring boot 项目,希望在 Spring Security 中禁用自动配置的用户,我尝试了很多配置但都没有用,我不知道我缺少什么?!!!
如您所见,我正在使用spring boot 2.1.5和web flux、security、mongo、actuator
为了禁用自动用户配置,我尝试实现自己的UserDetailService安全性,如下所示
@Configuration
@EnableWebSecurity
public class SecurityConfigurer extends WebSecurityConfigurerAdapter {
private UserDetailsService userDetailsService;
private PasswordEncoder passwordEncoder;
public SecurityConfigurer(EsportUserDetailService userDetailsService, PasswordEncoder passwordEncoder) {
this.userDetailsService = userDetailsService;
this.passwordEncoder = passwordEncoder;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll()
.and()
.authorizeRequests().anyRequest().authenticated();
}
}
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
public class EsportUserDetailService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Optional<User> userFindByUsername = userRepository.findByUsername(username);
User user = new User();
if (userFindByUsername.isPresent()) user = userFindByUsername.get();
return new EsportPrincipal(user);
}
}
@Configuration
public class BeansConfig{
@Bean
public PasswordEncoder getPasswordEncoding(){
return new BCryptPasswordEncoder();
}
@Bean
public EsportUserDetailService getEsportUsersDetailSericeImplementation(){
return new EsportUserDetailService();
}
}
陪伴而非守候
相关分类