324.3. 인증

권한 부여에 사용되는 보안 인증 정보를 가져오는 프로세스는 이 구성 요소에서 지정하지 않습니다. 필요에 따라 교환에서 인증 정보를 가져오는 고유한 프로세서 또는 구성 요소를 작성할 수 있습니다. 예를 들어 Jetty 구성 요소에서 시작된 HTTP 요청 헤더에서 인증 정보를 가져오는 프로세서를 만들 수 있습니다. 인증 정보를 수집하는 방법에 관계없이 Camel Spring Security 구성 요소가 액세스할 수 있도록 In message 또는 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 를 사용할 때 알아야 할 두 가지 문제가 있습니다. 먼저 컨텍스트 소유자는 스레드 로컬 변수를 사용하여 인증 개체를 보유합니다.First, the context holder uses a thread-local variable to hold the Authentication object. seda 또는 jms 와 같은 스레드 경계를 교차하는 모든 경로는 Authentication 오브젝트를 잃게 됩니다. 둘째, Spring Security 시스템은 컨텍스트의 Authentication 오브젝트가 이미 인증되고 역할이 있습니다(기술 개요 섹션 5.3.1 에서 자세한 내용은).

comel -spring-security 의 기본 동작은 Exchange.AUTHENTICATION 헤더에서 주체 를 찾는 것입니다. 이 주체에org.springframework.security.core.Authentication 의 하위 클래스여야 하는 보안 주체가 하나 이상 포함되어야 합니다. org.apache.camel.component.spring.security. Authentication Adapter 의 구현을 < authorizationPolicy > bean에 제공하여 주체 와 인증 오브젝트의 매핑을 사용자 지정할 수 있습니다. 이는 Spring Security를 사용하지 않고 주체 를 제공하는 구성 요소로 작업하는 경우 유용할 수 있습니다. 이때 CXF 구성 요소만 Exchange.AUTHENTICATION 헤더를 채웁니다.