<window title="Application Window" border="normal" width="100%" height="100%" apply="com.tugrulaslan.controller.IndexController"> <style> <!-- Listbox Header --> .z-listheader-content{ color: green !important; } <!-- Listbox Cell --> .z-listcell-content{ color: red !important; } <!-- Groupbox Header --> .z-caption-content{ color: blue !important; } <!-- Label --> .z-label{ color: yellow !important; } </style> </window>
Category: ZK Framework
Spring ZKoss Customized Login, Logout, Authorization and Authentication
CustomAuthenticationFilter.java
public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter { @Autowired private xxService xService; static final Logger logger = Logger.getLogger(CustomAuthenticationFilter.class); @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { String username = request.getParameter(getUsernameParameter()); String password = request.getParameter(getPasswordParameter()); String server = request.getParameter("server"); if(username.trim().length() == 0 || password.trim().length() == 0){ logger.error("Error empty credentials entered " + username + password + server); throw new BadCredentialsException("Error empty credentials entered!"); } return logonToBO(request, response, username, password, server); } public Authentication logonToBO(HttpServletRequest request, HttpServletResponse response, String username, String password, String server){ List<GrantedAuthority> authorities = collectAuthorities(username, password, server); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password, authorities); if(xService.checkUserEnterpriseLogOn(username, password,server)){ logger.debug("Access granted " + username+ password + server + token); return token; }else{ throw new BadCredentialsException("Error wrong credentials entered!"); } } public List<GrantedAuthority> collectAuthorities(String username, String password, String server){ List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>(); String userAuth = xService.acquireUserAuthorityGroup(username, password, server); if(userAuth.equals("YOUR_CUSTOM_AUTH_VALUE")){ auths.add(new GrantedAuthorityImpl("ROLE_USER")); } if(userAuth.equals("") || userAuth == ""){ logger.error("Unauthorized login " + username); throw new BadCredentialsException("You aren't authorized beyond this point!"); } return auths; } }
CustomAuthenticationProvider.java
public class CustomAuthenticationProvider implements AuthenticationProvider{ public Authentication authenticate(Authentication authentication) throws AuthenticationException { return authentication; } public boolean supports(Class<?> arg0) { return true; } }
CustomLogoutHandler.java
@Autowired private XXService XService; public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { //log out of BO Server XService.terminateEnterpriseSession(); //return the values and log off from spring security as well super.logout(request, response, authentication); }
applicationContext.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.zkoss.org/2008/zkspring/core http://www.zkoss.org/2008/zkspring/core/zkspring-core.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <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" /> <intercept-url pattern="/sectest.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.tugrulaslan.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.tugrulaslan.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.tugrulaslan.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>
login.zul
<?page title="Login" contentType="text/html;charset=UTF-8"?> <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?> <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> <zk> <div style="width: 700px; margin-left: auto; margin-right: auto;"> <div height="90px" width="600px" style="margin-left: auto; margin-right: auto;"> <label value="Login page" style="font-size:16px; font-weight:bold; color:red" /> </div> <window title="User Login Screen" border="normal" width="600px"> <html:form id="loginForm" name="loginForm" action="j_spring_security_check" method="POST" xmlns:html="native"> <grid> <rows> <row> <label value="Username" /> <textbox id="username" name="j_username" width="162px" /> </row> <row> <label value="Password" /> <textbox id="password" type="password" name="j_password" width="162px" /> </row> <row> <label value="Server" /> <combobox id="server" name="server" readonly="true" onCreate="self.setSelectedIndex(0)"> <comboitem value="SERVER 1" label="SERVER 1" /> <comboitem value="SERVER 2" label="SERVER 2" /> </combobox> </row> <row> <label /> <button id="login" type="submit" label="Giris" /> </row> </rows> </grid> <html style="color:red" if="${not empty param.login_error}"> <!-- Here spring failure messages printed --> <![CDATA[Hata: ${SPRING_SECURITY_LAST_EXCEPTION.message} ]]> </html> </html:form> </window> </div> </zk>
Getting selected items in listbox ZK
In one of my project I really needed one of piece of this code and dwelled on that for a long while. I asked for assistance from a senior and he showed me how to, and I would like to share this piece of helpful code with you. In controller class I did not create a simple arraylist for the listbox model just to give you the structure you may implement this code in your project. Normally for one lastly selected item you can use listbox.getSelectedItem.getValue() and acquire the selected item. How about when we have more than one selected items, that is the tricky point
The trick I learnt was that there was an iterator in the listbox, and I saw that I could easily collect the selected items. You will see how I achieved that thanks to the Senior.
Controller
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class) public class BulkAuthController extends SelectorComposer<Component> { @Wire Listbox universeListbox; //I have this simple array list to collect the selected items on listbox not mandatory private ArrayList<String> selectedUniverseListboxItems = new ArrayList<String>(); //here is the trick. public void collectSelectedItems() { Iterator<Listitem> iterator = universeListbox.getSelectedItems() .iterator(); while (iterator.hasNext()) { Listitem listitem = (Listitem) iterator.next(); selectedUniverseListboxItems.add(listitem.getValue().toString()); } } }
Zul Page
<?page title="Listbox Test" contentType="text/html;charset=UTF-8"?> <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?> <zk xmlns:h="http://www.w3.org/1999/xhtml" xmlns:zk="http://www.zkoss.org/2005/zk"> <window id="listboxSample" title="Listbox Test" border="normal" width="900px" apply="com.tugrulaslan.controller.ListboxTestController"> <listbox id="universeListbox" model="${$composer.userUniverseList()}" multiple="true" checkmark="true" height="500px"> <template name="model"> <listitem> <listcell label="${each}" value="${each}"> </listcell> </listitem> </template> </listbox> </window> </zk>
Using html elements in ZK
Well today I had to face this situation since I am a newbie in ZK Framework. In a single line in my code I had to leave a new space between two different components I found the solution first of all we need to add the ZK html namespace in our code and with the namespace we can use zk specialized html elements
<zk xmlns:h="http://www.w3.org/1999/xhtml" xmlns:zk="http://www.zkoss.org/2005/zk"> <window title="ZK HTML Elements" width="300px" border="normal"> <label value="First element"/> <h:br /> <label value="Second Element" /> <h:br /> <h:p>Another<h:br /> break <h:br /> example </h:p> </window> </zk>
Instant copy while typing in a textbox – ZK
I have been looking for the exact thing online and tried entire week how to copy instant typing to another textbox or label in zk, and finally I have come to the end of making one on my own. Normally you can do the same in zul but what I really needed in work project is with the controller. The below evaluate the code
zul:
<?page title="Instant Copy" contentType="text/html;charset=UTF-8"?> <zk> <window title="Instant Copy" apply="com.tugrulaslan.controller.InstantController" border="normal" width="300px"> <grid> <rows> <row> <label value="Source" /> <textbox id="dataTXT" /> </row> <row> <label value="Instant Copy" /> <textbox id="copyTXT" readonly="true" /> </row> </rows> </grid> </window> </zk>
controller
package com.akbank.controller; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import org.zkoss.zhtml.Messagebox; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.Execution; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.Page; import org.zkoss.zk.ui.Session; import org.zkoss.zk.ui.Sessions; import org.zkoss.zk.ui.metainfo.ComponentInfo; import org.zkoss.zk.ui.select.SelectorComposer; import org.zkoss.zk.ui.select.annotation.Listen; import org.zkoss.zk.ui.select.annotation.VariableResolver; import org.zkoss.zk.ui.select.annotation.Wire; import org.zkoss.zk.ui.select.annotation.WireVariable; import org.zkoss.zk.ui.util.Clients; import org.zkoss.zul.Button; import org.zkoss.zul.Label; import org.zkoss.zul.ListModelList; import org.zkoss.zul.Listbox; import org.zkoss.zul.Listitem; import org.zkoss.zul.SimpleListModel; import org.zkoss.zul.Textbox; import org.zkoss.zul.Window; import org.zkoss.zk.ui.event.*; @VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class) public class InstantController extends SelectorComposer<Component> { @Wire Textbox dataTXT; @Wire Textbox copyTXT; @Listen("onChanging = #dataTXT") public void butttonnn(InputEvent inputEvent){ copyTXT.setValue(inputEvent.getValue()); } }
Adding zk archetype to maven in eclipse
on eclipse or sts click on window > preferences
and on the left pane menu go to Maven and expand the menu choose Archetypes here click on add remote catalog and type in
Catalog file: http://mavensync.zkoss.org/maven2/
Description: zk
click on okay. When you create a new maven project from the catalog choose zk and select your favorite zk maven project