无法将 Spring Security BASIC 身份验证集成到 Jersey/JAX-RS 和 Tomcat 中

2023-12-22

我正在尝试将 BASIC 身份验证添加到我使用 Jersey/JAX-RS 和 Tomcat Apache 7.0 创建的 RESTful Web 服务。将来我想在 WebSphere 上部署此 Web 服务,因此我选择在我的项目中使用 Spring Security(版本 2.5.6)。

我的问题是这样的:虽然我相信我的各种 xml 文件是正确的,并且我已将 spring.jar 添加到我的类路径中,但在启动服务器时出现以下错误。

SEVERE: Error configuring application listener of class
org.springframework.web.context.ContextLoaderListener java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
    ...

等等。我查看过的每个资源都表明我应该将 spring.jar 添加到我的类路径中,这是我所拥有的。我对 Spring 完全陌生,所以如果我的任何文件设置不正确,请告诉我。以下是所有相关的 XML 文件和设置。

安全-applicationContext.xml:

<?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:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                       http://www.springframework.org/schema/security
                       http://www.springframework.org/schema/security/spring-security-2.0.xsd">

<security:global-method-security secured-annotations="enabled"/>

<security:http>
    <security:http-basic/>
    <security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http>


<security:authentication-manager alias="authenticationManager"/>


<bean id="basicProcessingFilter" class="org.springframework.security.ui.basicauth.BasicProcessingFilter">
    <property name="authenticationEntryPoint" ref="authenticationEntryPoint"/>
    <property name="authenticationManager" ref="authenticationManager"/>
</bean>


<bean id="httpSessionContextIntegrationFilter" class="org.springframework.security.context.HttpSessionContextIntegrationFilter">
    <!--property name="contextClass" value="org.springframework.security.context.SecurityContextImpl"/-->
    <property name="allowSessionCreation" value="false"/>
</bean>


<bean id="httpSessionContextIntegrationFilterWithASCFalse" class="org.springframework.security.context.HttpSessionContextIntegrationFilter">
    <property name="allowSessionCreation" value="false"/>
</bean>

<bean id="authenticationEntryPoint"
        class="org.springframework.security.ui.basicauth.BasicProcessingFilterEntryPoint">
    <property name="realmName" value="Your realm name"/>
</bean>

<security:authentication-provider>
    <security:password-encoder hash="md5"/>
    <security:user-service>
        <security:user name="admin" password="2fa3fa1c2deff56ed33e0bf974f2e29e" authorities="ROLE_PARTNER, ROLE_USER"/>
    </security:user-service>
</security:authentication-provider>

applicationContext.xml(有人告诉我它可能是空的):

<beans></beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee
/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestService</display-name>
<servlet>
   <servlet-name>Jersey REST Service</servlet-name>
   <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
 <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>TestService</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
      /WEB-INF/security-applicationContext.xml,
      /WEB-INF/applicationContext.xml
  </param-value>
</context-param>

<!-- Enables Spring Security -->

<filter> 
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>
      org.springframework.web.filter.DelegatingFilterProxy
  </filter-class>
  <init-param>
      <param-name>targetBeanName</param-name>
      <param-value>springSecurityFilterChain</param-value>
  </init-param>
</filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

最后,我的文件结构:

和我的服务器的运行配置:


This http://threebit.net/mail-archive/tomcat-users/msg13838.html看起来像一个类似的问题。

您可能想尝试不同的 Tomcat 版本。


您缺少依赖项。

最小的 spring-security 依赖应该是:

<properties>
    <spring.version>3.0.1.RELEASE</spring.version>
</properties>
<dependency>
    <groupId>org.springframework.security</groupId>
     <artifactId>spring-security-web</artifactId>
     <version>${spring.version}</version>
</dependency>
<dependency>
     <groupId>org.springframework.security</groupId>
     <artifactId>spring-security-config</artifactId>
     <version>${spring.version}</version>
</dependency>
<dependency>
     <groupId>org.springframework.security</groupId>
     <artifactId>spring-security-taglibs</artifactId>
     <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>${spring.version}</version>
</dependency>

你似乎没有使用maven所以这个网站 http://mvnrepository.com/将帮助您找到您需要的罐子。只需搜索<artifactId>您将能够下载依赖项的 .jar。

本指南 http://tech.javayogi.com/blogs/blog4.php/2010/04/19/minimal-spring-security可能会帮助您进行最小的 spring-security 配置。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法将 Spring Security BASIC 身份验证集成到 Jersey/JAX-RS 和 Tomcat 中 的相关文章

