0

[Spring Security] How to check that user signed in?

asked 2010-04-15 06:04:46 +0800

ady1981 gravatar image ady1981
120 4

I have found that anonymous user has role="ROLE_ANONYMOUS" and name="anonymousUser".

But it's not obviously for me how to check with zk tags whether user signed in or not...

So, this does not work:
<toolbarbutton id="signIn"
if="${sec:isAllGranted('ROLE_ANONYMOUS')}"
label="Sign In"
href="/login.zul"
/>
How to make it work?

Regards,

-Andrey

delete flag offensive retag edit

8 Replies

Sort by ยป oldest newest

answered 2010-04-16 08:12:20 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Hi ady1981
You can try to print the result

<label value="${sec:isAllGranted('ROLE_ANONYMOUS')}"/>

link publish delete flag offensive edit

answered 2010-04-26 03:40:11 +0800

ady1981 gravatar image ady1981
120 4

<label value="${sec:isAllGranted('ROLE_ANONYMOUS')}"/>
get out `false' in either cases (when user is signed in and when he is anonymousUser).

link publish delete flag offensive edit

answered 2010-04-26 23:02:21 +0800

ashishd gravatar image ashishd flag of Taiwan
1972 6

Hi ady1981,
I just tested your code with zkspring 3.0RC release and it worked as you are expecting. If user isn't signed in then "Sign In" toolbar button is displayed and after signing in the button isn't displayed. Can you show me your security configuration file and/or the username to roles mapping?

Thanks
- Ashish

link publish delete flag offensive edit

answered 2010-04-27 13:39:01 +0800

ady1981 gravatar image ady1981
120 4

updated 2010-04-27 13:42:28 +0800

ashishd,

Here is a portion from my applicationContext-security.xml:

<security:global-method-security pre-post-annotations="enabled" />

    <security:http auto-config="true">
  
        <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <security:form-login login-page="/login.zul"
                             default-target-url="/index.zul"
                             authentication-failure-url="/accessDenied.zul"/>
        <security:remember-me/>
        <security:logout logout-success-url="/index.zul"/>
    </security:http>

What do You mean by "username to roles mapping" (concerning ANONYMOUS user)?

Regards,

-Andrey

link publish delete flag offensive edit

answered 2010-04-27 20:32:37 +0800

ashishd gravatar image ashishd flag of Taiwan
1972 6

Hi ady1981,
you said "So, this does not work: ..."

<toolbarbutton id="signIn"
if="${sec:isAllGranted('ROLE_ANONYMOUS')}"
label="Sign In"
href="/login.zul"
/>

Is there any error on the server console/log? or do you see Sign In button no matter user is signed in or not?

By users to roles mapping I meant which user has which roles? For example from our sample code

    <authentication-manager>
    <authentication-provider>
        <password-encoder hash="md5"/>
        <user-service>
            <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" />
	    </user-service>
	</authentication-provider>
	</authentication-manager>

Here user rod has ROLE_SUPERVISOR, ROLE_USER and ROLE_TELLER roles.

You could also try giving some default role to all your users such as ROLE_USER and try your code with ${sec:isAllGranted('ROLE_USER')} instead.

Thanks
- Ashish

link publish delete flag offensive edit

answered 2010-04-29 13:16:26 +0800

ady1981 gravatar image ady1981
120 4

updated 2010-04-29 13:17:40 +0800

ashishd,
Finally I understood that I did not configure the zk spring-security tags correctly :(. As I could figure out the *.tld config absence in zk-spring-security-3.0RC.jar. I took security.tld from some previous version (/metainfo/*, /WEB-INF/tld/*) and used following setting in the page:
<?taglib uri="/WEB-INF/tld/zkspring/security.tld" prefix="sec"?>
...
<toolbarbutton id="signOut"
if="${sec:isAllGranted('ROLE_USER')}"
label="Sign Out"
href="/logout.zul"
/>
This zul does not reflect to whether user log in or not. And this zul simply get out the error:
<toolbarbutton id="signOut"
if="${sec:isAnyGranted('ROLE_USER')}"
label="Sign Out"
href="/logout.zul"
/>
Caused by: java.lang.ClassCastException: java.util.Collections$UnmodifiableRandomAccessList cannot be cast to org.springframework.security.core.GrantedAuthority
        at org.zkoss.spring.security.SecurityUtil.authoritiesToRoles(SecurityUtil.java:304)
        at org.zkoss.spring.security.SecurityUtil.retainAll(SecurityUtil.java:368)
        at org.zkoss.spring.security.SecurityUtil.isAnyGranted(SecurityUtil.java:183)
link publish delete flag offensive edit

answered 2010-04-29 20:07:15 +0800

ashishd gravatar image ashishd flag of Taiwan
1972 6

Hi ady1981,
Can you download zkspring 3.0RC again from here? I had fixed the missing tld issue several days back. New download should resolve your issue.

Thanks
- Ashish

link publish delete flag offensive edit

answered 2010-05-01 13:34:10 +0800

ady1981 gravatar image ady1981
120 4

ashishd,

Now the security tags works ok.
Thank You very much.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-04-15 06:04:46 +0800

Seen: 1,667 times

Last updated: May 01 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More