97.9. 使用 HTTP 基本身份验证
Netty HTTP 使用者通过指定要使用的安全域名称来支持 HTTP 基本身份验证,如下所示
<route> <from uri="netty-http:http://0.0.0.0:{{port}}/foo?securityConfiguration.realm=karaf"/> ... </route>
域名称是启用基本身份验证所必需的。默认情况下,使用基于 JAAS 的验证器,它使用指定的 realm 名称(上例中的karaf),并使用 JAAS 域和这个域的 JAAS \\{{LoginModule}}s 进行身份验证。
Apache Karaf / ServiceMix 最终用户开箱即用有一个 karaf realm,因此上例将从这些容器开箱即用的原因。
97.9.1. 在 web 资源中指定 ACL
org.apache.camel.component.netty.http.SecurityConstraint
允许定义 web 资源的约束。org.apache.camel.component.netty.http.SecurityConstraintMapping
开箱即用,可轻松定义带有角色的包含和排除项。
例如,在 XML DSL 中,我们定义约束 bean:
<bean id="constraint" class="org.apache.camel.component.netty.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>
以上约束被定义,以便
- 对 Attr 的访问会被限制,并接受任何角色(如果用户没有角色)
- 访问 /adminAttr 需要 admin 角色
- 访问 /guestAttr 需要 admin 或 guest 角色
- 访问 /publicAttr 是一个排除的,这意味着不需要身份验证,因此任何人都没有登录
要使用此约束,我们只需要引用 bean id,如下所示:
<route> <from uri="netty-http:http://0.0.0.0:{{port}}/foo?matchOnUriPrefix=true&securityConfiguration.realm=karaf&securityConfiguration.securityConstraint=#constraint"/> ... </route>