Keycloak ~ custom redirect_ The method of URI


In the process of integrating springboot with keycloak, for the interface that needs authorization, it will jump to keycloak to log in, and there is a redirect before it_ URI. After successful login, it will jump back to the client. However, there is no place to modify this address by default. We need to develop it manually. This is not very convenient.

Customize redirect_ URI

A rewrite beanpostprocessor to implement

public class KeycloackAuthenticationProcessingFilterPostProcessor implements BeanPostProcessor {

    private static final Logger logger = LoggerFactory.getLogger(KeycloackAuthenticationProcessingFilterPostProcessor.class);

    private void process(KeycloakAuthenticationProcessingFilter filter) {
        filter.setRequestAuthenticatorFactory(new SpringSecurityRequestAuthenticatorFactory() {
            public RequestAuthenticator createRequestAuthenticator(HttpFacade facade, HttpServletRequest request, KeycloakDeployment deployment, AdapterTokenStore tokenStore, int sslRedirectPort) {
                return new SpringSecurityRequestAuthenticator(facade, request, deployment, tokenStore, sslRedirectPort) {

                    protected OAuthRequestAuthenticator createOAuthAuthenticator() {
                        return new OAuthRequestAuthenticator(this, facade, deployment, sslRedirectPort, tokenStore) {

                            protected String getRequestUrl() {
                                return "http://localhost:8081/callback";


    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof KeycloakAuthenticationProcessingFilter) {
  "Injecting Custom handler...");
            process(((KeycloakAuthenticationProcessingFilter) bean));
        return bean;

Second, when there is no authorization, directly jump to the authorization system, and then observe the address redirect_ The URI has changed

Reference documents:

Recommended Today

Go carbon version 1.2.0 has been released to improve and optimize the multi scenario support for orm

Carbon is a lightweight, semantic and developer friendly golang time processing library, which supports chain call, lunar calendar, Gorm, xorm and other mainstream orm If you think it’s good, please give it to a star Installation and use //Using GitHub Library go get -u import ( “”) //Using gitee Library go get […]