302.3. Authentication
承認に使用されるセキュリティー認証情報を取得するプロセスは、このコンポーネントで指定されません。必要に応じて、エクスチェンジから認証情報を取得する独自のプロセッサーまたはコンポーネントを作成できます。たとえば、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 ヘッダーの代わりに、または SecurityContextHolder を使用する場合は、2 つの問題があります。まず、コンテキストホルダーはスレッドローカル変数を使用して 認証 オブジェクトを保持します。seda や jms などのスレッド境界にまたがるルートは、認証 オブジェクトが失われます。次に、Spring Security システムは、コンテキストの Authentication オブジェクトがすでに認証されており、ロールがあることが予想されます(詳細は、Technical Overview セクション を参照してください)。
camel-spring-security のデフォルト動作は、Exchange.AUTHENTICATION ヘッダーで Subject を検索します。この サブジェクト には、org.springframework.security.core.Authentication のサブクラスであるプリンシパルが少なくとも 1 つ含まれる必要があります。Subject から Authentication オブジェクトへのマッピングをカスタマイズするには、org.apache.camel.component.spring.security.AuthenticationAdapter の実装を < authorizationPolicy> Bean に指定します。これは、Spring Security を使用せず、Subject を提供するコンポーネントを使用している場合に役立ちます。現時点では、CXF コンポーネントのみが Exchange.AUTHENTICATION ヘッダーを設定します。