【发布时间】:2020-03-11 19:43:35
【问题描述】:
我的 Spring Boot 配置如下所示:
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilterBefore( new Filter(), UsernamePasswordAuthenticationFilter.class)
.csrf().disable() // Disabled, cause enabling it will cause sessions
.headers()
.frameOptions()
.sameOrigin()
.addHeaderWriter(new XXssProtectionHeaderWriter())
.and()
.authorizeRequests()
.antMatchers("/app/**", "/rest/**").hasAuthority(DefaultPrivileges.ACCESS_TASK)
.anyRequest().permitAll();
我的理解是只有以/app 或/rest 开头的请求会被我的自定义过滤器拦截,但事实证明对根 (http://localhost:8080/context/) 的请求也会被拦截。
我有多个 Spring Security 配置,其他配置如下所示:
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable();
if (taskAppProperties.isRestEnabled()) {
if (restAppProperties.isVerifyRestApiPrivilege()) {
http
.antMatcher("/*-api/**")
.authorizeRequests()
.antMatchers("/*-api/**").hasAuthority(DefaultPrivileges.ACCESS_REST_API)
.and()
.httpBasic();
} else {
http
.antMatcher("/*-api/**")
.authorizeRequests()
.antMatchers("/*-api/**").authenticated()
.and()
.httpBasic();
}
} else {
http
.antMatcher("/*-api/**")
.authorizeRequests()
.antMatchers("/*-api/**").denyAll();
}
谁能帮忙?
【问题讨论】:
-
http.authorizeRequests()表示http.antMatcher("/**").authorizeRequests()。这意味着所有 url 都将被拦截并验证授权。如果您希望您的 http 安全配置仅限于极少数 URL 集,那么您应该选择http.antMatcher("/app/**", "/rest/**").authorizeRequests()或者在高级中您可以选择 this link 中给出的 requestMatcher
标签: spring spring-boot spring-security