Java HttpSecurity、WebSecurity和AuthenticationManagerBuilder
谁能解释一下什么时候覆盖Java HttpSecurity、WebSecurity和AuthenticationManagerBuilder,java,spring,spring-boot,spring-mvc,spring-security,Java,Spring,Spring Boot,Spring Mvc,Spring Security,谁能解释一下什么时候覆盖configure(HttpSecurity),configure(WebSecurity)和configure(AuthenticationManagerBuilder) 配置(AuthenticationManagerBuilder)用于通过允许轻松添加AuthenticationProviders来建立身份验证机制:例如,以下定义了内置“用户”和“管理员”登录的内存中身份验证 public void configure(AuthenticationManagerBu
configure(HttpSecurity)
,configure(WebSecurity)
和configure(AuthenticationManagerBuilder)
配置(AuthenticationManagerBuilder)用于通过允许轻松添加AuthenticationProviders来建立身份验证机制:例如,以下定义了内置“用户”和“管理员”登录的内存中身份验证
public void configure(AuthenticationManagerBuilder auth) {
auth
.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("USER")
.and()
.withUser("admin")
.password("password")
.roles("ADMIN","USER");
}
configure(HttpSecurity)允许根据选择匹配在资源级别配置基于web的安全性-例如,下面的示例将以/admin/开头的URL限制为具有管理员角色的用户,并声明任何其他URL都需要成功验证
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
}
配置(Web安全)用于影响全局安全的配置设置(忽略资源、设置调试模式、通过实现自定义防火墙定义拒绝请求)。例如,出于身份验证目的,以下方法将导致忽略以/resources/开头的任何请求
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}
AuthenticationManagerBuilder
extends AbstractConfiguredSecurityBuilder<AuthenticationManager,AuthenticationManagerBuilder>
implements ProviderManagerBuilder<AuthenticationManagerBuilder>
您可以参考以下链接了解更多信息Web安全的一般用法
忽略()
方法忽略了Spring安全性,并且Spring安全性的任何功能都不可用。
Web安全性基于HttpSecurity之上
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**")
.antMatchers("/publics/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/publics/**").hasRole("USER") // no effect
.anyRequest().authenticated();
}
上面示例中的WebSecurity允许Spring忽略/resources/**
和/publics/**
。因此,HttpSecurity中的.antMatchers(“/publics/**”).hasRole(“用户”)
是不被考虑的
这将从安全过滤器链中完全忽略请求模式。
请注意,与此路径匹配的任何内容都不会应用任何身份验证或授权服务,并且可以自由访问
configure(HttpSecurity)
允许根据选择匹配在资源级别配置基于web的安全性-例如,下面的示例将以/admin/
开头的URL限制为具有管理员角色的用户,并声明需要成功验证任何其他URL
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
}
configure(WebSecurity)
用于影响全局安全性的配置设置(忽略资源、设置调试模式、通过实现自定义防火墙定义拒绝请求)。例如,出于身份验证目的,以下方法将导致忽略以/resources/
开头的任何请求
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}
AuthenticationManagerBuilder
extends AbstractConfiguredSecurityBuilder<AuthenticationManager,AuthenticationManagerBuilder>
implements ProviderManagerBuilder<AuthenticationManagerBuilder>
回答得好,尼克。使用spring-security-config-5.0.3(spring boot 2.0.0随附),我找不到方法
http.authorizeUrls()
,可能它不久前被重命名为http.authorizeRequests()
。我知道这很旧,但这里的最佳实践是什么?我发现了调用http.antMatchers(“/foo”).permitAll()”的configure(HttpSecurity http)方法实现的示例,这似乎相当于在configure(WebSecurity web)中调用web.ignering().antMatchers(“/foo”)方法。回答很好。我想知道我们是否需要调用HttpSecurity上的permitAll?难道我们不能忽略所有打开的url,比如使用WebSecurity的/register或/login吗?那么为什么所有教程或答案都使用HttpSecurity.permitAll for/register和/login,而WebSecurity.ingore for/publics of/resources?回答很好。我想知道我们是否是否需要调用HttpSecurity上的permitAll?我们不能忽略所有打开的url,如使用WebSecurity的/register或/login?那么为什么所有教程或答案都使用HttpSecurity.permitAll表示/register和/login,而WebSecurity.ingore表示/publics of/resources?