java - Apache Camel - 使用 Spring Security 进行 Camel 路由的基本身份验证

标签 java spring spring-security apache-camel apache-servicemix

我尝试向 Apache ServiceMix 提供的 Apache Camel 路由添加基本身份验证

我使用了命令feature:install Camel-spring-security 然后我添加到pom:

<dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring-security</artifactId>
        <version>2.16.4</version>
        <scope>provided</scope>
    </dependency>

我尝试使用 http://camel.apache.org/spring-security.html 中的代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cxf="http://camel.apache.org/schema/cxf"
       xmlns:spring-security="http://www.springframework.org/schema/security"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
                http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
                http://camel.apache.org/schema/spring-security http://www.springframework.org/schema/security/spring-security.xsd">

    <bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
        <constructor-arg index="0">
            <bean class="org.springframework.security.access.vote.RoleVoter"/>
        </constructor-arg>
        <property name="allowIfAllAbstainDecisions" value="true"/>
    </bean>

    <spring-security:authentication-manager alias="authenticationManager">
        <spring-security:authentication-provider user-service-ref="userDetailsService"/>
    </spring-security:authentication-manager>

    <spring-security:user-service id="userDetailsService">
        <spring-security:user name="jim" password="jimspassword" authorities="ROLE_USER, ROLE_ADMIN"/>
        <spring-security:user name="bob" password="bobspassword" authorities="ROLE_USER"/>
    </spring-security:user-service>

    <authorizationPolicy id="admin" access="ROLE_ADMIN"
                         authenticationManager="authenticationManager"
                         accessDecisionManager="accessDecisionManager"
                         xmlns="http://camel.apache.org/schema/spring-security"/>

    <bean id="synchBuilder" class="com.some.Class"/>
    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <routeBuilder ref="synchBuilder"/>
    </camelContext>
</beans>

在此之后,我在代码中添加了 from(something).policy("admin")......

但是我有异常(exception)

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'spring-security:authentication-manager'.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)[:]
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)[:]
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)[:]
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)[:]
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)[:]
    at org.apache.xerces.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)[:]
    at org.apache.xerces.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)[:]
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)[:]
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)[:]
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)[:]
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)[:]
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)[:]
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)[:]
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)[:]
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)[:]
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)[:]
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)[:]
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)[149:org.apache.servicemix.bundles.spring-beans:3.2.17.RELEASE_1]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:389)[149:org.apache.servicemix.bundles.spring-beans:3.2.17.RELEASE_1]
    ... 18 more

最佳答案

您使用的是太旧版本的 Camel/Spring Security 以及示例代码

<spring-security:authentication-manager alias="authenticationManager"> <spring-security:authentication-provider user-service-ref="userDetailsService"/> </spring-security:authentication-manager>

需要使用您不使用的较新版本的 Camel/Spring Security。升级 ServiceMix/Camel 或查找适合您的旧代码示例。

如果您浏览这些 XML 文件,也许 Camel 2.16.x 的一些单元测试可能会有所启发: https://github.com/apache/camel/tree/camel-2.16.x/components/camel-spring-security/src/test/resources/org/apache/camel/component/spring/security

关于java - Apache Camel - 使用 Spring Security 进行 Camel 路由的基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45036070/

相关文章:

java - Spring项目部署抛出org.apache.commons.logging.Log的ClassNotFoundException

maven-2 - 难以置信 : Cannot cast from class X to its super class

java - Spring Security 和 Web 服务 session

spring-boot - 如何捕获和处理 InvalidGrantException(用户被禁用)?

java - Spring security 不知道 spring mvc View 前缀

java - 如何在另一个类中使用来自一个类的 Action Listener 的目录路径变量?

java - 如何解决 Log4J No Such Property 警告/错误?

java - 方法上没有@ResponseBody 的@RestController 工作不正确

java - 二维对象数组不保留值

spring - Spring MVC 没有对像 WebFlux 这样的输入流在内存中缓冲的最大字节数施加限制吗?