243.9. 使用 HTTP 基本身份验证

Netty HTTP 使用者支持 HTTP 基本身份验证,方法是指定要使用的安全域名称,如下所示

<route>
   <from uri="netty4-http:http://0.0.0.0:{{port}}/foo?securityConfiguration.realm=karaf"/>
   ...
</route>

realm 名称是启用基本身份验证的强制性。默认情况下,使用了基于 JAAS 的验证器,它将使用指定的域名(上例中的karaf),并使用此域的 JAAS 域和 JAAS \{{LoginModule}}s 进行身份验证。

Apache Karaf / ServiceMix 的末尾有一个 karaf 域开箱即用,因此上面的示例在这些容器中无法使用该框的原因。

243.9.1. 在 web 资源中指定 ACL

org.apache.camel.component.netty4.http.SecurityConstraint 允许定义 Web 资源约束。而且,org.apache.camel.component.netty.http.SecurityConstraintMapping 开箱即用,允许轻松定义包含和排除角色。

例如,如 XML DSL 所示,我们定义约束 bean:

  <bean id="constraint" class="org.apache.camel.component.netty4.http.SecurityConstraintMapping">
    <!-- inclusions defines url -> roles restrictions -->
    <!-- a * should be used for any role accepted (or even no roles) -->
    <property name="inclusions">
      <map>
        <entry key="/*" value="*"/>
        <entry key="/admin/*" value="admin"/>
        <entry key="/guest/*" value="admin,guest"/>
      </map>
    </property>
    <!-- exclusions is used to define public urls, which requires no authentication -->
    <property name="exclusions">
      <set>
        <value>/public/*</value>
      </set>
    </property>
  </bean>

上述限制已定义

  • 对 /* 的访问受到限制,且接受任何角色(如果用户没有角色)
  • 访问 /admin/* 需要 admin 角色
  • 访问 /guest/* 需要 admin 或 guest 角色
  • 访问 /public/* 是一个排除,这意味着不需要身份验证,因此在没有登录的情况下,所有人都可以公开

要使用这个约束,我们需要引用 bean id,如下所示:

<route>
   <from uri="netty4-http:http://0.0.0.0:{{port}}/foo?matchOnUriPrefix=true&amp;securityConfiguration.realm=karaf&amp;securityConfiguration.securityConstraint=#constraint"/>
   ...
</route>