{"id":8355,"date":"2012-09-26T23:06:05","date_gmt":"2012-09-26T17:36:05","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8355"},"modified":"2012-09-26T23:06:05","modified_gmt":"2012-09-26T17:36:05","slug":"instance-based-login-in-spring-security-core","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/instance-based-login-in-spring-security-core\/","title":{"rendered":"Instance based login in spring security core"},"content":{"rendered":"<p>Spring security loads the roles of user from user role table based on all roles assigned to user and that role is application specific.<br \/>\nBut In my project i require to  assign roles to user based on instance . So when the instance is changed roles should be changed .In grails we can overide the methods of plugin. So when instance change i reauthenticate the user and overrided the &#8220;loadAuthorities &#8221; method of spring security. So instead of fetching roles from spring security loadAuthorities method , it loads from this overided loadAuthorities method&#8230;<\/p>\n<p>[java]<br \/>\nclass UserRole implements Serializable {<\/p>\n<p>    User user<br \/>\n    Role role<br \/>\n    BootCamp bootCamp<\/p>\n<p>[\/java]<\/p>\n<p> [java]<br \/>\nclass CustomUserDetailsService extends GormUserDetailsService {<\/p>\n<p>@Override<br \/>\nprotected Collection loadAuthorities(user, String username, boolean loadRoles) {<br \/>\n\t\tif (!loadRoles) {<br \/>\n\t\t\treturn []<br \/>\n\t\t}<br \/>\ndef conf = SpringSecurityUtils.securityConfig<br \/>\nString authoritiesPropertyName = conf.userLookup.authoritiesPropertyName<br \/>\nString authorityPropertyName = conf.authority.nameField<br \/>\nBootcamp bootCamp=BootCamp.get(RequestContextHolder.currentRequestAttributes().getSession()?.bootCampId))<br \/>\n  User loggedInUser = User.findByEmail(username)<br \/>\n \/\/Write your query for loading roles here for ex.<\/p>\n<p>Collection&lt;?&gt; userAuthorities =UserRole.findAllByUserAndBootCamp(loggedInUser, bootCamp)?.role<\/p>\n<p>def authorities = userAuthorities.collect { new GrantedAuthorityImpl(it.&quot;$authorityPropertyName&quot;) }<br \/>\nauthorities ?: NO_ROLES<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>for example i am reloading the role by calling this method&#8230;<br \/>\n[java]<br \/>\ndef getAuthoritiesBasedOnBootCamp() {<br \/>\n        session.bootCampId = params.bootCampId<br \/>\n        springSecurityService.reauthenticate(User.loggedInUser.email, &quot;&quot;)<br \/>\n        redirect(action: &#8216;dashBoard&#8217;)<br \/>\n    }<\/p>\n<p>[\/java]<\/p>\n<p>Shaurav@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring security loads the roles of user from user role table based on all roles assigned to user and that role is application specific. But In my project i require to assign roles to user based on instance . So when the instance is changed roles should be changed .In grails we can overide the [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[7],"tags":[4840,672],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8355"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8355"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8355\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}