From Documentation

Jump to: navigation, search

Spring Security 2.0 and 3.0 build on Acegi Security's solid foundations, adding many new features. The framework provides a comprehesive set of authentication and access-control services for your applications.


Running a sample application

  1. Download the war file
    The tutorial sample is borrowed from Spring Security 3.0, and has been modified to work with ZK seamlessly.
  2. Deploy the war file
  • Tomcat 5.5 and above.

Copy zkspringsec302.war to the $Tomcat_Home/webapps/ folder of Tomcat server and restart Tomcat.

  1. Use your browser to visit:
    username/password is rod/koala

Prerequisites of using Spring Security

To make Spring Security work with ZK properly, you have to copy the following jars to /WEB-INF/lib.

  • ZK Spring Core & Security Integration Library jar files
    • zkspring-core.jar
    • zkspring-security.jar
  • Spring Security library jar files
    • aopalliance-1.0.jar
    • aspectjweaver-1.6.8.jar
    • commons-codec-1.3.jar
    • commons-collections-3.2.jar
    • commons-lang.jar
    • commons-logging-1.1.1.jar
    • jstl-1.1.2.jar
    • log4j-1.2.14.jar
    • spring-security-acl-3.0.2.RELEASE.jar
    • spring-security-config-3.0.2.RELEASE.jar
    • spring-security-core-3.0.2.RELEASE.jar
    • spring-security-taglibs-3.0.2.RELEASE.jar
    • org.springframework.aop-3.0.2.RELEASE.jar
    • org.springframework.asm-3.0.2.RELEASE.jar
    • org.springframework.beans-3.0.2.RELEASE.jar
    • org.springframework.context-3.0.2.RELEASE.jar
    • org.springframework.core-3.0.2.RELEASE.jar
    • org.springframework.expression-3.0.2.RELEASE.jar
    • org.springframework.transaction-3.0.2.RELEASE.jar
    • org.springframework.web-3.0.2.RELEASE.jar
    • org.springframework.web.servlet-3.0.2.RELEASE.jar
    • standard-1.1.2.jar

Note: Please refer to Prerequisite for using Spring Security if using ZK Spring 1.1.0 release

Configure the /WEB-INF/web.xml file

To make Tomcat work with Spring Security properly, you shall specify the location of spring-security configuration file, and define listener of spring, and filter of spring-security as follows:

       - Location of the XML file that defines the root application context
       - Applied by ContextLoaderListener.

       - Loads the root application context of this web app at startup.
       - The application context is then available via
       - WebApplicationContextUtils.getWebApplicationContext(servletContext).
       - Spring Security Filter Chains

Create /WEB-INF/applicationContext-security.xml

Create application-security.xml file under $myApp/WEB-INF/. This file include required configuration of spring-security.

   - Spring namespace-based configuration
<beans:beans xmlns=""
       - Enable the @Secured annotation to secure service layer methods
	<global-method-security secured-annotations="enabled">

       - Secure the page per the URL pattern
    <http auto-config="true">
        <intercept-url pattern="/secure/extreme/**" access="ROLE_SUPERVISOR"/>
        <intercept-url pattern="/secure/**" access="IS_AUTHENTICATED_REMEMBERED" />
        <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <!-- use own login page rather than the default one -->
        <form-login login-page="/login.zul" authentication-failure-url="/login.zul?login_error=1"/>
        <!-- Following is list of ZK Spring Security custom filters. 
        	They needs to be exactly in the same order as shown below in order to work.  -->
        <custom-filter ref="zkDesktopReuseFilter" position="FIRST" />
        <custom-filter ref="zkDisableSessionInvalidateFilter" before="FORM_LOGIN_FILTER"/>
        <custom-filter ref="zkEnableSessionInvalidateFilter" before="FILTER_SECURITY_INTERCEPTOR"/>
        <custom-filter ref="zkLoginOKFilter" after="FILTER_SECURITY_INTERCEPTOR"/>
        <custom-filter ref="zkError403Filter" after="LOGOUT_FILTER"/>

    Usernames/Passwords are
        <password-encoder hash="md5"/>
            <user name="rod" password="a564de63c2d0da68cf47586ee05984d7" authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
	        <user name="dianne" password="65d15fe9156f9c4bbffd98085992a44e" authorities="ROLE_USER,ROLE_TELLER" />
            <user name="scott" password="2b58af6dddbd072ed27ffc86725d7d3a" authorities="ROLE_USER" />
            <user name="peter" password="22b5c9accc6e1ba628cedc63a72d57f8" authorities="ROLE_USER" />

