如何解决使用 nginx 代理的带有 keycloak 的 Spring Boot 仅在 redirect_uri localhost
我一直在为这个而头疼。
我有一个使用 Spring Boot 在 localhost:8082 上运行的基本 Web 应用程序、一个在 localhost:8081 上运行的 dockerized keycloak 服务器和一个在端口 80 上运行的 dockerized nginx 服务器。
当我使用 keycloak 没有 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 $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/$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 {
// 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()
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();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。