java 在Sping Boot 中升级已弃用的WebSecurityConfigurerAdapter [重复]

vngu2lb8  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(293)

此问题在此处已有答案

Spring Security: Upgrading the deprecated WebSecurityConfigurerAdapter in Spring Boot 2.7.0(6个答案)
2天前关闭。
如何在不使用WebSecurityConfigurerAdapter的情况下替换代码

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService() {
        return new UserDetailsServiceImpl();
    }

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

    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService());
        authProvider.setPasswordEncoder(passwordEncoder());

        return authProvider;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin().permitAll()
                .and()
                .logout().permitAll();
    }
}

我试了很多但是我不能修复它。请帮助我!

yc0p9oo0

yc0p9oo01#

我有一些最新消息要告诉你

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http.
            authorizeHttpRequests()
            .anyRequest()
            .authenticated()
            .and()
            .httpBasic();

    return http.build();
  }
}

相关问题