【发布时间】:2015-03-29 19:39:21
【问题描述】:
我想在我的 jsf 项目中使用 Spring Security,但使用最少的 xml 配置。但是当我使用基于注释的配置时:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("123456").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().access("hasRole('ROLE_USER')")
.and()
.formLogin()
.and()
.httpBasic();
}
}
我收到此错误:
org.springframework.context.ApplicationContextException: Custom context class [com.spring.SecurityConfig] is not of type [org.springframework.web.context.ConfigurableWebApplicationContext]
安全配置类应该实现 ConfigurableWebApplicationContext 吗?如果是的话怎么做?如果没有,解决办法是什么?
根据 spring 教程,spring security 只需要 2 个类,一个是我在上面粘贴的,另一个是:
public class SecurityWebApplicationInitializer extends
AbstractSecurityWebApplicationInitializer {
public SecurityWebApplicationInitializer() {
super(SecurityConfig.class);
}
}
还有我的 web.xml:
<?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>OnlineLibrary</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextClass</param-name>
<param-value>com.spring.SecurityConfig</param-value>
</context-param>
</web-app>
我将这些库用于我的项目: jsf 2.2
spring-security-3.2.4.RELEASE
还有其他 Spring 所需的库,我在 tomcat 7 上运行我的项目。 感谢您提供有用的答案。
【问题讨论】:
标签: jsf spring-security