基于自定义属性的 Spring Boot SOAP Web 服务的 Keycloak 身份验证

Keycloak authentication for a Spring Boot SOAP webservice based on custom atribute

我们正在使用 Keycloak 来保护 Spring Boot SOAP 应用程序。我们希望根据 keycloak 中用户属性的信息来处理对应用程序的授权,而不是标准的 'roles based' 访问权限。

例如,我们可以使用具有以下配置的标准 keycloak spring 启动适配器:

keycloak.security-constraints[0].authRoles[0]=special-role
keycloak.security-constraints[0].securityCollections[0].patterns[0]=/service/*

这将确保只有 'special-role' 的用户才能访问该服务。但是是否可以编写一个自定义过滤器来查看用户属性的值而不是她的角色?

我们得出的解决方案是使用 Keycloak 的 Spring 安全适配器,然后指定我们自己的 bean 来检查令牌:

@Component
public class WebSecurity {
    public boolean check(Authentication authentication) throws Exception {
        boolean result = false;
        ...
        return result;
    }
}

// In the web security configuration
        http
                .authorizeRequests()
                .antMatchers(HttpMethod.POST, SOME_URL)
                .access("@webSecurity.check(authentication)")