gpt4 book ai didi

java - Spring Boot中Spring Security的XML配置

转载 作者:搜寻专家 更新时间:2023-10-31 20:11:16 26 4
gpt4 key购买 nike

我想对 Spring Security 使用基于 XML 的配置。第一个想法是对用户密码使用 SHA-256 或任何其他哈希函数。我找不到用普通 java 解决这个问题的好方法。所以我开始在 xml 中配置东西。这就是重点,当它开始变得有趣时。

我的配置:

  • spring-boot 1.1.8.RELEASE
  • spring-boot-starter-* 在 1.1.8
  • tomcat-embed-jasper:8.0.8

spring-security.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd>

<http pattern="/css/**" security="none"/>
<http pattern="/login.html*" security="none"/>

<http>
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login login-page='/login.html'/>
</http>

<authentication-manager>

<authentication-provider>
<user-service>
<user name="admin" password="admin"
authorities="ROLE_USER, ROLE_ADMIN"/>
<user name="bob" password="bob"
authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>

</beans:beans>

我在类中加载 xml 文件,可以在其中找到 public static void main:

@Configuration
@ComponentScan
@EnableAutoConfiguration
@Order(HIGHEST_PRECEDENCE)
@ImportResource({
"/spring-security.xml"
})
public class PhrobeBootApplication extends SpringBootServletInitializer {
...
}

但是我在任何页面加载时都会遇到以下异常:

[ERROR] org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
...

所以看起来resources/WEB-INF/web.xml 的配置没有加载,如果我有一个好的understanding from the documentation,我应该在不带 Boot 的情况下使用普通 Spring 时使用它。 (应配置过滤器)。我说得对吗?

为什么会出现这个错误?有没有更好的方法在 spring-boot 中使用基于 xml 的 spring-security 配置? web.xml 甚至由 tomcat 加载吗?

最佳答案

我遇到了同样的问题,然后我更改了 XML 文件的路径并将其保留在 src/main/resources/spring 中。它工作正常。

@SpringBootApplication

@ImportResource("classpath:/spring/spring-security.xml")

关于java - Spring Boot中Spring Security的XML配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26332051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com