spring security不会排除service和dao层

时间:2014-04-11 10:10:22

标签: spring spring-security

我在弹簧安全方面遇到了大麻烦,这让我整整一周都找不到问题所在。

我检查了stackoverflow以及博客和spring文档,但我无法得出结论。问题是我有我的图层服务和dao,它们都在安全实现之前工作但令人惊讶的是弹簧安全块并且不会排除我的界面!另一件事是在我保留自定义spring类的安全包中有一个业务类可以处理像魅力一样的请求但是当我尝试使用我的接口时它将无法工作!我不能遗憾地创建一个接口的实例。下面我要添加我的代码和堆栈跟踪。

我的项目结构为*

com.xxx.dao => where my dao interfaces reside

com.xxx.service => where my service interfaces reside

com.xxx.security => where my custom spring security extended classes reside, also there is a class which does business logic in the case my service and dao interface were supposed to do

我正在使用

ZK 6.5.2

Spring Framework and Security 3.1.2

的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <description><![CDATA[My ZK Application]]></description>
    <display-name>CMC_REPORTING</display-name>

    <!-- Spring Security Configuration -->
    <filter><!-- the filter-name must be preserved, do not change it! -->
        <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>

    <!-- Spring configuration -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/applicationContext.xml 
        /WEB-INF/applicationContext-security.xml
    </param-value>
    </context-param>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Initialize spring context -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Enable webapp Scopes -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>

    <!-- log4j config -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>


    <!-- ZK -->
    <listener>
        <description>ZK listener for session cleanup</description>
        <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
    </listener>
    <servlet>
        <description>ZK loader for ZUML pages</description>
        <servlet-name>zkLoader</servlet-name>
        <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>

        <!-- Must. Specifies URI of the update engine (DHtmlUpdateServlet). It 
            must be the same as <url-pattern> for the update engine. -->
        <init-param>
            <param-name>update-uri</param-name>
            <param-value>/zkau</param-value>
        </init-param>
        <!-- Optional. Specifies whether to compress the output of the ZK loader. 
            It speeds up the transmission over slow Internet. However, if you configure 
            a filter to post-processing the output, you might have to disable it. Default: 
            true <init-param> <param-name>compress</param-name> <param-value>true</param-value> 
            </init-param> -->
        <!-- [Optional] Specifies the default log level: OFF, ERROR, WARNING, INFO, 
            DEBUG and FINER. If not specified, the system default is used. <init-param> 
            <param-name>log-level</param-name> <param-value>OFF</param-value> </init-param> -->
        <load-on-startup>1</load-on-startup><!-- Must -->
    </servlet>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>*.zul</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>*.zhtml</url-pattern>
    </servlet-mapping>
    <!-- [Optional] Uncomment it if you want to use richlets. <servlet-mapping> 
        <servlet-name>zkLoader</servlet-name> <url-pattern>/zk/*</url-pattern> </servlet-mapping> -->
    <servlet>
        <description>The asynchronous update engine for ZK</description>
        <servlet-name>auEngine</servlet-name>
        <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>

        <!-- [Optional] Specifies whether to compress the output of the ZK loader. 
            It speeds up the transmission over slow Internet. However, if your server 
            will do the compression, you might have to disable it. Default: true <init-param> 
            <param-name>compress</param-name> <param-value>true</param-value> </init-param> -->
        <!-- [Optional] Specifies the AU extension for particular prefix. <init-param> 
            <param-name>extension0</param-name> <param-value>/upload=com.my.MyUploader</param-value> 
            </init-param> -->
    </servlet>
    <servlet-mapping>
        <servlet-name>auEngine</servlet-name>
        <url-pattern>/zkau/*</url-pattern>
    </servlet-mapping>

    <!-- [Optional] Uncomment if you want to use the ZK filter to post process 
        the HTML output generated by other technology, such as JSP and velocity. 
        <filter> <filter-name>zkFilter</filter-name> <filter-class>org.zkoss.zk.ui.http.DHtmlLayoutFilter</filter-class> 
        <init-param> <param-name>extension</param-name> <param-value>html</param-value> 
        </init-param> <init-param> <param-name>compress</param-name> <param-value>true</param-value> 
        </init-param> </filter> <filter-mapping> <filter-name>zkFilter</filter-name> 
        <url-pattern>your URI pattern</url-pattern> </filter-mapping> -->

    <!-- DSP (optional) Uncomment it if you want to use DSP However, it is turned 
        on since zksandbox uses DSP to generate CSS. <servlet> <servlet-name>dspLoader</servlet-name> 
        <servlet-class>org.zkoss.web.servlet.dsp.InterpreterServlet</servlet-class> 
        <init-param> <param-name>class-resource</param-name> <param-value>true</param-value> 
        </init-param> </servlet> <servlet-mapping> <servlet-name>dspLoader</servlet-name> 
        <url-pattern>*.dsp</url-pattern> </servlet-mapping> -->

    <!-- [Optional] Session timeout -->
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

    <!-- [Optional] MIME mapping -->
    <mime-mapping>
        <extension>doc</extension>
        <mime-type>application/vnd.ms-word</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>gif</extension>
        <mime-type>image/gif</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>htm</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>html</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>jpeg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>jpg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>js</extension>
        <mime-type>text/javascript</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>pdf</extension>
        <mime-type>application/pdf</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>png</extension>
        <mime-type>image/png</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>txt</extension>
        <mime-type>text/plain</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xls</extension>
        <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xml</extension>
        <mime-type>text/xml</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>zhtml</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>zul</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>

    <welcome-file-list>
        <welcome-file>index.zul</welcome-file>
        <welcome-file>index.zhtml</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
</web-app>

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

    <!-- Moved the component scan to applicationContext.xml -->
    <context:annotation-config />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/images/**" location="/images/" />

    <!-- Bean definitions -->
    <bean id="businessObjectsDAO" class="com.xxx.dao.BusinessObjectsDAOImpl" />
    <bean id="businessObjectsService" class="com.xxx.service.BusinessObjectsServiceImpl" />
    <bean id="databaseDAO" class="com.xxx.dao.DatabaseDAOImpl" />
    <bean id="databaseService" class="com.xxx.service.DatabaseServiceImpl" />
    <bean id="officeSuiteDAO" class="com.xxx.dao.OfficeSuiteDAOImpl" />
    <bean id="officeSuiteService" class="com.xxx.service.OfficeSuiteServiceImpl" />

</beans>

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

 <!-- I have defined all packages -->
 <context:component-scan base-package="com.xxx.controller, com.xxx.dao, com.xxx.domain, com.xxx.security, com.xxx.service" />
</beans>

的applicationContext-security.xml文件

    <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"
    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">

    <!-- Enabling annotation use in classes -->
    <global-method-security pre-post-annotations="enabled" />

    <http auto-config="false" entry-point-ref="loginUrlAuthenticationEntryPoint">
        <!-- ZK au has to be accessed anonymously -->
        <intercept-url pattern="/zkau/**" access="IS_AUTHENTICATED_ANONYMOUSLY"
            requires-channel="any" />

        <!-- allowing access to css resources -->
        <intercept-url pattern="/css/**" access="IS_AUTHENTICATED_ANONYMOUSLY"
            requires-channel="any" />

        <!-- allowing access to images resources -->
        <intercept-url pattern="/images/**" access="IS_AUTHENTICATED_ANONYMOUSLY"
            requires-channel="any" />

        <!-- allowing access to layout resources -->
        <intercept-url pattern="/layout/**" access="IS_AUTHENTICATED_ANONYMOUSLY"
            requires-channel="any" />

        <!-- allowing access to login page -->
        <intercept-url pattern="/login.zul" access="IS_AUTHENTICATED_ANONYMOUSLY" />

        <!-- After defining other resources secure all pages and links -->
        <intercept-url pattern="/**" access="ROLE_USER" />

        <!-- Custom Login Filter -->
        <custom-filter ref="customAuthenticationFilter"
            position="FORM_LOGIN_FILTER" />

        <!-- Custom Logout Filter -->
        <custom-filter ref="customLogoutFilter" position="LOGOUT_FILTER" />
    </http>

    <!-- Custom Authentication Filter -->
    <beans:bean id="customAuthenticationFilter"
        class="com.xxx.security.CustomAuthenticationFilter">
        <beans:property name="authenticationManager" ref="authenticationManager" />
        <beans:property name="authenticationFailureHandler"
            ref="failureHandler" />
        <beans:property name="authenticationSuccessHandler"
            ref="successHandler" />
    </beans:bean>

    <!-- Custom Authentication Filter -->
    <authentication-manager alias="authenticationManager">
        <authentication-provider ref="customAuthenticationProvider" />
    </authentication-manager>

    <!-- Custom Authentication Provider For details check the regarding class -->
    <beans:bean id="customAuthenticationProvider"
        class="com.xxx.security.CustomAuthenticationProvider" />

    <!-- Successful login handler -->
    <beans:bean id="successHandler"
        class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
        <beans:property name="defaultTargetUrl" value="/index.zul" />
    </beans:bean>

    <!-- Failure login handler -->
    <beans:bean id="failureHandler"
        class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
        <beans:property name="defaultFailureUrl" value="/login.zul?login_error=1" />
    </beans:bean>

    <!-- Custom Logout Handler -->
    <beans:bean id="customLogoutHandler" class="com.xxx.security.CustomLogoutHandler" />
    <beans:bean id="customLogoutFilter"
        class="org.springframework.security.web.authentication.logout.LogoutFilter">
        <beans:constructor-arg value="/login.zul" />
        <beans:constructor-arg>
            <beans:list>
                <beans:ref bean="customLogoutHandler" />
            </beans:list>
        </beans:constructor-arg>
        <beans:property name="filterProcessesUrl" value="/j_spring_security_logout" />
    </beans:bean>

    <!-- Access Denied Handler -->
    <beans:bean id="accessDeniedHandler"
        class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
        <beans:property name="errorPage" value="/accessDenied.zul" />
    </beans:bean>

    <!-- Customized Login Entry Point -->
    <beans:bean id="loginUrlAuthenticationEntryPoint" scope="prototype"
        class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <beans:property name="loginFormUrl" value="/login.zul" />
    </beans:bean>

</beans:beans> 

*** This is the trick***

BusinessObjects DAO

@Secured(value = { "IS_AUTHENTICATED_ANONYMOUSLY" })
public interface BusinessObjectsDAO {

//business code

}

BusinessObjects服务

 @Service(value = "businessObjectsService")
   public class BusinessObjectsServiceImpl implements BusinessObjectsService {

   //business code

   }

BusinessObjects DAO      @Repository(value =“businessObjectsDAO”)         公共类BusinessObjectsDAOImpl实现BusinessObjectsDAO {
        //业务代码         }

Stack Trace *

org.zkoss.zel.ELException: java.lang.NullPointerException
    org.zkoss.zel.BeanELResolver.invoke(BeanELResolver.java:614)
    org.zkoss.zel.CompositeELResolver.invoke(CompositeELResolver.java:161)
    org.zkoss.xel.zel.XelELResolver.invoke(XelELResolver.java:148)
    org.zkoss.zel.impl.parser.AstValue.getValue(AstValue.java:166)
    org.zkoss.zel.impl.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    org.zkoss.xel.zel.ELXelExpression.evaluate(ELXelExpression.java:40)
    org.zkoss.zk.xel.impl.SimpleEvaluator.evaluate(SimpleEvaluator.java:72)
    org.zkoss.zk.ui.metainfo.AbstractEvalRef.evaluate(AbstractEvalRef.java:48)
    org.zkoss.zk.xel.ExValue.getValue(ExValue.java:109)
    org.zkoss.zk.ui.metainfo.Property.getValue(Property.java:134)
    org.zkoss.zk.ui.metainfo.Property.assign0(Property.java:227)
    org.zkoss.zk.ui.metainfo.Property.assign(Property.java:169)
    org.zkoss.zk.ui.metainfo.ComponentInfo.applyProperties(ComponentInfo.java:729)
    org.zkoss.zk.ui.impl.AbstractUiFactory.newComponent(AbstractUiFactory.java:106)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:809)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:778)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:687)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:651)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:824)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:778)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:687)
    org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:651)
    org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:136)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.channel.ChannelProcessingFilter.doFilter(ChannelProcessingFilter.java:144)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)

0 个答案:

没有答案