【问题标题】:How to enable security only for some servlets via web.xml如何通过 web.xml 仅为某些 servlet 启用安全性
【发布时间】:2012-12-06 15:07:56
【问题描述】:

场景:

应为某些 servlet 启用安全性(仅通过凭据访问),但对其他 servlet 不启用。全部通过 web.xml。

【问题讨论】:

  • 安全是什么意思?

标签: java jakarta-ee servlets web.xml


【解决方案1】:

这样做是这样的:

首先是安全角色+登录配置:

<security-role>
  <description>
    Main user for admin GUI
  </description>
  <role-name>admin</role-name>
</security-role>

<login-config>
  <auth-method>BASIC</auth-method>
  <realm-name>my login</realm-name>
</login-config>

应该公开访问的这个 servlet:

<servlet>
  <description>Landing Page for Admin GUI</description>
  <display-name>StartServlet</display-name>
  <servlet-name>StartServlet</servlet-name>
  <servlet-class>StartServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>StartServlet</servlet-name>
  <url-pattern>/index.html</url-pattern>
</servlet-mapping>

所有页面的限制(只能通过管理员用户访问):

<security-constraint>
  <web-resource-collection>
    <web-resource-name>Private</web-resource-name>
    <description>Matches all pages</description>
    <url-pattern>/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
     <role-name>admin</role-name>
  </auth-constraint>
</security-constraint>

公开的只是那些:

<security-constraint>
  <web-resource-collection>
    <web-resource-name>Public</web-resource-name>
    <description>Makes the landing page explicitly public (overrides Private above since more specific!)</description>
    <url-pattern>/index.html</url-pattern>
  </web-resource-collection>
  <!-- No auth-constraint = everybody has access! -->
</security-constraint>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2018-08-15
    • 2016-12-17
    • 1970-01-01
    相关资源
    最近更新 更多