【问题标题】:How can I use port number in securing web request如何在保护 Web 请求时使用端口号
【发布时间】:2015-02-20 06:37:59
【问题描述】:

我有一些使用 Spring-security 并部署在 Tomcat7 中的 Web 应用程序。在 tomcat 中有两个连接器(8080、8081)。我想共享我的应用程序的一部分并允许访问像 ${ip}:8080/${servercontext}/resource 这样的请求,并通过这个端口保护应用程序的其余部分,即拒绝像 ${ip}:8080/${ 这样的请求服务器上下文}/其他资源。但是像 ${ip}:8081/${servercontext}/otherresource 这样的请求必须是可访问的(8081 端口)。

我该怎么做?

【问题讨论】:

  • 我将通过 8080 端口在公共域上公开应用程序。我想在专用网络中使用端口 8081。任何来自 8081 端口的请求都不能被拒绝。

标签: java spring spring-mvc tomcat


【解决方案1】:

根据Spring security documentation,可以在intercept-url标签中使用requires-channel属性:

<http>
  <intercept-url pattern="/resource/**" access="ROLE_USER" requires-channel="https"/>
  <intercept-url pattern="otherresource" access="ROLE_USER" requires-channel="any"/>
  ...
</http>

您还可以注意到,还有一种 other 方法(非 spring-specific),在您的 web.xml 中添加以下代码:

<security-constraint>
      <web-resource-collection>
        <web-resource-name>HTTPSOnly Resources</web-resource-name>
        <url-pattern>/resources*</url-pattern>
    </web-resource-collection> 
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint> 
</security-constraint>

这会自动将用户重定向到 HTTPS(您需要将服务器配置为支持 HTTPS,但您似乎已经这样做了)

【讨论】:

  • 这不是我想要的。不得拒绝任何来自 8081 端口的请求。请看我上面的评论。此外,您的方法需要使用 SSL 进行操作 - 证书、容器等。
猜你喜欢
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 2020-09-17
  • 1970-01-01
  • 2014-08-30
  • 2015-03-15
  • 1970-01-01
相关资源
最近更新 更多