एक वर्ष सा लेकिन किसी और की कोशिश कर समूहों के लिए हाथ में आ सकता है:
उपयोगकर्ता वर्ग में:
Set<RoleGroup> getAuthorities() {
(UserRoleGroup.findAllByUser(this) as List<UserRoleGroup>)*.roleGroup as Set<RoleGroup>
}
Set<Role> getRoles() {
(UserRoleGroup?.findAllByUser(this)?.roleGroup?.authorities.collect{it}?.flatten() ?: oldRoles) as Set<Role>
}
List<String> getRoleNames() {
(UserRoleGroup?.findAllByUser(this)?.roleGroup?.collect{it.authorities.authority}?.flatten()?: oldRoles.authority) as List<String>
}
//Above will look up from userRoleGroup roleGroup.authorities = UserRole below
Set<Role> getOldRoles() {
(UserRole.findAllByUser(this) as List<Role>)*.role as Set<Role>
}
मैं भूमिकाओं का उपयोग करके किया गया था, भले ही समूह सक्षम किया गया था और वर्ष oldRoles के खिलाफ के सत्यापन विधि:
import grails.plugin.springsecurity.userdetails.GormUserDetailsService
import grails.transaction.Transactional
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException
class MySpringSecurityAuthenticator extends GormUserDetailsService{
UserDetails loadUserByUsername(String username, boolean loadRoles)
throws UsernameNotFoundException {
return loadUserByUsername(username)
}
@Transactional
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//enable login with either username or password
User user = User.find {
username == username || attributes.emailAddress == username || userCode == username
}
//if (!user) throw new UsernameNotFoundException('User not found', username)
if (!user) throw new UsernameNotFoundException('User not found')
return loadUserByUsername(user)
}
@Transactional
UserDetails loadUserByUsername(User user) throws UsernameNotFoundException {
if (!user) throw new UsernameNotFoundException('User not found')
//UserDetails.withNewSession {
//getAuthorities(user.oldRoles)
UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.username, user.password,
user.enabled, !user.accountExpired, !user.passwordExpired, !user.accountLocked,getAuthoritiesFromGroup(user.authorities))
Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities())
SecurityContextHolder.getContext().setAuthentication(authentication)
return userDetails
//}
}
public static List<GrantedAuthority> getAuthoritiesFromGroup(Set<RoleGroup> roles) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>()
roles?.each { role ->
println "-- role = ${role} vs ${role.getClass()}"
authorities.add(new SimpleGrantedAuthority(role.name))
}
return authorities
}
public static List<GrantedAuthority> getAuthorities(Set<Role> roles) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>()
roles?.each { role ->
println "-- role = ${role} vs ${role.getClass()}"
authorities.add(new SimpleGrantedAuthority(role.authority))
}
return authorities
}
}
और मेरे वसंत/resources.groovy में:
userDetailsService(MySpringSecurityAuthenticator){
grailsApplication = ref('grailsApplication')
}
क्या ऊपर अब तक कर रहा था उपयोगकर्ता भूमिकाओं के लिए वापस जा रहा था और उन्हें के माध्यम से हैंडलर प्रमाणीकरण को जोड़ने .. getAuthorities (user.oldRoles)
मैं अब के माध्यम से समूह के नाम में पढ़ने के लिए उपरोक्त विधि को बदल दिया है getAuthoritiesFromGroup (user.authorities) कौन सा संस्करण का एक पक्ष (प्रभाव) के रूप में मैं उपयोग कर रहा हूँ
यह केवल समूह के नाम को पार्स करता भी है, तो बनाने
तो अब शब्द ROLE_GROUPNAME शामिल करना चाहिए
@Transactional
def grantPermission(User user, String role='ROLE_ADMIN', String group='ROLE_SUPERGROUP') {
def adminRole = Role.findByAuthority(role)
if (!adminRole) {
adminRole = new Role(authority: role).save(flush: true)
}
UserRole.create user, adminRole, true
def adminRoleGroup = RoleGroup.findByName(group)
if (!adminRoleGroup) {
adminRoleGroup = new RoleGroup(name: group).save(flush: true)
}
def adminRoleGroupRole = RoleGroupRole.findByRole(adminRole)
if (!adminRoleGroupRole) {
adminRoleGroupRole = new RoleGroupRole(role: adminRole, roleGroup: adminRoleGroup).save(flush: true)
}
def alreadyDone = UserRoleGroup.findByUserAndRoleGroup(user,adminRoleGroup)
if (!alreadyDone) {
new UserRoleGroup(user: user, roleGroup: adminRoleGroup).save(flush: true)
}
}
मैं समूह के नामों और नहीं उपयोगकर्ता भूमिकाओं के खिलाफ प्रमाणित करने के लिए उम्मीद कर रहा हूँ ताकि संक्षेप में मैं
@Secured("hasAnyRole('ROLE_SUPERGROUP')")
करने के लिए अपने नियंत्रक को बदलने के लिए पड़ा है आशा है कि यह बनाता है भावना सीधे आगे सिर्फ पाने के लिए समय लगता है पालन करने के लिए किया जाना चाहिए इसके चारों ओर आपका सिर ..
इस बिंदु पर मैं अभी भी खेल रहा हूं और मैं ठोस जवाब के रूप में उपयोग नहीं करता क्योंकि मैं अपने समूह को अपने अधिकारियों के रूप में जोड़ने के लिए हैकिंग की तरह हूं और यदि मैं चाहता हूं कि मैं उन समूहों में से प्रत्येक के माध्यम से आगे बढ़ने के लिए एक और हुक जोड़ सकता है और इसमें वास्तविक भूमिकाएं भी शामिल हैं - यह सुनिश्चित करें कि इससे क्या उत्पादन होगा - फिलहाल
मैं इसे अनुरोध करने के लिए बदलना चाहता हूं मैप्स और इसे डीबी पर ले जाना चाहता हूं ताकि बहुत कुछ बदल सके और यह तय होगा कि मुझे वापस लौटना चाहिए या समूहों का उपयोग करना चाहिए, मुझे पता है कि मैं नियंत्रकों पर कम नाम कॉन्फ़िगर कर सकता हूं और समूह के नामों पर भरोसा कर सकता हूं ..
किसी भी तरह से अपनी यह सब के हुड के अंतर्गत और आप एक स्पष्ट विचार कुछ साल पर देता है, लेकिन दूसरों
अद्यतन तो मैं के साथ जाने का फैसला किया है काम में आ सकता है: ,getAuthorities(user.roles)
कहाँ कोड
कारण के ऊपर थोड़ा अंदर Set<Role> getRoles() {
विधि काफी सरल है:
results?.each {
it.user=User.get(it.id)
println "-- \n${it.id}:${it.user.roles} \n${it.id}:${it.user.oldRoles}"
7:[Role(authority:ROLE_ADMIN), Role(authority:ROLE_ADMINAHHA)]
7:[Role(authority:ROLE_ADMIN)]
आप देख सकते हैं मैं getOldRoles उपयोग कर एक नया उपयोगकर्ता जोड़ा मैं केवल getRoles मैं 2 मिल पर 1 भूमिका देखते हैं भूमिकाएं .. मैंने 2 भूमिकाओं के साथ उपयोगकर्ता को जोड़ा ..
तो अब यह सभी उपयोगकर्ता भूमिकाओं के माध्यम से विश्लेषण करेगा और उन्हें List<GrantedAuthority>
में जोड़ देगा प्रमाणीकरण अभी भी पहले के रूप में जेनरेट किए गए वास्तविक ROLE नामों के माध्यम से होगा।
.. यह सिर्फ जब मैं उपयोगकर्ता यह है कि उपयोगकर्ता के लिए कि अनुमति लोड हो रहा है बंद कर देना चाहिए की ओर से समूह को निष्क्रिय मतलब है
है कि क्या मॉडल
करना चाहिए