随机推荐

  • 截断字符串,但删除字符串的中间而不是结尾

    我想出了这个函数 它将给定的字符串截断为给定的单词数或给定的字符数 以较短者为准 然后 在截掉字符数或字数限制之后的所有内容后 它会在字符串中附加一个 如何从字符串中间删除字符 单词并将其替换为 而不是用 替换末尾的字符 单词 这是我的代码
  • 自动滚动不适用于 vbox 布局

    我需要将表单面板居中对齐 所以我使用了vbox布局 使用后自动滚动不再像以前那样工作 代码如下 Usr VWPanel Ext extend Ext Panel id null rid null closable true autoScro
  • 无法访问类 jdk.xml.internal.JdkXmlUtils

    我正在更新 hybris SAP Commerce 2005 的旧公司实习生扩展 它是使用 API 的扩展 我不知道这个扩展有多少年了 然而 当将它应用到java 11时 我发现了这样的问题 Java 11 导入 javax xml ws
  • 编写 Jena 内置函数

    我正在尝试写一个耶拿内置 http jena apache org documentation inference RULEbuiltins从给定的算法返回一个值 然后与该值进行比较 例如 String rule exRule d rdf
  • 最好的 Python Cassandra 库/包装器? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 I found lazyboy https github com digg lazyboy and pycassa https github c
  • 需要密码才能禁用 Android 设备管理员

    我正在考虑一个具有设备管理员权限的安全应用程序 我想看看当用户尝试在 设置 gt 安全 gt 设备管理员 下以管理员身份取消选中该应用程序时 是否可能需要密码 这将增加一个障碍 不允许用户轻易卸载应用程序 因为他们首先需要从应用程序中删除管
  • PHP-解码 JSON

    我有以下脚本从 API 获取搜索结果 然后对数组进行切片并转储它 我在将 JSON 解码为数组时遇到问题 它返回Array 0 这是一个 WordPress 简码 以下是从 api 获取的 Json 示例 barcode 000015426
  • Java:如何获取xml节点路径

    我有以下 xml
  • 我如何知道 UdpClient 是否已关闭/处置?

    我通过通常的异步回调从 UdpClient 接收数据 private void OnUdpData IAsyncResult result byte data udpReceive EndReceive result ref receive
  • 将一个类的所有对象放入一个列表中

    我有一个 C 类 称为 C 当我执行该程序时 我创建该类的新对象 在某些时候 我需要创建一个列表List
  • 如何将 Ruby 数组从 Heroku 控制台导出为 CSV?

    我希望将数组从 heroku 控制台导出到本地 CSV 文件中 在我目前的情况下 我每天都有一个 rake 任务 它寻找谈论我的应用程序的推文 我想分析这些推文 看看它们是什么时间出现的 等等 heroku run console twee
  • 录制视频时的音频音量

    因此 经过大量搜索后 我找到了允许在录制视频的同时播放背景音频的代码块 我已将所述代码块粘贴在下面 fileprivate func setBackgroundAudioPreference guard allowBackgroundAud
  • 枚举内的枚举

    我想在 java 中的 sql 查询的枚举中创建一个枚举 比如我想说table create它会返回 CREATE TABLE 或database create它会返回创建数据库 我怎样才能做到这一点 enum SQL table ALTE
  • 未找到 Phonegap 3.0 IOS 插件

    我在 XCode 中收到此错误 2013 08 23 14 36 18 284 Tell The DJ 14955 c07 ERROR Plugin Device not found or is not a CDVPlugin Check
  • 我可以使用 UriTemplate 将非字符串传递给 WCF RESTful 服务吗?

    我可以执行以下操作吗 OperationContract WebGet UriTemplate foo id string GetFoo int id 我希望我的服务既可以作为 RESTful 服务 又可以作为 RPC 样式的 SOAP 服
  • 如何在C++中链接头文件

    我是使用头文件进行 C 编程的新手 这是我当前的代码 a h ifndef a H define a H namespace hello class A int a public void setA int x int getA endif
  • 如何在 Swift 中从文件夹中获取 UIImage 数组?

    我有一个像这样的普通 Xcode 项目 请注意 有一个名为 images 的文件夹 它是一个实际的文件夹 而不仅仅是一个组 它包含 25 个 png 图像 我想做的就是做一个array of UIimage与每一个图像 或者甚至是图像名称或
  • 使用 register_shutdown_function() 处理 PHP 中的致命错误

    根据对此答案的评论 https stackoverflow com questions 4409426 can i hook a method to my php file that if any page crashes should e
  • 模糊错误表明我的组件名称以零开头

    我收到一个晦涩的错误 我的组件名称为零 但我的组件中没有一个名称是零 这个错误很难追踪 任何人都知道问题可能是什么 这样我就可以朝着正确的方向前进来解决它 vendor js 66537 Vue warn 无效的组件名称 0 组件名称只能包
  • 无法将 Spring Security BASIC 身份验证集成到 Jersey/JAX-RS 和 Tomcat 中

    我正在尝试将 BASIC 身份验证添加到我使用 Jersey JAX RS 和 Tomcat Apache 7 0 创建的 RESTful Web 服务 将来我想在 WebSphere 上部署此 Web 服务 因此我选择在我的项目中使用 S