基于Spring安全表达式的访问控制-引用bean

问题描述 投票:0回答:1

我尝试通过在 Web 安全表达式中引用 bean 方法来在 Spring 安全中使用基于表达式的访问控制。例如我有这样的东西

@Component
public class AuthorizationChecker {
    public boolean check(Authentication authentication, HttpServletRequest request) {
        return true;
    }
}

然后像这样的安全配置方法

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and().anonymous().disable().authorizeRequests().antMatchers(HttpMethod.POST, "/**")
            .access("@authorizationChecker(authentication, request)").and().oauth2ResourceServer().jwt();
}

但是,当启动 Spring Boot 应用程序时,我遇到解析异常。深入研究 Spring 代码,我可以看到它解析失败并显示以下消息

EL1041E: 解析有效表达式后,表达式中还有更多数据:'lparen(()'

有什么想法吗?据我所知,它与 Spring Security 文档中给出的语法相匹配。

https://docs.spring.io/spring-security/reference/5.7/servlet/authorization/expression-based.html#el-access-web-beans

java spring spring-boot spring-security
1个回答
0
投票

你应该尝试这个,注意

check
方法的显式调用:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and()
        .anonymous().disable()
        .authorizeRequests().antMatchers(HttpMethod.POST, "/**")
            .access("@authorizationChecker.check(authentication, request)")
        .and().oauth2ResourceServer().jwt();
}
© www.soinside.com 2019 - 2024. All rights reserved.