组合 configure() + hasAuthority() 和 PreAuthorize

Combine configure() + hasAuthority() and PreAuthorize

我这样配置我的应用程序:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .anonymous().principal("guest").authorities("GUEST_ROLE")
    .and()
    .authorizeRequests()
            .antMatchers("/**").permitAll()
            .antMatchers("/admin/**").hasAuthority("ADMIN")
    .and()
    .formLogin();
}

我在“/admin/”中的控制器之一定义为

@PreAuthorize("@securityService.hasPermission('SALEPOINT', #id)")

但是如果用户不是“ADMIN”但同意我的securityService.hasPermission,则允许​​。实际上,我想检查 hasAuthority("ADMIN")。

从配置中删除 .antMatchers("/admin/**").hasAuthority("ADMIN"),因为它已被注解 @PreAuthorize

覆盖

然后在@PreAuthorize注解中添加如下逻辑,使

@PreAuthorize("hasAuthority('ADMIN') AND @securityService.hasPermission('SALEPOINT', #id)")