使用JSF2.2,Spring 4.0和SpringSecurity 3.2无法访问ManagedBean

时间:2014-02-02 11:52:13

标签: spring-security jsf-2.2 spring-4

我正在使用JSF 2.2,Spring Framework 4.0和Spring Security 3.2开发动态Web应用程序。我首先集成了JSF和Spring框架(JSF Page => ManagedBean => Service => DAO)。它工作正常。但是当我介绍Spring Security时,我无法访问JSF页面中的ManagedBean并且根本没有错误。甚至没有调用ManagedBean(甚至没有调用构造函数)。我已经为这个问题冲浪了2天了,但找不到任何解决方案。

我的配置文件如下:

faces-config.xml中

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">

    <application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

</faces-config>

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>my-app-ui</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- Spring Framework -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <!-- Spring context config locations -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/applicationContext.xml</param-value>
        <param-value>classpath*:/securityConfig.xml</param-value>
    </context-param>

    <!-- PROJECT STAGE START FOR DEVELOPEMENT MARK IT AS DEVELOPMENT. FOR TESTING / PRODUCTION REMOVE THIS -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <!-- Spring Security -->
    <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>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>


    <!-- JSF -->
    <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>
    </servlet-mapping>
</web-app>

applicationconContext.xml

<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" 
      xmlns:tx="http://www.springframework.org/schema/tx"   
      xsi:schemaLocation="http://www.springframework.org/schema/beans   
                            http://www.springframework.org/schema/beans/spring-beans.xsd  
                         http://www.springframework.org/schema/context   
                         http://www.springframework.org/schema/context/spring-context.xsd">  

    <context:annotation-config />
    <context:component-scan base-package="com.myapp.managedbean"/>
    <context:component-scan base-package="com.myapp.service"/>
    <context:component-scan base-package="com.myapp.domain"/>
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/myDS" />
    </bean>

    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>  

securityConfig.xml

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

    <debug />


    <http pattern="/resources/**" security="none" />
    <http pattern="/login*" security="none" /> 

    <http auto-config="true" use-expressions="true" disable-url-rewriting="true" >
        <intercept-url pattern="/**" access="authenticated" />
        <form-login authentication-failure-url="/loginfailed.html" 
            default-target-url="/" always-use-default-target="true"/>

        <access-denied-handler error-page="/denied.html"/>

        <session-management invalid-session-url="/session-expire.html" session-fixation-protection="none" />

        <logout delete-cookies="JSESSIONID" invalidate-session="true" />

        <http-basic />
    </http>

    <authentication-manager>
      <authentication-provider>
        <user-service>
          <user name="user_admin" password="password" authorities="ROLE_USER,ROLE_ADMIN" />
          <user name="user" password="password" authorities="ROLE_USER" />
        </user-service>
      </authentication-provider>
    </authentication-manager>
</b:beans> 

第一JSF-page.xhtml

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets">
<head>

</head>
<body>

Managed bean data: #{myMB.fetchDataFromService()}


</body>
</html>

ManagedBean

@Component("myMB")
@Scope("session")
public class MyMB implements Serializable {
    public MyMB () {
        System.out.println("Constructor called");
    }

    @Autowired  
    @Qualifier("myService")  
    public MyService myService; 

    Public String fetchDataFromService() {
        System.out.println("Invoking Service");  
        return myService.getMessage();  
    }
}

MyService

public interface MyService {
    public String getMessage();  
}

MyServiceImpl

@Service("myService")  
public class MyServiceImpl implements MyService {  

     @Override  
     public String getMessage() {  
          return "JSF and Spring Integrtion - Done";  
     }  
}  

如果我从Web.xml中删除“DelegatingFilterProxy”,一切正常。请帮助我。

我正在使用Eclipse和tomcat服务器。

0 个答案:

没有答案