我在Spring Boot应用程序中使用Keycloak
,但我不知道如何提供自己的重定向uri
这是我的配置
keycloak:
use-resource-role-mappings: true
realm: customer-realm
resource: web-frontend
token-minimum-time-to-live: 30
principal-attribute: preferred_username
credentials:
secret: 321321321-88a2-424c-bb4c-2312312321
auth-server-url: http://auth.customer.mydomain.tld:9080/auth
我尝试通过以下方式设置redirect_uri
<a href="/login?redirect_uri=http://localhost:8443/customer/account">Login</a>
我当然已经将此URI添加到我的领域设置中。
但是当用户单击链接时,它看起来如下所示
http://auth.customer.mydomain.tld:9080/auth/realms/customer-realm/protocol/openid-connect/auth?response_type=code&client_id=web-frontend&redirect_uri=http%3A%2F%2Flocalhost%3A8443%2Fsso%2Flogin&state=e81ad405-8a83-4e84-a155-2f1275f4390b&login=true&scope=openid
如何提供自己的重定向URI?
谢谢
答案 0 :(得分:1)
我不知道这个问题是否仍然有意义,但我偶然发现了同一问题。并且尽管可以通过配置keycloak.redirect-rewrite-rules.pathOld=pathNew
重写URI的某些部分,但这不适用于授权机构,即域名。
但是redirect-uri的域名取自请求的Host
头,因此,例如,适当地设置此头就足够了。在代理中。
或者,您需要注入一个自定义的PostProcessor,它最终会注入覆盖了OAuthRequestAuthenticator
的子类getRequestUrl()
。草绘的代码看起来像这样:
@Component
public class KeycloackAuthenticationProcessingFilterPostProcessor implements BeanPostProcessor {
private static final Logger logger = LoggerFactory.getLogger(KeycloackAuthenticationProcessingFilterPostProcessor.class);
private void process(KeycloakAuthenticationProcessingFilter filter) {
filter.setRequestAuthenticatorFactory(new SpringSecurityRequestAuthenticatorFactory() {
@Override
public RequestAuthenticator createRequestAuthenticator(HttpFacade facade, HttpServletRequest request, KeycloakDeployment deployment, AdapterTokenStore tokenStore, int sslRedirectPort) {
return new SpringSecurityRequestAuthenticator(facade, request, deployment, tokenStore, sslRedirectPort) {
@Override
protected OAuthRequestAuthenticator createOAuthAuthenticator() {
return new OAuthRequestAuthenticator(this, facade, deployment, sslRedirectPort, tokenStore) {
@Override
protected String getRequestUrl() {
return "http://localhost:8080";
}
};
}
};
}
});
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof KeycloakAuthenticationProcessingFilter) {
logger.info("Injecting Custom handler...");
process(((KeycloakAuthenticationProcessingFilter) bean));
}
return bean;
}
}
当我有一个代理服务器时,我走了简单的路,并相应地调整了Host
头。
答案 1 :(得分:0)
我们有一个spring boot应用程序,可通过配置进行设置:
@Configuration
public class Config {
@Value("${app.client.id}")
private String clientId;
@Value("${app.client.secret}")
private String clientSecret;
@Value("${auth.server.hostname}")
private String authServerHost;
@Value("${auth.server.port}")
private String authServerPort;
@Bean
public SecurityConfiguration securityInfo() {
Map<String, Object> additionalQueryStringParams = new HashMap<>();
additionalQueryStringParams.put("redirect_uri",
"http://" + authServerHost + ":" + authServerPort + "/sso/login");
return new SecurityConfiguration(clientId, clientSecret, "", "", "", additionalQueryStringParams, false);
}
}
也许这也对您有用?