Notice that here only zkspring-security.xsd is referenced in the namespace declaration. In case your application needs ZK Spring core support please include zkspring-core.xsd as well. For Zk Spring 1.1.0 release namespace configuration refer to Create /WEB-INF/applicationContext-security.xml under ZK Spring 1.1.0 Release configuration details section.

Define which service to be secured

Use Spring Security annotation @Secured to protect the calling of a business service as follows:

 public interface BankService {
     public Account readAccount(Long id);
    public Account[] findAccounts();
     public Account post(Account account, double amount);

Define which ZK event to be secured

In /WEB-INF/applicationContext-security.xml, define required configurations of the namespace of zk, and specify which ZK event to secure as follows,

   - Spring namespace-based configuration
<beans:beans xmlns=""

    <http ...>

 - Secure the ZK event processing per the event name and ZK component path pattern
<zksp:zk-event login-template-close-delay="5">
<zksp:intercept-event event="onClick" path="//**/btn_*" access="ROLE_TELLER"/>
<zksp:intercept-event path="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>

  • xmlns:zksp=" tells the Spring Security engine that we will use the ZK Spring Security namespace configuration and define the name space to zksp.
  • tells the Spring Security engine where to find such ZK Spring Security namespace configuration schema.
  • <zksp:zk-event> tells the Spring Security engine we want to secure ZK event processing. This will configure the necessary listeners , filters, and Spring beans automatically. In this example, login-template-close-delay="5" tells ZK to CLOSE the login window automatically in five(5) seconds if login successfully; zero(0) means close the login window immediately; and a negative value means wait for users operation.
  • <zksp:intercept-event event="onClick" path="//**/btn_*" access="ROLE_TELLER"/> tells the Spring Security engine which ZK event and components we want to secure. In this example, it says that any onClick event that is fired to the ZK components whose id starts with btn_ will be checked against the ROLE_TELLER authority.
  • <zksp:intercept-event path="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/> tells that all anonymous users can access to all events and all components.

Note: For Zk Spring 1.1.0 configuration please refer to Define which ZK event to be secured section under ZK Spring release 1.1.0 configuration details

The ZUML Page

In the following example, ZK prompts end user to login when he/she attempts to change the balance of account since the onClick event of Button component has been secured by Spring-Security.

<?variable-resolver class="org.zkoss.spring.DelegatingVariableResolver"?>
<window title="Accouts" border="normal" width="500px">
        void adjBalance(Button btn) {
            double bal = Double.parseDouble((String)btn.getAttribute("bal"));
            //get the account object
            bigbank.Account a = bankService.readAccount(btn.getAttribute("aid"));
            //change the account balance
  , bal);
            //update the account balance on the browser
            <row forEach="${accounts}">
                <label value="${}"/>
                <label value="${each.holder}"/>
                <label id="bal_${}" value="${each.balance}"/>
                <button id="btn_m20_${}" label="-$20" onClick="adjBalance(self)">
                    <custom-attributes aid="${}" bal="-20"/>
                <button id="btn_m5_${}" label="-$5" onClick="adjBalance(self)"> 
                    <custom-attributes aid="${}" bal="-5"/>
<button label="Home" href="/index.zul"/>
<button label="Logout" href="/j_spring_security_logout"/>

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.