135.4. 認証
認可に使用されるセキュリティー資格証明を取得するプロセスは、このコンポーネントでは指定されていません。必要に応じて、交換から認証情報を取得する独自のプロセッサーまたはコンポーネントを作成できます。たとえば、Jetty コンポーネントで発生した HTTP 要求ヘッダーから認証情報を取得するプロセッサーを作成できます。認証情報がどのように収集されても、Camel Spring Security コンポーネントがアクセスできるように、Inmessage または SecurityContextHolder に配置する必要があります。
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);
}
}
SpringSecurityAuthorizationPolicy は、必要に応じて Authentication オブジェクトを自動的に認証します。
Exchange.AUTHENTICATION ヘッダーの代わりに、またはそれに加えて SecurityContextHolder を使用する場合は、次の 2 つの問題に注意してください。
-
コンテキストホルダーはスレッドローカル変数を使用して
Authenticationオブジェクトを保持します。seda や jms など、スレッド境界を越えるルートはすべて、Authenticationオブジェクトを失います。 Spring Security システムは、コンテキスト内の
Authenticationオブジェクトがすでに認証されており、ロールを持っていることを前提としています。詳細は、Spring Technical Overview のセクション 5.3.1「Spring Security の認証とは何ですか?」を参照してください。
camel-spring-security のデフォルトの動作は、Exchange.AUTHENTICATION ヘッダーで Subject を探すことです。この Subject には、org.springframework.security.core.Authentication のサブクラスである必要があるプリンシパルが少なくとも 1 つ含まれている必要があります。
<authorizationPolicy> Bean に org.apache.camel.component.spring.security.AuthenticationAdapter の実装を提供することで、Subject から Authentication オブジェクトへのマッピングをカスタマイズできます。
これは、Spring Security を使用しないが Subject を提供するコンポーネントを操作している場合に役立ちます。
現時点では、CXF コンポーネントのみが Exchange.AUTHENTICATION ヘッダーに入力されます。