[英]Spring security initialization failed
我正在尝试使用Spring Security(我是Spring Security的新手)来实现简单的登录。 我从Spring Security参考开始,该参考包含一个简单的“ Hello world”示例( link )。 我已经在Eclipse中准备了一个Web项目(带有EAR项目),文件结构如下所示:
SecurityConfig
和SecurityWebApplicationInitializer
类与上面的链接(“ Hello world”示例)中的类相同。 我还添加了Log4j 2配置文件和主页(login.xhtml),该文件(现在)仅显示“测试”。 以后它将用作自定义登录页面。
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模块仅需要弹簧罐(ear中的任何其他模块都不需要),则可以将这些spring jar从ear的lib
目录移到war的WEB-INF/lib
目录。
如果其他模块也需要弹簧罐,则可以在战争的MANIFEST.MF
文件和需要这些罐的其他模块中以可移植的方式显式设置Class-Path
条目。
例如
Manifest-Version: 1.0
Class-Path: lib/spring-beans-4.3.9.RELEASE.jar lib/spring-context-4.3.9.RELEASE.jar
以这种方式添加所有需要的jar,并用sapce( ' '
)分隔。
注意:-我使用以下jar使该示例运行。
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
使用此版本的spring和spring-security,您可能必须对代码进行一些小的更改。
User.withDefaultPasswordEncoder().username("user").password("user").roles("USER").build();
要么
User.withUsername("user").password("{noop}user").roles("USER").build();
请参阅-Spring Boot PasswordEncoder错误以获取有关所需代码更改的更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.