【问题标题】:Custom Basic Authentication in Camel JettyCamel Jetty 中的自定义基本身份验证
【发布时间】:2012-08-16 15:37:44
【问题描述】:

我有一个部署在 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 时,我会看到基本身份验证窗口,需要用户名和密码。到此为止没关系。

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

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

【问题讨论】:

    标签: java jetty osgi apache-camel apache-karaf


    【解决方案1】:

    由于您似乎在使用 Spring,请考虑使用 Spring Security 模块为您的 web 应用程序提供安全性。

    甚至还有一个JdbcDaoImpl 你可以连接起来为安全提供 UserDetailsS​​ervice http://static.springsource.org/spring-security/site/docs/3.1.x/reference/core-services.html#d0e2875

    【讨论】:

      【解决方案2】:

      如果您需要将其从您自己的用户存储中提取出来,那么您需要提供您自己的 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

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

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

      上面的哈希示例中有许多示例,包括一个 spnego 选项。

      【讨论】:

      • 其实我不用自己创建IdentityService,我用的是默认的。我所做的是创建了我自己的登录服务扩展 AbstractLifeCycle,实现了 LoginService,实现了我需要的方法,并且它现在可以工作了。我有来自数据库的用户。基本身份验证也有效。谢谢,你真的让我朝着正确的方向前进。
      猜你喜欢
      • 2012-03-18
      • 1970-01-01
      • 2013-11-10
      • 2015-11-12
      • 2017-03-10
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多