HttpSecurity,WebSecurity和AuthenticationManagerBuilder

use*_*241 95 java spring spring-mvc spring-security

任何人都可以解释何时覆盖configure(HttpSecurity),configure(WebSecurity)configure(AuthenticationManagerBuilder)

小智 118

configure(AuthenticationManagerBuilder)用于通过允许轻松添加AuthenticationProvide来建立身份验证机制:例如,以下内容使用内置的"user"和"admin"登录定义内存中身份验证.

public void configure(AuthenticationManagerBuilder auth) {
    auth
        .inMemoryAuthentication()
        .withUser("user")
        .password("password")
        .roles("USER")
    .and()
        .withUser("admin")
        .password("password")
        .roles("ADMIN","USER");
}
Run Code Online (Sandbox Code Playgroud)

configure(HttpSecurity)允许基于选择匹配在资源级别配置基于Web的安全性 - 例如,下面的示例将以/ admin /开头的URL限制为具有ADMIN角色的用户,并声明需要任何其他URL成功通过身份验证

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/admin/**").hasRole("ADMIN")
        .anyRequest().authenticated()
}
Run Code Online (Sandbox Code Playgroud)

configure(WebSecurity)用于影响全局安全性的配置设置(忽略资源,设置调试模式,通过实现自定义防火墙定义来拒绝请求).例如,以下方法将导致以/ resources /开头的任何请求被忽略以进行身份​​验证.

public void configure(WebSecurity web) throws Exception {
    web
        .ignoring()
        .antMatchers("/resources/**");
}
Run Code Online (Sandbox Code Playgroud)

有关Spring Security Java Config Preview的更多信息,请参阅以下链接:Web Security

  • 我知道这很旧,但是这里的最佳做法是什么?我已经找到了调用http.antMatchers(“ / foo”)。permitAll()“的configure(HttpSecurity http)方法实现的示例,这似乎等同于configure(WebSecurity)中调用web.ignoring()。antMatchers(” / foo“)网络)方法。 (3认同)
  • 尼克回答不错.使用spring-security-config-5.0.3(带有spring-boot 2.0.0),我找不到方法`http.authorizeUrls()`,也许它被重命名为`http.authorizeRequests()`前一阵子. (2认同)

Pat*_*mil 5

WebSecurityignoring()方法的一般使用会忽略 Spring Security,并且 Spring Security 的所有功能都将不可用。WebSecurity 基于 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();
}
Run Code Online (Sandbox Code Playgroud)

上面示例中的 WebSecurity 让 Spring 忽略/resources/**/publics/**。因此.antMatchers("/publics/**").hasRole("USER")在 HttpSecurity 中是不考虑的

这将完全从安全过滤器链中省略请求模式。请注意,与此路径匹配的任何内容都将不会应用任何身份验证或授权服务,并且可以自由访问。

configure(HttpSecurity)允许基于选择匹配在资源级别配置基于 Web 的安全性- 例如,下面的示例将以 开头的 URL 限制为/admin/具有ADMIN 角色的用户,并声明任何其他 URL 需要成功验证。

configure(WebSecurity)用于影响全局安全性的配置设置(忽略资源、设置调试模式、通过实现自定义防火墙定义拒绝请求)。例如,以下方法将导致任何以 开头的请求/resources/都被忽略以进行身份​​验证

AuthenticationManagerBuilder
extends AbstractConfiguredSecurityBuilder<AuthenticationManager,AuthenticationManagerBuilder>
implements ProviderManagerBuilder<AuthenticationManagerBuilder>
Run Code Online (Sandbox Code Playgroud)

SecurityBuilder 用于创建一个AuthenticationManager. 允许轻松构建内存身份验证、LDAP 身份验证、基于 JDBC 的身份验证、添加 UserDetailsS​​ervice 和添加 AuthenticationProvider 的.

@Override
     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); 
        auth.userDetailsService(customUserDetailService).passwordEncoder(new BCryptPasswordEncoder());
     }
Run Code Online (Sandbox Code Playgroud)