【问题标题】:Spring Boot Security configuring HttpSecuritySpring Boot Security 配置 HttpSecurity
【发布时间】:2021-06-29 21:17:06
【问题描述】:

为了授权请求,我们覆盖了configure(HttpSecurity) 方法,我们在该方法中提到了访问我们想要哪个角色的 API。但是我们没有提到的 API 无需登录即可访问。为什么会出现这种行为?

我没有为 API 写 permitAll() 为什么这是默认行为?

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/admin").hasRole("ADMIN")
                .antMatchers("/user").hasAnyRole("ADMIN", "USER")
                .antMatchers("/").hasRole("USER")
                .and()
            .formLogin();
    }
}

在此代码中admin API 可以通过管理员角色访问,user API 可以通过管理员和用户角色访问,/ API 可以通过用户角色访问并且还有一个 API @987654327 @ 我没有提到,我可以在没有登录的情况下访问它。

问题是我怎么没有为学生 API 编写 permitAll() 方法。

【问题讨论】:

    标签: java spring spring-boot security spring-security


    【解决方案1】:

    将此行:.antMatchers("/").hasRole("USER") 更改为:.anyRequest().hasRole("USER")

    【讨论】:

      【解决方案2】:

      仅检查已配置的 URL。如果不配置 URL,则根本不会检查。

      因此,您应该添加一个配置,检查所有其他 URL,请参阅 Spring Security Reference

      11.2.使用 FilterSecurityInterceptor 授权 HttpServletRequest

      [...]

      protected void configure(HttpSecurity http) throws Exception {
          http
              // ...
              .authorizeRequests(authorize -> authorize                                1                     
                  .mvcMatchers("/resources/**", "/signup", "/about").permitAll()       2  
                  .mvcMatchers("/admin/**").hasRole("ADMIN")                           3  
                  .mvcMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") 4  
                  .anyRequest().denyAll()                                              5  
              );
      }
      

      1 指定了多个授权规则。 每个规则都按照它们声明的顺序进行考虑。

      2 我们指定了任何用户都可以访问的多个 URL 模式。 具体来说,如果 URL 以“/resources/”开头、等于“/signup”或等于“/about”,则任何用户都可以访问请求。

      3 任何以“/admin/”开头的 URL 都将被限制为具有“ROLE_ADMIN”角色的用户。 您会注意到,由于我们正在调用 hasRole 方法,因此我们不需要指定“ROLE_”前缀。

      4 任何以“/db/”开头的 URL 都要求用户同时拥有“ROLE_ADMIN”和“ROLE_DBA”。 您会注意到,由于我们使用的是 hasRole 表达式,因此我们不需要指定“ROLE_”前缀。

      5 任何尚未匹配的 URL 都将被拒绝访问。 如果您不想意外忘记更新授权规则,这是一个很好的策略。

      【讨论】:

        【解决方案3】:

        为了保护未在configure方法中配置的API,必须添加行.anyRequest().denyAll()

         @Override
            protected void configure(HttpSecurity http) throws Exception {
                http
                    .authorizeRequests()
                        .antMatchers("/admin").hasRole("ADMIN")
                        .antMatchers("/user").hasAnyRole("ADMIN", "USER")
                        .antMatchers("/").hasRole("USER")
                        .anyRequest().denyAll()
                        .and()
                    .formLogin();
            }
        }
        

        对每个请求进行保护而不在配置方法中进行配置

        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests(authorize -> authorize
                    .anyRequest().authenticated()
                );
        }
        

        【讨论】:

          猜你喜欢
          • 2020-03-04
          • 2017-10-18
          • 2016-08-08
          • 1970-01-01
          • 2016-03-14
          • 2017-09-21
          • 2014-07-25
          • 2021-07-04
          • 2013-11-01
          相关资源
          最近更新 更多