java - Spring Security:如何设置自定义 PortMapper?
问题描述
我的应用程序同时监听 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 API,因此我无法从似乎包含ServerHttpSecurity
端口映射器的显式设置的位置中受益。
设置自定义端口映射器的正确方法是什么?它有什么优雅的吗?
解决方案
portMapper()
servlet API 还允许使用该方法设置端口映射器。
在这种情况下,您可以使用以下配置
httpSecurity
.requiresChannel()
.requestMatchers(/* some matchers */)
.requiresSecure()
.and()
.portMapper()
.portMapper(new MyPortMapper());
推荐阅读
- javascript - 使用 Webpack 和 Babel 在 JS 中按名称获取构造函数
- python - 当列中有列表对象时,获取熊猫数据框中唯一值的计数
- elasticsearch - 错误:Logstash 因错误停止处理:(SystemExit)退出
- java - 从 HSM 中提取键值
- go - 未知的修订版本
- python - 总结 CSV 键:值数据的更好/更快的方法
- c# - 如何使用 BouncyCastle Blowfish 实现解密数据?
- sql - 加入子查询时 Sybase ASE 触发异常 11040
- reactjs - 当正文处于 RTL 中时,HTML5 日期选择器不翻转
- c# - 只有在另一个功能执行后才激活天蓝色功能?