谁调用 configureGlobal 函数?如何调用 configureGlobal 函数? [英] Who calls configureGlobal function? How call configureGlobal function?

查看:47
本文介绍了谁调用 configureGlobal 函数?如何调用 configureGlobal 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习 Spring Security 时有一个问题.

I have a question while studying Spring Security.

为了设置 AuthenticationManagerBuilder,我使用了 configureGlobal 方法和 @Autowired.

To set up AuthenticationManagerBuilder, I used configureGlobal method with @Autowired.

我听说 configurationGlboal 函数名可以是任何东西(甚至是任何东西!)

I heard that the configurationGlboal function name can be anything (even whatever!)

谁调用了 configureGlobal 函数?如果函数名可能不同,它如何调用它?

Who calls the configureGlobal function? How does it call this if the function name might be different?

@EnableWebMvc
@Configuration
@ComponentScan({ "com.company.mypackage" })
@EnableWebSecurity
public class SpringWebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
          .withUser("user").password("password").roles("USER").and()
          .withUser("admin").password("password").roles("USER", "ADMIN");
    }     
}

推荐答案

Spring 调用该方法,因为它被标记为自动装配,参见 Autowired:

Spring calls that method, because it is marked as autowired, see Autowired:

自动装配方法

配置方法可以有任意名称和任意数量的参数;这些参数中的每一个都将使用 Spring 容器中的匹配 bean 自动装配.Bean 属性 setter 方法实际上只是这种通用配置方法的一个特例.此类配置方法不必公开.

Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.

但是对于 Spring Security,您还必须注释您的类,请参阅 EnableGlobalAuthentication:

But for Spring Security you also have to annotate your class, see EnableGlobalAuthentication:

使用 EnableGlobalAuthentication 注释的注解也表明注解的类可用于配置 AuthenticationManagerBuilder 的全局实例.例如:

Annotations that are annotated with EnableGlobalAuthentication also signal that the annotated class can be used to configure a global instance of AuthenticationManagerBuilder. For example:

@Configuration
@EnableWebSecurity
public class MyWebSecurityConfiguration extends WebSecurityConfigurerAdapter {

       @Autowired
       public void configureGlobal(AuthenticationManagerBuilder auth) {
               auth.inMemoryAuthentication().withUser("user").password("password").roles("USER")
                           .and().withUser("admin").password("password").roles("USER", "ADMIN");
       }

       // Possibly overridden methods ...
}

以下注解使用了EnableGlobalAuthentication

  • 启用WebSecurity
  • EnableWebMvcSecurity
  • EnableGlobalMethodSecurity

在没有 EnableGlobalAuthentication 注释的类中配置 AuthenticationManagerBuilder 会产生不可预测的结果.

Configuring AuthenticationManagerBuilder in a class without the EnableGlobalAuthentication annotation has unpredictable results.

这篇关于谁调用 configureGlobal 函数?如何调用 configureGlobal 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