Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So these are at least two questions:</p> <ul> <li>How to make the granted authorities/privileges/Roles dynamic?</li> <li>How to make the access restriction for the URLs dynamic?</li> </ul> <h2>1) How to make the granted authorities/privileges/Roles dynamic?</h2> <p><em>I will not answer this in great detail, because I believe this theme was discussed often enough.</em></p> <p>The easiest way would be to store the complete user information (login, password and roles) in a database (3 Tables: User, Roles, User2Roles) and use the <code>JdbcDetailService</code>. You can configure the two SQL Statements (for authentication and for granting the roles) very nicely in your xml configuration.</p> <p>But then the user needs to logout and login to get these new Roles. If this is not acceptable, you must also manipulate the Roles of the current logged in user. They are stored in the users session. I guess the easiest way to do that is to add a filter in the spring security filter chain that updates the Roles for every request, if they need to be changed.</p> <h2>2) How to make the access restriction for the URLs dynamic?</h2> <p>Here you have at last two ways:</p> <ul> <li>Hacking into the <code>FilterSecurityInterceptor</code> and updating the <code>securityMetadataSource</code>, the needed Roles should be stored there. At least you must manipulate the output of the method <code>DefaultFilterInvocationSecurityMetadataSource#lookupAttributes(String url, String method)</code></li> <li>The other way would be using other expressions for the <code>access</code> attribute instead of <code>access="hasRole('ROLE_USER')"</code>. Example: <code>access="isAllowdForUserPages1To3"</code>. Of course you must create that method. This is called a "custom SpEL expression handler" (If you have the <a href="http://web.archive.org/web/20161012152658/http://www.springsecuritybook.com/" rel="nofollow noreferrer">Spring Security 3 Book</a> it's around page 210. Wish they had chapter numbers!). So what you need to do now is to subclass <code>WebSecurityExpressionRoot</code> and introduce a new method <code>isAllowdForUserPages1To3</code>. Then you need to subclass <code>DefaultWebSecurityExpressionHandler</code> and modify the <code>createEvaluationContext</code> method so that its first request <code>StandartEvaluationContext</code> calls super (you need to cast the result to <code>StandartEvaluationContext</code>). Then, replace the <code>rootObject</code> in the <code>StandartEvaluationContext</code> using your new <code>CustomWebSecurityExpressionRoot</code> implementation. That's the hard part! Then, you need to replace the <code>expressionHandler</code> attribute of the <code>expressionVoter</code> (<code>WebExpressionVoter</code>) in the xml configuration with your new subclassed <code>DefaultWebSecurityExpressionHandler</code>. (This sucks because you first need to write a lot of security configuration explicity as you can't access them directly from the security namespace.)</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload