有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一些使用Spring安全性并部署在Tomcat7中的web应用程序。在tomcat中有两个连接器(808081)。我想共享我的应用程序的一部分,并允许访问${ip}:8080/${servercontext}/resource之类的请求,并通过此端口保护应用程序的其余部分,即拒绝${ip}:8080/${servercontext}/otherresource之类的请求。但是像${ip}:8081/${servercontext}/otherresource这样的请求必须是可访问的(8081端口)

我怎么做


共 (1) 个答案

  1. # 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>
    

    您还可以注意到,在您的web中有一种其他方法(非spring特有)。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,但似乎您已经这样做了)