我正在尝试使用Spring Security实现一个简单的登录(我是Spring Security的新手)。我已经开始使用Spring Security参考,其中包含一个简单的“Hello world”示例(link)。我在Eclipse中准备了一个Web项目(带有EAR项目),文件结构如下所示:
SecurityConfig
和SecurityWebApplicationInitializer
类与上面链接中的类相同(“Hello world”示例)。我还添加了Log4j 2配置文件和主页(login.xhtml)(现在)只打印“test”。稍后它将用作自定义登录页面。
web.xml
只包含一个welcome-file
元素:
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SpringSecurity</display-name>
<welcome-file-list>
<welcome-file>pages/login.xhtml</welcome-file>
</welcome-file-list>
</web-app>
EAR的部署程序集如下所示:
当我在JBoss AS 7.1.1上部署我的EAR时,没有错误,但是当我访问http://localhost:8080/SpringSecurity
时,我的主页正常显示。
我猜测上面的配置应该生成一个默认的登录页面。当我访问我的主页时,应该出现该登录页面,对吗?看起来Spring Security甚至没有加载,访问我的主页时没有任何保护。
我真的不明白为什么这个简单的例子不起作用。有帮助吗?
我目前在我的机器上没有JBoss。但是我能够在tomcat上部署这个例子。
问题当然是war classloader无法在运行时看到spring jar。
可以有两种可能的解决方案来解决这个问题。
lib
目录移动到war的WEB-INF/lib
目录。Class-Path
文件中以及以便携方式明确设置需要这些罐子的其他模块中的MANIFEST.MF
条目。EG
Manifest-Version: 1.0
Class-Path: lib/spring-beans-4.3.9.RELEASE.jar lib/spring-context-4.3.9.RELEASE.jar
以这种方式添加所有必需的罐子,用空格分隔(qazxsw poi)。
注意: - 我使用以下jar来运行此示例。
' '
使用此版本的spring和spring-security,您可能需要对代码进行一些小改动。
spring-aop-5.0.2.RELEASE.jar
spring-beans-5.0.2.RELEASE.jar
spring-context-5.0.2.RELEASE.jar
spring-core-5.0.2.RELEASE.jar
spring-expression-5.0.2.RELEASE.jar
spring-jcl-5.0.2.RELEASE.jar
spring-security-config-5.0.0.RELEASE.jar
spring-security-core-5.0.0.RELEASE.jar
spring-security-crypto-5.0.0.RELEASE.jar
spring-security-web-5.0.0.RELEASE.jar
spring-web-5.0.2.RELEASE.jar
要么
User.withDefaultPasswordEncoder().username("user").password("user").roles("USER").build();
有关所需代码更改的更多详细信息,请参阅User.withUsername("user").password("{noop}user").roles("USER").build();
。