小编典典

Spring Security配置-HttpSecurity与WebSecurity

spring-boot

我只需要了解Spring Security Configuration中的内容。使用下面的示例…

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .httpBasic()
            .and()
            .authorizeRequests().antMatchers("/secret/**").authenticated()
            .and()
            .authorizeRequests().antMatchers("/**").permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**");
    }

}

configure(WebSecurity web)方法的目的是什么?

我不能只是添加/resources/**configure(HttpSecurity http)方法,在这条线.authorizeRequests().antMatchers("/**", "/resources/**").permitAll(); 应该不是它的工作相同,即允许所有的请求/resources/**没有任何身份验证?


阅读 3924

收藏
2020-05-30

共1个答案

小编典典

WebSecurity ignoring()方法的常规用法 省略了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();
}

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

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

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

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

2020-05-30