如何拒绝没有特定角色的所有请求 - Spring Boot 安全配置
How to deny all requests without specific roles - SpringBoot security config
我有以下 SpringBoot 网络安全配置。
对于授权,我想自动禁止身份验证不包含角色 ADMIN
、SUPER_ADMIN
、CUSTOMER
的所有请求,但这会拒绝所有请求,并且只获取 denyAll
属性在 springExprFilter 中因此它投票拒绝访问。
我的配置中缺少什么?
@EnableWebSecurity
@RequiredArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final MemberDetailsService memberDetailsService;
private final JwtRequestFilter jwtRequestFilter;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(memberDetailsService).passwordEncoder(getPasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers()
.disable()
.csrf()
.disable()
.authorizeRequests()
// permit all request for authentication
.antMatchers("/v1/authenticate")
.permitAll()
.and()
.authorizeRequests()
// permit all request with the following list of roles
// methods will enforce their own authorization logic
.antMatchers("/v1/members/")
.hasAnyAuthority("ADMIN", "CUSTOMER", "SUPER_ADMIN")
.and()
.authorizeRequests()
.anyRequest()
.denyAll()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
我发现 spring 安全配置表达式是正确的。问题是 antmatcher .antMatchers("/v1/members/")
不正确。这意味着将请求与路径 /v1/members/
匹配,这不是预期的功能。
对于任何感兴趣的人,我提出的请求是 GET v1/members/:uuid
。
我应该使用通配符 .antMatchers("/v1/members/**")
来捕获对成员端点的所有请求。
我有以下 SpringBoot 网络安全配置。
对于授权,我想自动禁止身份验证不包含角色 ADMIN
、SUPER_ADMIN
、CUSTOMER
的所有请求,但这会拒绝所有请求,并且只获取 denyAll
属性在 springExprFilter 中因此它投票拒绝访问。
我的配置中缺少什么?
@EnableWebSecurity
@RequiredArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final MemberDetailsService memberDetailsService;
private final JwtRequestFilter jwtRequestFilter;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(memberDetailsService).passwordEncoder(getPasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers()
.disable()
.csrf()
.disable()
.authorizeRequests()
// permit all request for authentication
.antMatchers("/v1/authenticate")
.permitAll()
.and()
.authorizeRequests()
// permit all request with the following list of roles
// methods will enforce their own authorization logic
.antMatchers("/v1/members/")
.hasAnyAuthority("ADMIN", "CUSTOMER", "SUPER_ADMIN")
.and()
.authorizeRequests()
.anyRequest()
.denyAll()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
我发现 spring 安全配置表达式是正确的。问题是 antmatcher .antMatchers("/v1/members/")
不正确。这意味着将请求与路径 /v1/members/
匹配,这不是预期的功能。
对于任何感兴趣的人,我提出的请求是 GET v1/members/:uuid
。
我应该使用通配符 .antMatchers("/v1/members/**")
来捕获对成员端点的所有请求。