From Documentation

Jump to: navigation, search


Secure Your Application in Spring's Way

Spring Security is a widely-adopted framework. It can also work with ZK without problems. This doesn't even need zkspring-security. This page will show you how to do it. We assume you know the basic of Spring Boot and Spring Security. (You can read a Spring Security guide: Securing a Web Application ) So here we just mention those configurations specific to ZK framework.

ZK Spring Boot Starter

Spring encourages users to start with Spring Boot. So Please include zk spring boot starter, and it will automatically configure for you with most commonly-used settings.

Spring Boot Starter Security

Follow Securing a Web Application, we add the following elements:


Spring Controller

For simplicity, we just register 2 URL mappings:

  • /login: login page
  • /secure/{page}: all secure pages
public class Application {

    public static void main(String[] args) throws Throwable {, args);

    public String login() {
        return "login";

    public String secure(@PathVariable String page) {
        return "secure/" + page;

Then put the corresponding zul under web/zul folder.


Web Security Configuration

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    public static final String ZUL_FILES = "/zkau/web/**/*.zul";
    public static final String[] ZK_RESOURCES = {"/zkau/web/**/js/**", "/zkau/web/**/zul/css/**", "/zkau/web/**/img/**"};
    // allow desktop cleanup after logout or when reloading login page
    public static final String REMOVE_DESKTOP_REGEX = "/zkau\\?dtid=.*&cmd_0=rmDesktop&.*";

    protected void configure(HttpSecurity http) throws Exception {
            .antMatchers(ZUL_FILES).denyAll() // block direct access to zul files
            .antMatchers(HttpMethod.GET, ZK_RESOURCES).permitAll() // allow zk resources
            .regexMatchers(HttpMethod.GET, REMOVE_DESKTOP_REGEX).permitAll() // allow desktop cleanup
            .requestMatchers(req -> "rmDesktop".equals(req.getParameter("cmd_0"))).permitAll() // allow desktop cleanup from ZATS

    public UserDetailsService userDetailsService() {
        UserDetails user =

        return new InMemoryUserDetailsManager(user);

Login Page

No matter how you design a login page, remember to enclose it with a <form> and the login URL you specify in web security config.

    <n:form action="/login" method="POST">
        <grid width="450px">
                <row spans="2" align="right">
                    <button type="reset" label="Reset" /> <button type="submit" label="Submit" />

Download Demo Project

github - zkoss/zkspringboot - zkspringboot-security-demo

For an example without springboot (warfile with spring and zk-spring-security), please refer to: github - zkoss/zkspring - zkspringessentials/zkspringcoresec

Version History

Last Update : 2020/12/8

Version Date Content

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