Keycloak ~ custom redirect_ The method of URI

Time:2020-11-21

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

@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:8081/callback";
                            }
                        };
                    }

                };
            }
        });
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof KeycloakAuthenticationProcessingFilter) {
            logger.info("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
1

Reference documents:https://stackoverflow.com/questions/54163924/how-to-set-redirect-uri-in-keycloak-with-spring-boot

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 github:github.com/golang-module/carbon gitee:gitee.com/go-package/carbon Installation and use //Using GitHub Library go get -u github.com/golang-module/carbon import ( “github.com/golang-module/carbon”) //Using gitee Library go get […]