我正在使用从数据库中获取的角色和权限来实现 spring 安全性。它仅在映射 url 的情况下工作正常。对于未映射的 url,我没有得到 403。下面是我的 http 配置。任何帮助表示赞赏。
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfigure extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserDetailsService);
}
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
List<Role> roleModules = roleActionRepository.findAll();
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry urlRegistry = httpSecurity.authorizeRequests();
httpSecurity.csrf().disable();
urlRegistry.antMatchers(
"/authenticate",
"/public/**",
"/common/**"
).permitAll();
roleModules.forEach(roleAction -> {
urlRegistry.antMatchers(HttpMethod.valueOf(module.getType()), module.getName()).hasAuthority(roleAction.getName());
});
urlRegistry.anyRequest().authenticated()
.and().csrf().disable().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
假设我有一个从数据库中获取的 URL 映射 /employee/**。为此,我的代码工作正常。 但是可以说我有另一个像 /user/** 这样的 url,它没有为任何角色配置。因此,理想情况下,可以访问该端点。但是我可以在没有角色分配的情况下访问该点。那么我如何才能防止这件事。
您还可以查看角色映射的屏幕截图
每当添加称为第四次缩进的 urlRegistry.anyRequest().authenticated()
时。