Results 1 to 4 of 4

Thread: ifAllGranted in servlet?

  1. #1
    Join Date
    Aug 2006
    Posts
    26

    Default ifAllGranted in servlet?

    Hi All,
    in jsp I use "<authz:authorize ifAllGranted="ROLE_ADMIN">"
    could somebody tell me how can I do same check in servlet?

    thanks.

  2. #2

    Default

    The following method can be used to see if a person has a permission.

    Code:
    	/**
    	 * Returns true if the current user has been granted the given permission, else false.
    	 * @param permToken
    	 * @return
    	 */
    	
    	public boolean isAuthorized(String permToken) {
    		// find the permission in the user's list of permissions
    		SecurityContext context = SecurityContextHolder.getContext();
    		Authentication auth = context.getAuthentication();
    		GrantedAuthority[] auths = auth.getAuthorities();
    		for (int i = 0; i < auths.length; i++) {
    			if (auths[i].getAuthority().equals(permToken)) {
    				return true;
    			}
    		}
    		
    		// no permission found with the given name
    		return false;
    	}
    jh

  3. #3
    Join Date
    Oct 2004
    Posts
    207

    Default

    Or Consider adding a SecurityContextHolderAwareRequestFilter to your filterChainProxy config.
    http://www.acegisecurity.org/multipr...estFilter.html

    Then you can simply use request.isUserInRole("YOUR_ROLE_HERE");

  4. #4
    Join Date
    Aug 2006
    Posts
    26

    Default

    thanks! I've added SecurityContextHolderAwareRequestFilter and ... sometimes it works! the problem is - sometimes it doesn't .
    sometimes request.isUserInRole returns false and I get nullpointerexception at the same time in this peace of code:
    SecurityContext context = SecurityContextHolder.getContext();
    Authentication auth = context.getAuthentication();
    GrantedAuthority[] auths = auth.getAuthorities();




    Quote Originally Posted by RayKrueger
    Or Consider adding a SecurityContextHolderAwareRequestFilter to your filterChainProxy config.
    http://www.acegisecurity.org/multipr...estFilter.html

    Then you can simply use request.isUserInRole("YOUR_ROLE_HERE");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •