Как правильно настроить базовую аутентификацию для CXF / HTTP в JBoss Fuse?

Я уже довольно давно пытаюсь настроить базовую аутентификацию для всех моих открытых веб-сервисов, но безуспешно.

Я использую JBoss Fuse 6.2.1 с контейнером Karaf (karaf-2.4.0.redhat-621117), и в настоящее время у меня есть три интеграции, которые потребляют из равного количества cxfEndpoints.

Я хочу добиться того, чтобы пользователи этих служб предлагали диалог авторизации при вызове служб или попытке просмотра WSDL. Обратите внимание, что я не хочу использовать ws-security, которая размещает аутентификацию в Soap-конверте, а скорее на уровне http.

Я просматривал следующие записи документации: [1] https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.2.1/html/Security_Guide/CamelJetty-BasicAuth.html

[2] http://cxf.apache.org/docs/client-http-transport-including-ssl-support.html

[3] http://cxf.apache.org/docs/jetty-configuration.html < / а>

Но я не понимаю, какой из этих подходов (если вообще есть) я должен использовать. Фактически, ни один из них пока не работал у меня, но это могло быть связано с ошибкой пользователя от моего имени.

Ниже я покажу, что я пытался (и впоследствии потерпел неудачу):

Использование комбинации [1] и [3]

blueprint.xml:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
    xmlns:cxf-core="http://cxf.apache.org/blueprint/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:httpj="http://cxf.apache.org/transports/http-jetty/configuration"
    xsi:schemaLocation="
        http://cxf.apache.org/transports/http-jetty/configuration http://cxf.apache.org/schemas/configuration/http-jetty.xsd
        http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
        http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <bean id="loginService" class="org.eclipse.jetty.plus.jaas.JAASLoginService">
        <property name="name" value="karaf"/>
        <property name="loginModuleName" value="karaf"/>
        <property name="roleClassNames">
            <list>
                <value>org.apache.karaf.jaas.boot.principal.RolePrincipal</value>
            </list>
        </property>
    </bean>

    <bean id="identityService" class="org.eclipse.jetty.security.DefaultIdentityService"/>

    <bean id="constraint" class="org.eclipse.jetty.util.security.Constraint">
        <property name="name" value="BASIC"/>
        <property name="roles" value="Administrator"/>
        <property name="authenticate" value="true"/>
    </bean>

    <bean id="constraintMapping" class="org.eclipse.jetty.security.ConstraintMapping">
        <property name="constraint" ref="constraint"/>
        <property name="pathSpec" value="/*"/>
    </bean>

    <bean id="securityHandler" class="org.eclipse.jetty.security.ConstraintSecurityHandler">
        <property name="authenticator">
            <bean class="org.eclipse.jetty.security.authentication.BasicAuthenticator"/>
        </property>
        <property name="constraintMappings">
            <list>
                <ref bean="constraintMapping"/>
            </list>
        </property>
        <property name="loginService" ref="loginService"/>
        <property name="strict" value="false"/>
        <property name="identityService" ref="identityService"/>
    </bean>

    <httpj:engine-factory bus="cxf">
        <httpj:engine port="8181">
            <httpj:handlers>
                <ref component-id="securityHandler" />
            </httpj:handlers>
        </httpj:engine>
     </httpj:engine-factory>

</blueprint>

Использование [2]

blueprint.xml:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
    xmlns:cxf-core="http://cxf.apache.org/blueprint/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
    xsi:schemaLocation="
        http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
        http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
        http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <http-conf:conduit name="http://localhost:8181/.*" xmlns:sec="http://cxf.apache.org/configuration/security">
        <http-conf:authorization>
            <sec:UserName>test</sec:UserName>
            <sec:Password>test</sec:Password>
            <sec:AuthorizationType>BASIC</sec:AuthorizationType>
        </http-conf:authorization>
    </http-conf:conduit>

</blueprint>

В обоих случаях используется cxfEndpoint

<cxf:cxfEndpoint address="${address}" id="myWs" serviceClass="com.company.test.CxfService">
    <cxf:properties>
        <entry key="dataFormat" value="PAYLOAD" />
    </cxf:properties>
</cxf:cxfEndpoint>

