【发布时间】:2019-01-12 19:41:36
【问题描述】:
我正在使用 Spring4 和 tomcat,我的 web.xml 如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/resources/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<resource-ref>
<description>MySQL Datasource</description>
<res-ref-name>jdbc/informagiovani</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
我的 beans.xml 如下,是空的,因为我需要的所有 beans 都用注释定义:
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
</beans>
我将spring-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="it.informagiovani" />
<mvc:annotation-driven />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
我定义了在 Spring 文档中找到的安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("giacomo").password("230483").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.antMatchers("/addUser").access("hasRole('ADMIN')")
.antMatchers("/saveUser").access("hasRole('ADMIN')")
.and().formLogin();
}
}
当我启动应用程序时,我得到一个:
No bean named 'springSecurityFilterChain' available
怎么了?我想我至少为 spring 安全性混合了注释和 xml 配置,但由于以前的配置,我需要 web.xml 文件。有没有办法解决这个问题或者我必须使用 security-config.xml 而不是我发布的配置类?
谢谢
【问题讨论】:
-
这里有一些解决方案stackoverflow.com/a/23573471/4727991
-
那些解决方案与我的配置完全不同,我没有 WebInitializer,也没有 WebMvcConfigurerAdapter(我在 spring-servlet.xml 中定义了它)。来吧伙计们
-
@James 请在您的问题中包含有关另一个带有
component-scan标签的弹簧配置文件的信息,如有必要,请在我的回答下留下评论。
标签: java spring spring-mvc spring-security spring-4