Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to immediately enable the authority after update user authority in spring security?

Tags:

I'm using spring-security framework.When I update the permissions,It does not take effect immediately.I have to quit the current user(means logout), and then re-visit(means login) will be to update the user's permission.

Is a way that immediately enable the authority after update user authority in spring security?

like image 877
Gordian Yuan Avatar asked May 21 '09 12:05

Gordian Yuan


People also ask

How do I change the authority in Spring Security?

getAuthorities() method just returns a Collection<GrantedAuthority> object. You can use the appropriate Collection method to add your new authority to that collection. Selah. @Slavak That would really depend on what implementation you're using for UserDetails.

What is SecurityContextHolder getContext () getAuthentication ()?

The HttpServletRequest.getUserPrincipal() will return the result of SecurityContextHolder.getContext().getAuthentication() . This means it is an Authentication which is typically an instance of UsernamePasswordAuthenticationToken when using username and password based authentication.


2 Answers

You can set alwaysReauthenticate in your AbstractSecurityInterceptor like this

<bean id="filterSecurityInterceptor" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">         <property name="alwaysReauthenticate" value="true"/>  ... </bean> 

Of course you should pay attention because 99,9% you don't need reauthentication. As authentication might use a database or something else your performance might degrade. But usually you have a cache, like 2nd Level with hibernate, so loading the userdetails everytime should be an memory only operation in all cases where authorities havn't changed.

like image 184
Janning Avatar answered Sep 20 '22 18:09

Janning


Gandalf solution is valid but not complete. In order for the new permissions to be considered by spring security (eg. allow access to pages previously not available), you need to create a new authentication object (eg. new UsernamePasswordAuthenticationToken) containing the new list of authorities.

like image 45
Hyperion Avatar answered Sep 18 '22 18:09

Hyperion