Spring 引导 1.5.2.Release - 默认角色前缀后处理器不会删除默认"ROLE_"前缀



在Spring Boot应用程序中,如参考手册第8.3节所述,已定义了DefaultrolespRefixPostProcessor。摘录如下:

public class DefaultRolesPrefixPostProcessor implements BeanPostProcessor, PriorityOrdered {
    @Override 
    public Object postProcessAfterInitialization(final Object bean, final String beanName) { 
        if (bean instanceof DefaultMethodSecurityExpressionHandler) { 
            ((DefaultMethodSecurityExpressionHandler) bean).setDefaultRolePrefix(null); 
        } 
    ....    // if statement for DefaultWebSecurityExpressionHandler
    ....    // if statement for SecurityContextHolderAwareRequestFilter
    return bean;
}

SecurityConfig类覆盖配置(httpsecurity(方法:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    ....
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
        .anyRequest().authenticated();
        http.addFilterBefore(authenticationFilter, BasicAuthenticationFilter.class);
    }
}

但是,任何@Scured方法仍然需要" rooh _"前缀,否则收到403,主要是由于默认的accessDecisionManager的colevervoter返回0

@RestController
public class MyController {
    @Secured("ROLE_XXX")
    public String hello() {
        return "hello";
    }
}

无论如何是否有解决此问题的问题,或者使用" hasauthority"是规避此问题的唯一方法?

您应该从类SecurityConfig

中删除@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // your config
    }
}

如果使用@EnableWebSecurity,则默认配置将完全关闭

关闭默认的Web应用程序安全配置 完全可以添加

的豆子

在您的情况下不需要。但是,如果您想像自己一样使用它,则必须启用方法。

所以我认为您的帖子处理器根本不使用,如果您没有手动到达尚未发布的地方。

最新更新