在Spring Security 6中,.not()的替代品是什么?

y3bcpkx1  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(74)

在Spring Security 5中,您可以像这样配置HttpSecurity

http.antMatcher("/**")
    .authorizeRequests()
        .antMatchers("/").not().hasRole("INVITED")
        .antMatchers("/forgot-password").not().authenticated()

字符串
在Spring Security 6中,新系统中不再有任何not()方法:

http.securityMatcher("/**")
    .authorizeHttpRequests((authorize) -> authorize
        .requestMatchers("/").???.hasRole("INVITED")
        .requestMatchers("/forgot-password").???.authenticated()
    )


在Spring Security 6中,像这样否定授权表达式的方法是什么?

nnvyjq4y

nnvyjq4y1#

据我所知,没有替代品,但你可以自己实现一个:

public static <T> AuthorizationManager<T> not(AuthorizationManager<T> manager) {
    return (authentication, object) -> {
        AuthorizationDecision decision = manager.check(authentication, object);
        if (decision == null) {
            return null;
        } else {
            return new AuthorizationDecision(!decision.isGranted());
        }
    };
}

字符串
然后可以将其与来自org.springframework.security.authorization的其他静态导入结合使用:

http.securityMatcher("/**")
    .authorizeHttpRequests((authorize) -> authorize
        .requestMatchers("/").access(not(hasRole("INVITED")))
        .requestMatchers("/forgot-password").access(not(authenticated()))
    )

相关问题