324.3. 身份验证


此组件没有指定用于授权的安全凭证的过程。您可以编写自己的处理器或组件,这些处理器或组件从交换中获取身份验证信息,具体取决于您的需要。例如,您可以创建一个处理器,从来自 Jetty 组件的 HTTP 请求标头获取凭证。无论如何收集凭证,都需要将它们放在 In 消息或 SecurityContextHolder 中,以便 Camel Spring Security 组件可以访问它们:

import javax.security.auth.Subject;
import org.apache.camel.*;
import org.apache.commons.codec.binary.Base64;
import org.springframework.security.authentication.*;


public class MyAuthService implements Processor {
    public void process(Exchange exchange) throws Exception {
        // get the username and password from the HTTP header
        // http://en.wikipedia.org/wiki/Basic_access_authentication
        String userpass = new String(Base64.decodeBase64(exchange.getIn().getHeader("Authorization", String.class)));
        String[] tokens = userpass.split(":");

        // create an Authentication object
        UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(tokens[0], tokens[1]);

        // wrap it in a Subject
        Subject subject = new Subject();
        subject.getPrincipals().add(authToken);

        // place the Subject in the In message
        exchange.getIn().setHeader(Exchange.AUTHENTICATION, subject);

        // you could also do this if useThreadSecurityContext is set to true
        // SecurityContextHolder.getContext().setAuthentication(authToken);
    }
}
Copy to Clipboard Toggle word wrap

如果需要,SpringSecurityAuthorizationPolicy 会自动验证 Authentication 对象。

在使用 SecurityContextHolder 而不是 Exchange.AUTHENTICATION 标头时,需要注意两个问题。首先,上下文拥有者使用线程本地变量来存放 Authentication 对象。任何跨线程边界(如 sedajms )的路由都将丢失 身份验证 对象。其次,Spring Security 系统似乎预期上下文中的 Authentication 对象已经通过身份验证,并具有角色(更多详情请参阅 Technical Overview 部分 5.3.1 )。

camel-spring-security 的默认行为是在 Exchange.AUTHENTICATION 标头中查找 Subject。此主题必须至少包含一个主体,它必须是 org.springframework.security.core.Authentication 的子类。您可以通过向 < authorizationPolicy & gt; bean 提供 org.apache.camel.component.spring.security.AuthenticationAdapter 的实现来自定义 Subject 到 Authentication 对象的映射。如果您使用不使用 Spring Security 的组件,但确实会提供 Subject,这非常有用。目前,只有 CXF 组件会填充 Exchange.AUTHENTICATION 标头。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat