Custom resolver to explose logged in user info for ZK (Spring Security)

To expose spring security’s authentication info with object name “authentication” in ZK CE, the following simple resolver is useful.

import org.springframework.security.context.SecurityContextHolder;
import org.zkoss.xel.VariableResolver;
/**
* Resolver to make “authentication” object available for zk EL.
*
* @author Anil
*
*/
public class SecurityResolver implements VariableResolver {
public Object resolveVariable(String name) {
if (“authentication”.equals(name)) {
return SecurityContextHolder.getContext().getAuthentication();
}
return null;
}
}

This is resolver will be used as below:

<?variable-resolver class=”com.gavs.zk.common.SecurityResolver”?>

<toolbar id=”toolbar” style=”margin-top: -15px;”>

<toolbarbutton image=”/images/icons/small/home.ico” label=”Home”   if=”${authentication.principal == ‘roleAnonymous’}” style=”font-size: 11px; font-weight: bold;”>

<attribute name=”onClick”>

<![CDATA[Executions.sendRedirect(“/index.zul”); ]]>

</attribute>

</toolbarbutton>

</toolbar>

Here  “Home” link gets displayed only if user has not yet logged into system.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: