323.3. 認証
認可に使用されるセキュリティー資格証明を取得するプロセスは、このコンポーネントでは指定されていません。必要に応じて、交換から認証情報を取得する独自のプロセッサーまたはコンポーネントを作成できます。たとえば、Jetty コンポーネントで発生した HTTP 要求ヘッダーから認証情報を取得するプロセッサーを作成できます。認証情報がどのように収集されても、Camel Spring Security コンポーネントがアクセスできるように、In メッセージまたは 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 ヘッダーの代わりに、または Exchange.AUTHENTICATION ヘッダーに加えて SecurityContextHolder を使用する場合、注意すべき 2 つの問題があります。まず、コンテキストホルダーはスレッドローカル変数を使用して Authentication オブジェクトを保持します。seda や jms など、スレッドの境界をまたぐルートは、Authentication オブジェクトを失います。次に、Spring Security システムは、コンテキスト内の Authentication オブジェクトがすでに認証されており、ロールを持っていることを期待しているように見えます (詳細については、技術概要 セクション 5.3.1 を参照してください)。
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 ヘッダーに入力されます。