使用 OAuth2 和 Keycloak 在 Jhipster 5.10 上通过 curl 访问数据
Access data by curl on Jhistrer 5.10 with OAuth2 and Keyclock
我对 JHipster 5.1.0 和带有 Keycloak 的 OAuth2 有疑问,如果有人能帮助我,我将不胜感激。
我创建了一个默认的 jhipster 项目(Monolithic,Angular 6,OAuth 2.0 / OIDC Authentication with Keycloak)并且我没有对生成的代码源进行任何更改。我只配置了 PostgreSQL 并生成了一个实体。
我通过“./mvnw”初始化项目,通过 docker-compose -f "src/main/docker/keycloak.yml up" 初始化 Docker 图像,然后我登录并访问了项目的所有数据网络。
到这里一切正常。
但是我需要通过curl访问数据。这是我的问题。
我使用这个生成了令牌:curl -X POST web_app:web_app@localhost:9080/auth/realms/jhipster/protocol/openid-connect/token -d "username=admin&password=admin&grant_type=password&scope=read"
但我无法使用此令牌访问任何数据。
例如,我尝试使用以下多种变体访问我的数据:curl ocalhost:8080/api/organizacaos -H "Authorization: Bearer $GENERATED_TOKEN_HERE"
但永远的回应是:
{
"type" : "https://www.jhipster.tech/problem/problem-with-message",
"title" : "Unauthorized",
"status" : 401,
"detail" : "Full authentication is required to access this resource",
"path" : "/api/organizacaos",
"message" : "error.http.401"
}
有人能帮帮我吗?
谢谢大家。
将 curl 与 JHipster OAuth2 应用程序一起使用并避免处理 cookie 的一种方法是添加可以读取不记名令牌的 ResourceServer 配置。这是在 Ionic and React Native JHipster 客户端中完成的,可以在此处应用。
Copy this file 到您的 Java 配置包中,为授权 header 添加 RequestHeaderRequestMatcher 的重要行如下:
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
....
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
....
.requestMatcher(new RequestHeaderRequestMatcher("Authorization"))
....
然后您可以获得您描述的令牌并向安全 API 发出请求:
# get access token
curl -X POST web_app:web_app@localhost:9080/auth/realms/jhipster/protocol/openid-connect/token -d "username=admin&password=admin&grant_type=password&scope=read"
# request to a secured API
curl localhost:8080/api/account -H "Authorization: Bearer $ACCESS_TOKEN"
我对 JHipster 5.1.0 和带有 Keycloak 的 OAuth2 有疑问,如果有人能帮助我,我将不胜感激。
我创建了一个默认的 jhipster 项目(Monolithic,Angular 6,OAuth 2.0 / OIDC Authentication with Keycloak)并且我没有对生成的代码源进行任何更改。我只配置了 PostgreSQL 并生成了一个实体。
我通过“./mvnw”初始化项目,通过 docker-compose -f "src/main/docker/keycloak.yml up" 初始化 Docker 图像,然后我登录并访问了项目的所有数据网络。
到这里一切正常。
但是我需要通过curl访问数据。这是我的问题。
我使用这个生成了令牌:curl -X POST web_app:web_app@localhost:9080/auth/realms/jhipster/protocol/openid-connect/token -d "username=admin&password=admin&grant_type=password&scope=read"
但我无法使用此令牌访问任何数据。
例如,我尝试使用以下多种变体访问我的数据:curl ocalhost:8080/api/organizacaos -H "Authorization: Bearer $GENERATED_TOKEN_HERE"
但永远的回应是:
{
"type" : "https://www.jhipster.tech/problem/problem-with-message",
"title" : "Unauthorized",
"status" : 401,
"detail" : "Full authentication is required to access this resource",
"path" : "/api/organizacaos",
"message" : "error.http.401"
}
有人能帮帮我吗?
谢谢大家。
将 curl 与 JHipster OAuth2 应用程序一起使用并避免处理 cookie 的一种方法是添加可以读取不记名令牌的 ResourceServer 配置。这是在 Ionic and React Native JHipster 客户端中完成的,可以在此处应用。
Copy this file 到您的 Java 配置包中,为授权 header 添加 RequestHeaderRequestMatcher 的重要行如下:
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
....
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
....
.requestMatcher(new RequestHeaderRequestMatcher("Authorization"))
....
然后您可以获得您描述的令牌并向安全 API 发出请求:
# get access token
curl -X POST web_app:web_app@localhost:9080/auth/realms/jhipster/protocol/openid-connect/token -d "username=admin&password=admin&grant_type=password&scope=read"
# request to a secured API
curl localhost:8080/api/account -H "Authorization: Bearer $ACCESS_TOKEN"