【问题标题】:Users are able to access all endpoints after setting antMachers in Spring Security在 Spring Security 中设置 antMachers 后,用户可以访问所有端点
【发布时间】:2020-06-29 12:17:38
【问题描述】:

我正在开发一个spring-boot应用,它的spring安全配置如下:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter  {
@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
            .authorizeRequests()
            .antMatchers("/actuator/**", "/login*", "/logout*")
            .permitAll();

        httpSecurity
            .cors().and()
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/taas/v1/**").hasRole("admin")
            .antMatchers("/taas/v1/teams", "/taas/v1/profiles", "/taas/v1/tests/summary").hasRole("tester")
            .antMatchers( "/taas/v1/teams", "/taas/v1/tests/summary").hasRole("user")
            .anyRequest().authenticated()
            .and()
            .exceptionHandling().accessDeniedHandler(customAccessDeniedHandler)
            .and()
            .httpBasic()
            .and()
            .formLogin()
            .successHandler(customAuthenticationSuccessHandler)
            .failureHandler(customAuthenticationFailureHandler)
            .and()
            .logout()
                .logoutSuccessHandler(customLogoutSuccessHandler())
                .invalidateHttpSession(true)
                .deleteCookies("JSESSIONID");
    }
}

即使我已经为每个角色设置了 url 模式。所有用户都可以访问 antMatchers() 中提到的所有端点。角色为user 的用户不应访问/taas/v1/profiles。但是当我尝试通过以user 登录来访问该端点时,我收到了响应,但预期的响应是403 forbidden

我请求某人为我提供解决方法。

【问题讨论】:

标签: spring spring-boot spring-security


【解决方案1】:

我通过对我的 antMatchers() 进行一些小改动解决了这个问题。下面是修改后的代码。

主要问题是 antMatcher() 模式不能包含上下文路径,参见Spring security antMatcher does not work

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter  {
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {

        httpSecurity
            .cors()
                .and()
            .authorizeRequests()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/profiles").hasAnyRole("TESTER")
                .antMatchers( "/teams", "/tests/summary").hasAnyRole("USER", "TESTER", "ADMIN")
                .anyRequest().authenticated()
                .and().csrf().disable()
            .exceptionHandling()
                .accessDeniedHandler(customAccessDeniedHandler)
                .and()
            .httpBasic()
                .and()
            .formLogin()
                .successHandler(customAuthenticationSuccessHandler)
                .failureHandler(customAuthenticationFailureHandler)
                .and()
            .sessionManagement()
                .invalidSessionUrl("/invalidSession.html")
                .maximumSessions(1).sessionRegistry(sessionRegistry()).and()
                .sessionFixation().none()
                .and()
            .logout()
                .logoutSuccessHandler(customLogoutSuccessHandler())
                .invalidateHttpSession(true)
                .deleteCookies("JSESSIONID");
    }
}

【讨论】:

  • 这不起作用,因为USER 将无法访问这些 URL。当它们匹配时,唯一允许的角色是TESTER。您应该使用hasAnyRole("USER", "TESTER") 作为双方都可以访问的 URL。
  • @M. Deinum 对不起,这是错字。我做了改变。 :)
【解决方案2】:

请验证您共享的代码,因为正如您所提到的。具有角色用户的user 不应访问/ptaas/v1/profiles。但是当我尝试通过以用户身份登录来访问该端点时。

您的映射表明您没有按照给定的方式配置对user role 的访问权限。

.antMatchers( "/taas/v1/teams", "/taas/v1/tests/summary").hasRole("user")

根据您的 cmets,它应该是 .antMatchers( "/taas/v1/teams", "/taas/v1/tests/summary", "/ptaas/v1/profiles").hasRole("user")

【讨论】:

  • 因此` .antMatchers( "/taas/v1/teams", "/taas/v1/tests/summary").hasRole("user") ` 在我看来是正确的。
  • 哦!对不起,我误解了,当用户无权访问 /profiles 时,很明显会被禁止 403,因为用户无权访问 url 端点
  • 精确。但我没有得到 403。
  • 好的,你能给我你的代码吗,我想在我的 IntelliJ 上测试一下
  • 对不起!目前这是不可能的。你认为我的 antMatchers() 设置正确吗?
猜你喜欢
  • 2015-06-08
  • 2020-07-19
  • 1970-01-01
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 2020-11-05
  • 2011-07-28
相关资源
最近更新 更多