Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring security中registerGlobal()、configure()、configureGlobal()和configureGlobalSecurity之间的差异_Java_Spring_Spring Security - Fatal编程技术网

Java Spring security中registerGlobal()、configure()、configureGlobal()和configureGlobalSecurity之间的差异

Java Spring security中registerGlobal()、configure()、configureGlobal()和configureGlobalSecurity之间的差异,java,spring,spring-security,Java,Spring,Spring Security,我有以下三个代码段,它们都在做同样的事情:创建内存中的身份验证。那么它如何影响用不同的方法名称定义它呢 注册全球 配置 配置全局 配置全局安全性 第一个: public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user").password("password").rol

我有以下三个代码段,它们都在做同样的事情:创建内存中的身份验证。那么它如何影响用不同的方法名称定义它呢

  • 注册全球
  • 配置
  • 配置全局
  • 配置全局安全性
  • 第一个:

    public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
          .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER").and()
            .withUser("admin").password("password").roles("USER","ADMIN");
        }
    }
    
    第二个:

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
             .inMemoryAuthentication()
                  .withUser("user").password("password").roles("USER");
     }
    
    第三个:

    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
             .inMemoryAuthentication()
                  .withUser("user").password("password").roles("USER");
    }
    
    第四:

    @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth)     throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
    }
    
    更新1: 我想补充一点:

    WebSecurityConfigureAdapter类中存在configure()方法,而其他方法不存在

    更新2:

    我将我的示例项目中的方法重命名为下文,令我惊讶的是,它正在工作并验证用户

    你可以给它起任何名字,它就能工作

    @Autowired
    public void anyMethodName(AuthenticationManagerBuilder auth) throws Exception {
                auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");      
    }
    

    查看:
    registerGlobal(AuthenticationManagerBuilder auth)
    configureGlobal(AuthenticationManagerBuilder auth)

    configureGlobal方法的名称不重要。但是,仅在用@EnableWebSecurity、@EnableWebMvcSecurity、@EnableGlobalMethodSecurity或@EnableGlobalAuthentication注释的类中配置AuthenticationManagerBuilder非常重要。否则会产生不可预测的结果

    来源:
    “HelloSpringSecurityJavaConfig”指南中的章节



    protectedvoid configure(AuthenticationManagerBuilder auth)
    是一种可能由(及其接口
    websecurityconfigure
    )提供的方法-我想说这只是一种更为类型化的保存方法,但其结果并没有什么不同。

    事实上,您只有两个不同的选项

    选项1:仅使用注释(包括示例1、3和4-请注意,示例中未包含相关注释)

    registerGlobal
    configureGlobal
    configureGlobalSecurity
    都是完全相同的做法。你可以根据自己的喜好来命名这种方法。唯一的限制是:

    • @Autowired
    • 该方法必须位于用以下项之一批注的类中:、或
    • (当然,该方法有一个类型为的参数)
    (正如您所看到的,方法的名称并不重要,这就是为什么在搜索代码示例时发现了这么多不同的方法名称)

    下面是一个示例,展示了它的外观:

    @EnableWebSecurity
    public class MyConfiguration {
    
        @Autowired
        public void whatever(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
              .withUser("user").password("password").roles("USER").and()
              .withUser("admin").password("password").roles("USER", "ADMIN");
        }
    
        ...
    
    }
    
    选项2:使用注释+方法重写(包括示例2)

    重写
    configure
    在的子类(或任何@Configuration类实现)中是一种方便的方法,但其效果与其他选项相同


    如何选择正确的方法? 这只是一个品味/编程风格的问题,因为两种方法都有相同的效果

    当您希望/需要将配置保存在单个类中,但@configuration类已经扩展了其他类(并且您不希望实现整个接口)时,第一个选项是有意义的


    让我们更详细地解释我的最后一点。Spring提供了许多适配器类,您可以扩展这些类以加快Spring配置的开发

    作为一个例子,让我们使用一个常用的适配器:您将从以下非常简单的配置开始:

    @EnableWebMvc
    @Configuration
    @ComponentScan({ "com.company.mypackage" })
    public class SpringWebConfig extends WebMvcConfigurerAdapter {
    
    }
    
    这里重要的是:您的类已经扩展了一个适配器类,所以您不能扩展另一个适配器类


    现在,您需要添加安全配置。您可以选择是将其包含在现有的
    SpringWebConfig
    配置类中,还是创建新的特定于安全性的配置类。以下是两种方法的示例:

    1)单一@配置类方法

    这里需要注意的是:SpringWebConfig扩展了WebMVCConfigureAdapter+@EnableWebSecurity

    @EnableWebMvc
    @Configuration
    @ComponentScan({ "com.company.mypackage" })
    @EnableWebSecurity
    public class SpringWebConfig extends WebMvcConfigurerAdapter {
    
        @Autowired
        public void whatever(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
              .withUser("user").password("password").roles("USER").and()
              .withUser("admin").password("password").roles("USER", "ADMIN");
        }     
    }
    

    2)特定安全@配置类

    @Autowired
    public void anyMethodName(AuthenticationManagerBuilder auth) throws Exception {
                auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");      
    }
    
    这里需要注意的是:MySecurityConfig扩展了websecurityconfig适配器

    保持SpringWebConfig不变,并创建一个新的
    @Configuration
    类:

    @Configuration
    @EnableWebSecurity
    public class MySecurityConfig extends WebSecurityConfigurerAdapter {
        @Overide
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
              .withUser("user").password("password").roles("USER").and()
              .withUser("admin").password("password").roles("USER", "ADMIN");
        }
    }
    

    您是否讨论过这一点:?对于
    configureGlobal
    registerGlobal
    方法是否有@autoired注释,或者它们是否由其他方法调用?@Ralph都是自动连接的。另外,我不知道这篇文章和我的问题有什么关系:我知道上面的评论。。但如果名称不重要,那么为什么有三种方法呢?一定有意义。它们在同一个应用程序中吗?不。。它们不在同一应用程序中。。但是,在浏览spring security的示例时,我使用AuthenticationManagerBuilder发现了这三种风格。因此,如果名称不重要,那么
    registerGlobal
    configureGlobal
    方法是相等的,只是因为方法做什么很重要,而不是它的名称。谢谢@ben75。。当你编辑你的答案时,我还通过一些尝试和错误发现,给出任何名称都是有效的。Ben,你能解释一下你的最后一点吗“当你想/需要将你的配置保存在一个类中时,第一个选项是有意义的,但是你的@configuration类已经扩展了其他类(而且你不想实现整个WebSecurity配置器界面)。“@AskerAsker我添加了一个详细的示例来说明我的观点。谢谢Ben。现在我理解了你的最后一点。另外,在我的spring安全示例中,我有一个单独的安全配置文件。