使用身份验证提供商不使用spring Security 5.7.0中的WebsecurityConfigurerAdapter

发布于 2025-02-11 17:25:06 字数 665 浏览 0 评论 0原文

现在,我正在尝试为我的Spring Boot Spring Security项目实现Spring Security身份验证提供商。因此,以前,我们能够在我们的安全配置文件中扩展WebsecurityConfigurerAdapter,以自定义HTTP和httpsececurity HTTPAuthenticationManagerBuilder auth通过覆盖配置。

但是现在(Spring Security 5.7.0)WebsecurityConfigurerAdapter被弃用了,我正在遵循以下内容的WebsecurityCustomizer方法,

@EnableWebSecurity
@Configuration
public class SecurityConfig{

 @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring()
                .antMatchers("/users/getUser");
    }

}

因此在这里使用WebsEcurityCustomizer,我如何在REST API中使用身份验证提供程序功能?谁能指导我解决此问题或请建议更新的文档请参阅?

Now I am trying to implement spring security authentication provider for my spring boot spring security project. So previously we were able to extend WebSecurityConfigurerAdapter in our Security config file to customize the http and HttpSecurity http and AuthenticationManagerBuilder auth by overriding configure.

But now (Spring Security 5.7.0) WebSecurityConfigurerAdapter got deprecated and I am following WebSecurityCustomizer method like the following,

@EnableWebSecurity
@Configuration
public class SecurityConfig{

 @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring()
                .antMatchers("/users/getUser");
    }

}

So Here using WebSecurityCustomizer , how can I use authentication provider functionality in my ReST API ? Can anyone guide me to solve this issue or kindly suggest updated documentation for refer please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听你说爱我 2025-02-18 17:25:07

我认为WebsecurityCustomizer是您在这种情况下要使用的BEAN。我想您要做的是配置SecurityFilterChain,就像:

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults());
        return http.build();
    }

无论如何,不​​建议在某个端点中禁用Spring Security,替代方法是使用授权>授权>授权。 AntMatchers(“/my-endpoint”)。如果您不希望在该端点上安全性,请允许()

关于Authentication ProviderAuthenticationManager此链接可以为您提供可能遇到的问题。

全局身份验证策略
创建一个可用于整个可用的身份验证管理器
应用程序您可以将AuthenticationManager注册为@bean。

局部身份验证manager
在Spring Security 5.6中,我们介绍了Httpsecurity#AuthenticationManager的方法,该方法覆盖了特定的SecurityFilterChain的默认authenticationManager。
以下是一个示例配置,将自定义AuthenticationManager设置为默认值:

@Configuration
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults())
            .authenticationManager(new CustomAuthenticationManager());
        return http.build();
    }

}

I don't think that the WebSecurityCustomizer is the bean that you want to use in this case. I guess that what you are trying to do is to configure a SecurityFilterChain, like so:

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults());
        return http.build();
    }

Anyways, it is not recommended to disable Spring Security in a certain endpoint, the alternative is to use authorizeHttpRequests.antMatchers("/my-endpoint").permitAll() if you don't want security on that endpoint.

About the AuthenticationProvider and AuthenticationManager, this link can help you with questions that you may have.

Global AuthenticationManager
To create an AuthenticationManager that is available to the entire
application you can simply register the AuthenticationManager as a @Bean.

Local AuthenticationManager
In Spring Security 5.6 we introduced the method HttpSecurity#authenticationManager that overrides the default AuthenticationManager for a specific SecurityFilterChain.
Below is an example configuration that sets a custom AuthenticationManager as the default:

@Configuration
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults())
            .authenticationManager(new CustomAuthenticationManager());
        return http.build();
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文