我想使用基于XML的配置来实现Spring Security。首先考虑使用SHA-256或任何其他哈希函数来对用户密码进行加密。但是,我没有找到一个很好的方法来解决这个问题,所以我开始在xml中配置这些内容。这就是当它变得有趣的时候。
我的配置如下:
看起来像是从
为什么会出现这个错误?有没有更好的方法在spring-boot中使用基于xml的spring-security配置?web.xml是否由tomcat加载?
我的配置如下:
- spring-boot 1.1.8.RELEASE
- spring-boot-starter-* at 1.1.8
- tomcat-embed-jasper:8.0.8
<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
的配置没有加载,如果我正确地理解了文档,我应该在仅使用普通的spring时使用它,而不是使用boot。(应该配置过滤器)。我是对的吗?为什么会出现这个错误?有没有更好的方法在spring-boot中使用基于xml的spring-security配置?web.xml是否由tomcat加载?