spring-boot - nginx反向代理背后的spring security的身份验证问题
问题描述
我对 Spring Security 和 nginx 反向代理服务器有疑问。在我的 Spring 启动应用程序中,我的大多数路由都受到 Basic Auth 的保护。但是,我希望有一组特定的路由仅受 nginx 基本身份验证保护。
不幸的是,我遇到的问题是路由总是要求两种身份验证。
我创建了一个针对这个特定春季路线的位置。spring 应用程序和 nginx 服务器各自运行在一个单独的 docker 容器中。
这是我的 Spring Security 设置。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/reporting/**").permitAll()
.antMatchers("/user/create").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.httpBasic();
}
这是我的位置:
location /smartphone-reporting {
rewrite /smartphone-reporting(.*)$ $1 break;
proxy_pass http://172.17.0.1:8888/reporting;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/htpasswd.users;
}
在我的 nginx 服务器配置中,我定义了以下标头参数:
server {
...
# Add X-Forwarded-* headers
proxy_set_header X-Forwarded-Host $hostname;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Cert $ssl_client_s_dn;
}
如果我在 proxy_pass 路由上的服务器上使用 curl,我会收到没有任何身份验证的响应。如果我从服务器外部发出请求,我最终会陷入无限循环,要求两种身份验证类型。
我如何设置它正在工作的 nginx?
解决方案
我解决了。我的重写规则在 Spring 中没有转发到 /reporting,我需要清除身份验证标头。
以下位置配置对我有用:
location /smartphone-reporting/ {
proxy_pass http://172.17.0.1:8888/reporting/;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/htpasswd.users;
proxy_set_header Authorization "";
}
推荐阅读
- azure - 如何将事件从 Linux 机器发送到 Azure IoT Edge Hub
- php - 如何使多个语句循环合二为一?(性能问题/PHP)
- c# - 减少元组列表中元组搜索的 foreach 循环
- asp.net-web-api2 - 使用 SimpleInjector 实现 MassTransit IoC
- java - Java - 内部 ArrayList 循环未执行预期功能
- mysql - mysql st_within 不使用虚拟列
- wagtail - 如何列出主页的直接子级?
- unix - 需要找到我的文件的路径(Window7 中的 cygwin)
- xml - 从 XML 文件中获取 XPath
- javascript - WebRTC:如何简单地使用 getStats()?