views:

25

answers:

1

At pages I am using tag: security:authorize ifAnyGranted="ROLE_USER,ROLE_ADMIN" ... It works. But at server side: I use SecurityContextHolder.getContext().getAuthentication().isAuthenticated(),it is always true. When I didn't log in, the system take anonymousUser as the log in user.

Whow can I avoid this?

Thanks

A: 

If it is spring security 2.x, there is AuthorityUtils.userHasAuthority(String authority) which you can use to make explicit check for the role.

You could iterate over SecurityContextHolder.getContext().getAuthentication().getAuthorities() and ensure you permit operation only for the roles that you want.

Raghuram
Thanks, I knew this approach,I throught there was anthoer method.SecurityContextHolder.getContext().getAuthentication().getAuthorities()
Tom

related questions