Nai*_*der 29 java spring spring-security
我有三个代码片段都在做同样的事情:创建内存中的身份验证.那么它如何影响在不同的方法名称中定义它?
第一:
public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER","ADMIN");
}
}
Run Code Online (Sandbox Code Playgroud)
第二个:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
Run Code Online (Sandbox Code Playgroud)
第三个:
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
Run Code Online (Sandbox Code Playgroud)
第四:
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
}
Run Code Online (Sandbox Code Playgroud)
更新1: 我还想补充一点:
configure()方法存在于WebSecurityConfigurerAdapter类中,而其他类不存在.
更新2:
我将示例项目中的方法重命名为以下内容,令我惊讶的是它正在对用户进行工作和身份验证.
你把它命名为什么,它的工作原理
@Autowired
public void anyMethodName(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
}
Run Code Online (Sandbox Code Playgroud)
ben*_*n75 43
实际上,您只有2种不同的选择.
选项1:仅使用注释(它涵盖您的示例1,3和4 - 请注意您未在样本中包含相关注释)
registerGlobal,configureGlobal,configureGlobalSecurity是做事的完全相同的方式.您可以根据自己的喜好命名方法.唯一的限制是:
@AutowiredAuthenticationManagerBuilder)(因为你可以看到方法的名称并不重要,这就是为什么你在谷歌搜索代码样本时发现了这么多不同的方法名称)
以下是它的外观示例:
@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");
}
...
}
Run Code Online (Sandbox Code Playgroud)
选项2:使用注释+方法覆盖(它涵盖了您的示例2)
覆盖configure是WebSecurityConfigurerAdapter(或任何@Configuration类实现WebSecurityConfigurer)的子类中的方便方法,但它具有与其他选项相同的效果.
这只是品味/编程风格的问题,因为两种方法都具有相同的效果.
当您希望/需要将配置保留在单个类中时,第一个选项有意义,但您的@Configuration类已经扩展了其他类(并且您不希望实现整个WebSecurityConfigurer接口).
让我们更详细地解释一下我的最后一点.Spring提供了许多适配器类,您可以扩展它们以加速Spring配置的开发.
举个例子,我们来看一个常用的适配器:WebMvcConfigurerAdapter.您将从一个非常简单的配置开始,如下所示:
@EnableWebMvc
@Configuration
@ComponentScan({ "com.company.mypackage" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {
}
Run Code Online (Sandbox Code Playgroud)
这里有什么重要的:你的类已经扩展了一个Adapter类,所以你不能扩展另一个类
现在,您需要添加安全配置.您可以选择将其包含在现有SpringWebConfig配置类中,还是创建新的特定于安全性的配置类.以下是两种方法的示例:
1)单个@Configuration类方法
这里需要注意的重要事项:SpringWebConfig 扩展了WebMvcConfigurerAdapter + @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");
}
}
Run Code Online (Sandbox Code Playgroud)
2)特定安全性@Configuration类
这里需要注意的重要事项是:MySecurityConfig 扩展了WebSecurityConfigurerAdapter
保持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");
}
}
Run Code Online (Sandbox Code Playgroud)