Spring 仅当 redirect_uri localhost 时使用 nginx 代理通过 keycloak 启动

Spring boot with keycloak using nginx proxy only works if redirect_uri localhost

我一直在为这个问题苦思冥想。

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

当我使用 keycloak 没有 spring 安全集成时,访问受保护资源的用户将被重定向到keycloak 登录页面位于 http://{keycloak-server}/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  $scheme;

    # 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/ permanent;
                }

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

}

我的安全配置class:

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

    // Submits 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()

.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();
}

所以最后你会得到:

@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http.authorizeRequests()
            .antMatchers("/login*", "/error*", "/sso*" ).permitAll()
            .antMatchers("/portal/client*")
            .hasRole("user")
            .anyRequest()
            .authenticated();
}