Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过KeyClope保护单个资源不受Spring引导的影响_Java_Spring_Spring Boot_Authorization_Keycloak - Fatal编程技术网

Java 通过KeyClope保护单个资源不受Spring引导的影响

Java 通过KeyClope保护单个资源不受Spring引导的影响,java,spring,spring-boot,authorization,keycloak,Java,Spring,Spring Boot,Authorization,Keycloak,我有一个非常简单的Spring Boot应用程序,其资源位于/repositories和/persons 这是我的build.gradle文件 plugins { id 'org.springframework.boot' version '2.4.0' id 'io.spring.dependency-management' version '1.0.10.RELEASE' id 'java' } // use java 11 until keycloak is fi

我有一个非常简单的Spring Boot应用程序,其资源位于
/repositories
/persons

这是我的
build.gradle
文件

plugins {
    id 'org.springframework.boot' version '2.4.0'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

// use java 11 until keycloak is fixed
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom "org.keycloak.bom:keycloak-adapter-bom:12.0.1"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.keycloak:keycloak-spring-boot-starter'

    implementation 'org.flywaydb:flyway-core'
    runtime 'org.postgresql:postgresql'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

...
...
这是我的
SecurityConfig.java
文件

plugins {
    id 'org.springframework.boot' version '2.4.0'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

// use java 11 until keycloak is fixed
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom "org.keycloak.bom:keycloak-adapter-bom:12.0.1"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.keycloak:keycloak-spring-boot-starter'

    implementation 'org.flywaydb:flyway-core'
    runtime 'org.postgresql:postgresql'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

...
...
@keydepose配置
公共类SecurityConfig扩展了KeyDopperWebSecurity配置适配器{
@自动连线
public void configureGlobal(AuthenticationManagerBuilder身份验证){
var keydapertificationprovider=keydapertificationprovider();
keydovetAuthenticationProvider.setGrantedAuthoritiesMapper(新的SimpleAuthorityMapper());
auth.authenticationProvider(keydaperthenticationProvider);
}
@豆子
@凌驾
受保护的SessionAuthenticationStrategy SessionAuthenticationStrategy(){
返回新的RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
super.configure(http);
http.authorizeRequests()
.antMatchers(“/人*”)
.hasRole(“用户”)
.anyRequest()
.permitAll();
}
@豆子
public keydeposeConfigResolver keydeposeConfigResolver(){
返回新的keydepospringbootconfigResolver();
}
}
这是我的
application.yaml
文件

plugins {
    id 'org.springframework.boot' version '2.4.0'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

// use java 11 until keycloak is fixed
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom "org.keycloak.bom:keycloak-adapter-bom:12.0.1"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.keycloak:keycloak-spring-boot-starter'

    implementation 'org.flywaydb:flyway-core'
    runtime 'org.postgresql:postgresql'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

...
...
spring:
数据源:
url:jdbc:postgresql://localhost:5432/postgres
用户名:约翰
密码:约翰
钥匙斗篷:
验证服务器url:http://localhost:8081/auth/
领域:我的领域
资源:myclient
资格证书:
秘密:45d43bd6-5ab9-476c-83c8-67bd203a78ee
这一切都在我的本地机器上,keybeave和Postgres都是通过docker compose启动的


版本:“3.1”
卷数:
postgres_数据:
司机:本地
服务:
db:
图片:“博士后:13.1”
端口:
- "5432:5432"
环境:
POSTGRES_DB:POSTGRES
博士后用户:john
POSTGRES_密码:john
博士后:
图片:“博士后:13.1”
卷数:
-postgres_数据:/var/lib/postgresql/data
环境:
博士后:钥匙斗篷
POSTGRES_用户:密钥斗篷
POSTGRES_密码:PASSWORD
钥匙斗篷:
图片:quay.io/keydove/keydove:12.0.1
环境:
DB_供应商:POSTGRES
数据库地址:postgres
DB_数据库:密钥斗篷
DB_用户:密钥斗篷
DB_模式:公共
DB_密码:密码
密钥斗篷用户:管理员
密码:Pa55w0rd
端口:
- 8081:8080
取决于:
-博士后
我在keybeave中有用户
zemirco
one
,他们都有
user
角色。这一切都运行得很好,路径
/persons
受到保护,而
/repositories
对所有人开放

这是我的问题!我想保护个人资源,例如
/person/1
。我无法让它工作:(我已经尝试了好几天了,但没有运气。这是我的钥匙斗篷设置

这是我的政策

这是资源

这是许可证

它在keydove的评估过程中都能工作。但是它不能直接在我的Spring Boot应用程序中工作。我仍然可以访问
/person/1
作为
zemirco
,尽管用户
one
应该只有访问权限

我现在有点迷路了。你有什么想法吗

多谢各位


编辑2021年1月10日

谢谢大家的回答,但我仍然认为这是可行的,不需要我(应用程序方面)付出太多额外的努力。我特别关注政策执行者

PEP负责从KeyClope服务器强制执行访问决策,这些决策是通过评估与受保护资源关联的策略做出的。它在应用程序中充当筛选器或拦截器,以检查是否可以根据许可证满足对受保护资源的特定请求这些决定所给予的支持


这听起来和我想做的一模一样。不幸的是,我无法让它工作。我只是认为我的配置是错误的,或者可能不完整。我将为这个问题添加一个悬赏。

KeyClope以拥有访问管理解决方案中最差的策略/访问工具而闻名,但是,您描述的问题似乎有点奇怪

KeyClope是一个访问管理工具,从策略的角度来看,它提供决策,但不强制执行决策,因此,由于策略评估工作如预期,您需要做的就是从spring应用程序对评估点进行api调用,如果是200,则用户将访问,如果不是,则页面未显示

另一种解决方案是使用预先制定的策略实施点,apache/nginx spring都有自己的custon策略实施点

一些有用的链接:


在KeyClope中AuthZ是PITA,我认为不应该使用它。有更好的解决方案(例如)。只要遵循原则:“不要做愚蠢的事情!”

其次,不要混淆AuthN和AuthZ。 仅仅因为在keybeave中配置了一些AuthZ策略,这并不意味着它们可以“自动且神奇地”使用。 如果您使用任何适配器(无论是Spring Security还是某些KC自己的适配器)进行身份验证,那么它们只执行“OIDC”,而OIDC是关于AuthN的,而不是关于AuthZ的

如果要在应用程序中调整或使用AuthZ策略和规则,则必须将它们与身份验证分开查询。 keydape为此提供了
keydape-authz客户端
,该客户端记录在这里:

它终于工作了:)我很高兴

这就是我所做的

首先,我使用了我的keydove客户端安装中的
keydove.json
文件。开始时,我试图将所有配置放入我的
应用程序.yaml
keydape.json
文件必须位于
src/main/webapp/WEB-INF/keydape.json
。当您使用JSON文件时,我