如何在保护Web请求时使用端口号

时间:2014-12-22 07:05:53

标签: java spring spring-mvc tomcat

我有一些使用Spring-security并部署在Tomcat7中的Web应用程序。在tomcat中有两个连接器(8080,8081)。我想分享我的部分应用程序,并访问$ {ip}:8080 / $ {servercontext} / resource等请求,并通过此端口保护应用程序的其余部分,即拒绝请求,如$ {ip}:8080 / $ { ServerContext中} / otherresource。但是必须可以访问$ {ip}:8081 / $ {servercontext} / otherresource等请求(8081端口)。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

根据Spring security documentation,您可以使用requires-channel代码中的intercept-url属性:

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

您还可以注意到有一种其他方式(非特定于春天),在您的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,但似乎您已经这样做了)