java - Camel Jetty 中的自定义基本身份验证

标签 java jetty osgi apache-camel apache-karaf

我有一个部署在 Apache Karaf 中的 OSGi 包。我正在使用 BASIC 身份验证来检查用户凭据。这是我的配置 Spring 文件:

<beans...>
...
    <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.modules.RolePrincipal</value>
            </list>
        </property>
    </bean>

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

    <bean id="constraint" class="org.eclipse.jetty.http.security.Constraint">
        <property name="name" value="BASIC"/>
        <property name="roles" value="admin"/>
        <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>

    <camelContext trace="true" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="jetty:http://0.0.0.0:8282/services?handlers=securityHandler&amp;matchOnUriPrefix=true"/>
            <transform>
                <constant>&lt;html>&lt;body>Hello from Fuse ESB server&lt;/body>&lt;/html></constant>
            </transform>
        </route>
    </camelContext>
    ....
</beans>

当我在浏览器中输入此 URL:http://localhost:8282/services 时,我会看到基本身份验证窗口,需要用户名和密码。到这一步就OK了。

用户凭据在 Apache Karaf &{base.dir}/etc/ 目录的 user.properties 中设置。从那里,身份 validator 将获取用户凭据进行检查。

我的问题是我需要以某种方式覆盖身份 validator 才能使用数据库中的凭据。我还没有尝试任何事情来完成这项工作,因为我不知道从哪里开始。我尝试在互联网上搜索,但没有任何线索如何使这项工作有效,甚至从哪里开始,使这项工作有效。因此,如果有人能为我指出如何做到这一点的正确方向,我将非常感激。

最佳答案

如果您需要将其从您自己的用户存储中取出,那么您需要提供您自己的 IdentityService 和 LoginService 并在上面的示例中替换它们。

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-security/src/main/java/org/eclipse/jetty/security/HashLoginService.java

这是一个登录服务的示例,它从属性类型文件加载用户并将它们存储在 HashMap 中。

您可能会很好地使用现有的 BasicAuthenticator,因为它使用提供的 LoginService 和 IdentityService...因此覆盖它们并替换它们,您应该可以开始了。

上面的哈希示例中的目录中有许多示例,其中包括 spnego 选项。

关于java - Camel Jetty 中的自定义基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12052364/

相关文章:

java - protected 成员的继承

java - 为什么通过二维数组打印时显示整个对象属性

java - Generics Java,无限通配符

java - 使用 Java 8 的 Streams 和 Lambdas 表达式迭代集合时出现问题

java - Okhttp android Websocket 和 Jetty 服务器与 SSL 的连接

java - 从jetty 7迁移到jetty 8时如何调用.addfilter()?

java - OSGi ClassNotFoundException 异常

java - 如何在 JAX-WS 中控制 JAXBContext?

maven - 使用 Jetty 9 和 Maven 插件在 Windows 中锁定文件

java - ServiceMix 找不到 OSGI 数据源