[英]Behind a Reverse Proxy, Spring Security Login Redirects to Wrong Port
我有以下配置:
Host
, X-Forwarded-Proto
) 我遇到的问题是,成功登录后,Spring Security构建的重定向URL为https://server:8443/whatever
。 除端口外,URL正确(它是基于初始保存的请求构建的)。 在此配置中,端口8443上没有任何运行。
我看到这是在Spring的PortMapperImpl
发生的。 默认情况下,这里有两个映射: 8080 -> 8443
和80 -> 443
。
如何覆盖PortMapperImpl
或实现自己的并强制Spring使用它? 还是有其他方法可以解决此问题?
我找到了解决方案。 在Spring Java配置中:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${server.port}")
private int serverPort;
@Value("${security.sslRedirectPort}")
private int sslRedirectPort;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.[the usual request matchers, authorizeRequests, etc]
.and()
.requestCache().requestCache(requestCache());
}
private PortMapper portMapper() {
PortMapperImpl portMapper = new PortMapperImpl();
Map<String, String> mappings = Maps.newHashMap();
mappings.put(Integer.toString(serverPort), Integer.toString(sslRedirectPort));
portMapper.setPortMappings(mappings);
return portMapper;
}
private RequestCache requestCache() {
HttpSessionRequestCache requestCache = new HttpSessionRequestCache();
PortResolverImpl portResolver = new PortResolverImpl();
portResolver.setPortMapper(portMapper());
requestCache.setPortResolver(portResolver);
return requestCache;
}
}
这里发生了什么事:
server.port
设置注入http端口(默认情况下,春季为8080) security.sslRedirectPort
的设置。 将此属性设置为您要重定向到application.yaml
文件中的任何属性。 RequestCache
并将其插入到spring安全配置中。 在此自定义RequestCache,我设置了PortResolver
和它的PortMapper
,然后设置相应的映射值。 现在,当我运行时,登录后会得到正确的重定向URL,端口设置为将security.sslRedirectPort
设置为的端口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.