Java jhipster/spring密钥斗篷集成-设置自定义重定向\u uri
我有一个spring/jhipster应用程序,有一个按钮可以登录到keydepeat。当我单击login按钮时,它会将我带到KeyClope,URL看起来像Java jhipster/spring密钥斗篷集成-设置自定义重定向\u uri,java,spring,spring-boot,jhipster,keycloak,Java,Spring,Spring Boot,Jhipster,Keycloak,我有一个spring/jhipster应用程序,有一个按钮可以登录到keydepeat。当我单击login按钮时,它会将我带到KeyClope,URL看起来像 https://keycloak.mydomain.com/auth/realms/my-realm/protocol/openid-connect/auth?response_type=code&client_id=myclient&redirect_uri=http://localhost:8080/login/oau
https://keycloak.mydomain.com/auth/realms/my-realm/protocol/openid-connect/auth?response_type=code&client_id=myclient&redirect_uri=http://localhost:8080/login/oauth2/code/oidc
在上面,我想更改重定向uri=http://localhost:8080/login/oauth2/code/oidc 去别的地方
因此,我在src/main/java/./config/AuthorizationServerConfig.java中添加了以下代码:
@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Bean
PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("myclient")
.secret(passwordEncoder().encode("mysecret"))
.scopes("resource:read")
.authorizedGrantTypes("authorization_code")
.redirectUris("https://new-url-I-want.com");
}
}
但是,即使进行了上述更改,重定向uri也没有任何更改。有人知道这是为什么吗
关于我为什么做出这些改变的更多信息:
通过OIDC的授权代码流,服务提供商(在本例中为“我的网站”)向身份提供商keydepot提供URI,以便在成功身份验证后将用户重定向回。但是,根据Spring文档,在默认配置下,虽然授权代码流在技术上是允许的,但它没有完全配置:
文档继续声明OAuth2引导不支持将重定向URI配置为属性 — 比如,除了客户id和客户机密之外。所以
要添加重定向URI,需要使用InMemoryClientDetailsService或JdbcClientDetailsService指定客户端:
您可以通过添加以下属性和src/main/resources/config/application.yml来添加另一个重定向uri
security:
oauth2:
client:
provider:
oidc:
issuer-uri: http://localhost:9080/auth/realms/jhipster
registration:
oidc:
client-id: web_app
client-secret: web_app
redirect-uri: http://localhost:8080/my-custom-redirect
此外,您必须在SecurityConfiguration中为oauth2登录配置自定义重定向uri,如下所示
...
.and()
.oauth2Login()
.redirectionEndpoint()
.baseUri("/my-custom-redirect")
.and()
.and()
.oauth2ResourceServer()
...
在方法SecurityConfiguration.configure中添加以下行 即:
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { ...
@Override
public void configure(WebSecurity web) { ... }
@Override
public void configure(HttpSecurity http) throws Exception {
...
.and()
.authorizeRequests()
.antMatchers("/api/auth-info").permitAll()
.antMatchers("/api/**").authenticated()
.antMatchers("/management/health").permitAll()
.antMatchers("/management/info").permitAll()
.antMatchers("/management/prometheus").permitAll()
.antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN)
.and()
.oauth2Login()
.defaultSuccessUrl("/url_to_redirect") // <--- HERE
}
有一个属性可以更改重定向uri,但我认为您需要该重定向uri才能成功登录。如果您真的需要/想要更改重定向uri,您可以尝试将该属性添加到src/main/resources/config/application.yml spring.security.oauth2.client.registration.oidc.redirect-uri中。官方的引导文档可以在这里找到。我需要在上面的重定向uri中添加额外的内容吗?我试着让它使用我网站的URL,在登录到Key斗篷后,它会将我重定向回我网站的主页,但似乎什么都没有发生。然后我将它改为htp s://mysite.net/my-added-url/login/oauth2/code/oidc,但它找不到该路由,即使它应该重定向回那里,所以我将尝试进一步处理该URI。我将尝试再次检查,但基本上这是我使用的配置。谢谢@atomfree。我之所以需要这个重定向,是因为我的应用程序位于一个其他应用程序共享的URL上,而这个主URL就是负载均衡器。然后我的应用程序就存在于myurl.com/myapp上。但是在我登录到keydape之后,它会把我带到myurl.com/login/oauth。。。而不是myurl.com/myapp/login/oauth。。
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { ...
@Override
public void configure(WebSecurity web) { ... }
@Override
public void configure(HttpSecurity http) throws Exception {
...
.and()
.authorizeRequests()
.antMatchers("/api/auth-info").permitAll()
.antMatchers("/api/**").authenticated()
.antMatchers("/management/health").permitAll()
.antMatchers("/management/info").permitAll()
.antMatchers("/management/prometheus").permitAll()
.antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN)
.and()
.oauth2Login()
.defaultSuccessUrl("/url_to_redirect") // <--- HERE
}