Оптимальным было бы использовать JAAS, но для начала я бы предпочел что-нибудь попроще.

Я должен добавить, что я не получаю никаких ошибок ни с одним из них, мне просто не предлагается ввести какие-либо учетные данные при просмотре http://localhost:8181/cxf или при вызове сервисов через SoapUI.

Я был бы очень признателен, если бы кто-нибудь мог указать мне правильное направление.


person Erik Karlstrand    schedule 04.10.2016    source источник


Ответы (1)


Мне удалось найти жизнеспособное решение, поэтому я опубликую свои выводы с надеждой, что это кому-то еще поможет в какой-то момент.

blueprint.xml:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
    xmlns:cxf-core="http://cxf.apache.org/blueprint/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:httpj="http://cxf.apache.org/transports/http-jetty/configuration"
    xsi:schemaLocation="
        http://cxf.apache.org/transports/http-jetty/configuration http://cxf.apache.org/schemas/configuration/http-jetty.xsd
        http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
        http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <bean id="loginService" class="org.eclipse.jetty.plus.jaas.JAASLoginService">
        <property name="name" value="karaf"/>
        <property name="loginModuleName" value="karaf"/>
        <property name="roleClassNames">
            <list>
                <value>org.apache.karaf.jaas.boot.principal.RolePrincipal</value>
            </list>
        </property>
    </bean>

    <bean id="identityService" class="org.eclipse.jetty.security.DefaultIdentityService"/>

    <bean id="constraint" class="org.eclipse.jetty.util.security.Constraint">
        <property name="name" value="BASIC"/>
        <property name="roles">
            <list>
                <value>admin</value>
            </list>
        </property>
        <property name="authenticate" value="true"/>
    </bean>

    <bean id="constraintMapping" class="org.eclipse.jetty.security.ConstraintMapping">
        <property name="constraint" ref="constraint"/>
        <property name="pathSpec" value="/*"/>
    </bean>

    <bean id="securityHandler" class="org.eclipse.jetty.security.ConstraintSecurityHandler">
        <property name="authenticator">
            <bean class="org.eclipse.jetty.security.authentication.BasicAuthenticator"/>
        </property>
        <property name="constraintMappings">
            <list>
                <ref component-id="constraintMapping"/>
            </list>
        </property>
        <property name="loginService" ref="loginService"/>
        <property name="strict" value="false"/>
        <property name="identityService" ref="identityService"/>
    </bean>

    <httpj:engine-factory bus="cxf">
        <httpj:engine port="8083">
            <httpj:handlers>
                <ref component-id="securityHandler" />
            </httpj:handlers>
        </httpj:engine>
    </httpj:engine-factory>

    <cxf:cxfEndpoint address="http://localhost:8083/MyService" id="myWs" serviceClass="com.company.test.CxfService">
        <cxf:properties>
            <entry key="dataFormat" value="PAYLOAD" />
        </cxf:properties>
    </cxf:cxfEndpoint>

    ...

</blueprint>

Также необходимо добавить в pom.xml следующее:

<Import-Package>
    javax.security.auth,
    javax.security.auth.callback,
    javax.security.auth.login,
    javax.security.auth.spi,
    org.apache.karaf.jaas.modules,
    org.apache.karaf.jaas.boot.principal,
    org.eclipse.jetty.server,
    org.eclipse.jetty.plus.jaas;version=${jetty-version},
    org.eclipse.jetty.security;version=${jetty-version},
    *
</Import-Package>

org.eclipse.jetty.server необходим для работы httpj: engine-factory. Казалось бы, вы не можете использовать порт по умолчанию (8181), если хотите настроить базовую аутентификацию. Это решение вместо этого устанавливает собственный контейнер причала на порт 8083 (вы можете использовать другой порт, просто убедитесь, что ваши cxfEndpoints публикуются на том же порту).

person Erik Karlstrand    schedule 05.10.2016
comment
Где вы установили фактические значения имени пользователя и пароля? В JAAS или Караф ты как-то использовал? - person Souciance Eqdam Rashti; 06.10.2016
comment
Это решение использует область JAAS по умолчанию. Т.е. учетные данные берутся из файла users.properties, который находится в каталоге Fuse / etc. - person Erik Karlstrand; 06.10.2016