Java Spring4安全配置
我将Spring4与tomcat一起使用,我的web.xml如下:Java Spring4安全配置,java,spring,spring-mvc,spring-security,spring-4,Java,Spring,Spring Mvc,Spring Security,Spring 4,我将Spring4与tomcat一起使用,我的web.xml如下: <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/resources/beans.xml</param-value> </context-param> <listener> <list
<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>
当我启动应用程序时,我得到:
No bean named 'springSecurityFilterChain' available
怎么了?我想我混合了注释和xml配置,至少是为了spring的安全性,但由于之前的配置,我需要web.xml文件。有没有办法解决这个问题,或者我必须使用security-config.xml而不是我发布的配置类
谢谢我想问题出在您的
beans.xml
配置文件中。您没有声明组件扫描
属性,Spring也不知道您有java安全配置
只需将
添加到beans.xml中,错误就会消失。以下是一些解决方案,这些解决方案与我的配置完全不同,我没有WebInitializer,也没有WebMVCConfigureAdapter(我在spring servlet.xml中定义了它)。来吧guys@James请在您的问题中包含关于另一个带有组件扫描
标记的spring配置文件的信息,如果有必要,请在我的答案下留下评论。
<?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>
@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