程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 nginx 代理的带有 keycloak 的 Spring Boot 仅在 redirect_uri localhost大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 nginx 代理的带有 keycloak 的 Spring Boot 仅在 redirect_uri localhost?

开发过程中遇到使用 nginx 代理的带有 keycloak 的 Spring Boot 仅在 redirect_uri localhost的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 nginx 代理的带有 keycloak 的 Spring Boot 仅在 redirect_uri localhost的解决方法建议,希望对你解决使用 nginx 代理的带有 keycloak 的 Spring Boot 仅在 redirect_uri localhost有所启发或帮助;

我一直在为这个而头疼。

我有一个使用 Spring Boot 在 localhost:8082 上运行的基本 Web 应用程序、一个在 localhost:8081 上运行的 dockerized keycloak 服务器和一个在端口 80 上运行的 dockerized Nginx 服务器。

当我使用 keycloak @H_403_6@没有 spring 安全集成时,访问安全资源的用户将被重定向到 http://{keycloak-server 的 keycloak 登录页面}/auth/realms/{myrealm}/protocol/openID-connect/auth/...一切都很好。

当我根据 this tutorial 和其他几个人添加 spring 安全性时,突然我的应用程序尝试重定向到 http://{myapp}/sso/login,那里没有 /sso/login 端点所以我得到了一个 404。

只能通过直接访问 http://localhost:8082 并将客户端的 keycloak 中的 redirect_uri 设置为 http://localhost:8082/* 来将应用程序路由到正确的登录端点.

我怀疑它可能与 Nginx 配置有关,但同样,在我将 spring 安全性添加到混合中之前它已经工作了,所以我正在挠头。这是我的 Nginx.conf

worker_processes 2;

events {
    worker_connections 1024;
}

http {

    proxy_set_header    Host               $host;
    proxy_set_header    X-Real-IP          $remote_addr;
    proxy_set_header    X-ForWARDed-For    $proxy_add_x_forWARDed_for;
    proxy_set_header    X-ForWARDed-Host   $host;
    proxy_set_header    X-ForWARDed-Server $host;
    proxy_set_header    X-ForWARDed-Port   $server_port;
    proxy_set_header    X-ForWARDed-Proto  $scheR_327_11845@e;

    # auth local (keycloak)
    server {
        Listen  80;
        server_name auth.local;

            LOCATIOn / {
                # keycloak
                proxy_pass http://host.docker.internal:8081/;
            }

        }

        server {
            Listen  80;
            server_name guardian.local;

                LOCATIOn / {
                    # send to portal
                    rewrite ^/(.*)$ http://guardian.local/portal/$1 peRMANent;
                }

                LOCATIOn /portal {
                    # guardian web-portal
                    proxy_pass http://host.docker.internal:8082;
                }
            }

}

我的安全配置类:

@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {

    // @R_301_5555@s the KeycloakAuthenticationProvIDer to the AuthenticationManager
    @autowired
    public voID configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        KeycloakAuthenticationProvIDer keycloakAuthenticationProvIDer = keycloakAuthenticationProvIDer();
        keycloakAuthenticationProvIDer.setGrantedAuthoritIEsmapper(new SimpleAuthoritymapper());
        auth.authenticationProvIDer(keycloakAuthenticationProvIDer);
    }

    /**
     * Used by Spring Security to add Keycloak config from spring config resource (like application.propertIEs).
     * @return
     */
    @Bean
    public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();
    }

    // SpecifIEs the session authentication strategy
    @Bean
    @OverrIDe
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
    }

    @OverrIDe
    protected voID configure(httpSecurity http) throws Exception {
        super.configure(http);
        http.authorizerequests()
                .antMatchers("/portal/clIEnt*")
                .hasRole("user")
                .anyrequest()
                .permitAll();
    }
}

还有我的 application.propertIEs

server.port = 8082

keycloak.auth-server-url = http://auth.local/auth
keycloak.realm = myRealm
keycloak.resource = guardian-web-portal
keycloak.public-clIEnt = true
keycloak.use-resource-role-mapPings = true
keycloak.principal-attribute = preferred_username

对此的任何帮助将不胜感激。我使用的是 keycloak-spring-boot-starter 12.0.4 和 spring-boot-starter-security 2.4.5。

解决方法

尝试添加以下内容并将permitAll()改为authenticated()

border-image

你的

.antMatchers("/login*","/error*","/sso*" ).permitAll()

所以最后你会得到:

@Override
protected void configure(httpSecurity http) throws Exception {
    super.configure(http);
    http.authorizerequests()
            .antMatchers("/portal/client*")
            .hasRole("user")
            .anyrequest()
            .permitAll();
}

大佬总结

以上是大佬教程为你收集整理的使用 nginx 代理的带有 keycloak 的 Spring Boot 仅在 redirect_uri localhost全部内容,希望文章能够帮你解决使用 nginx 代理的带有 keycloak 的 Spring Boot 仅在 redirect_uri localhost所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。