我的应用程序同时监听HTTP和HTTPS端口。HTTP通道只对几个客户端可用,当尝试访问不安全的客户端时,必须将所有其他客户端重定向到安全通道(HTTPS)。
问题是用于重定向的HTTPS端口,出于很好的理由,我将根据应用程序属性自行启动连接器。默认的端口映射实现PortMapperImpl
不知道配置,因此它总是返回443
或8443
,忽略应用程序正在侦听的实际端口。
到目前为止,我已经发现了这一点;它很有效,但是看起来很难看:
@Configuration
class MySecurityAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.requiresChannel()
.requestMatchers(/* some matchers */)
.requiresSecure()
.and()
.setSharedObject(PortMapper.class, myPortMapper());
}
@Bean
PortMapper myPortMapper() { return new MyPortMapper(); }
}
我使用的是标准Servlet,因此我无法从ServerHttpSecurity
(端口映射器似乎被包括在内的显式设置)中获益。
设置自定义端口映射程序的正确的方法是什么?那里有优雅的吗?
发布于 2020-01-23 14:13:44
servlet还允许使用portMapper()
方法设置端口映射器。
在这种情况下,您可以使用以下配置
httpSecurity
.requiresChannel()
.requestMatchers(/* some matchers */)
.requiresSecure()
.and()
.portMapper()
.portMapper(new MyPortMapper());
https://stackoverflow.com/questions/59874547
复制相似问题