首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grails Spring Security:在双重角色之间切换

Grails Spring Security:在双重角色之间切换
EN

Stack Overflow用户
提问于 2011-07-21 15:08:14
回答 2查看 1.3K关注 0票数 1

在我的项目中,某些用户具有双重角色,所以如果任何这样的用户登录,我如何让他在这两个角色之间切换,以便他可以执行该特定角色提供的某些操作。

我很欣赏一步一步的过程,因为我是Grails的新手。任何这样的在线文献与例子都是高度赞赏的。

更新:- WorkridersUser loadUserByUsername(String username,String roleName)抛出UsernameNotFoundException { // def conf = SpringSecurityUtils.securityConfig //类User =User

代码语言:javascript
复制
    SchemeUser.withTransaction { status ->

        SchemeUser user = SchemeUser.findByUsername(username) 
        if (!user){ throw new UsernameNotFoundException('User not found', username)}

        UserProfile userProfile = UserProfile.findByEmail(user.username)
        Representative representative = Representative.findByUser(user)
        Organization organization = Organization.get(representative.organization.id)


        def authorities = user.authorities.collect {new GrantedAuthorityImpl(it.authority)}

        return new WorkridersUser(user.username, user.password, user.enabled, 
            !user.accountExpired, !user.passwordExpired, !user.accountLocked, 
            authorities ?: roleName, user.id, organization.companyName,userProfile) 
    } 
}

谢谢

Sri

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-26 17:25:02

您需要覆盖loadUserByUsername方法,如下所示:

代码语言:javascript
复制
UserDetails loadUserByUsername(String username, String selectedRole) {
  // current selected role is stored in the member variable of the UserDetail class         
  this.selectedRole = selectedRole
  return loadUserByUsername(username)
}

要在选择框中显示当前用户的所有角色,请编写自己的标记库,如下所示:

代码语言:javascript
复制
def authorities = springSecurityService.principal.originalAuthorities
if (authorities.size() > 1) {
    out << "<form name='switchRoleForm' action='switchRole' method='post'>"
    out << "<select name='roles'>"
    Role tmpRole
    authorities.each {
        tmpRole = Role.findByAuthority(it.authority)
        // access your current select role
            if (springSecurityService.principal.selectedRole== it.authority)
            out << "<option selected value='${it.authority}'>${tmpRole.name}</option>"
        else
            out << "<option value='${it.authority}'>${tmpRole.name}</option>"
    }
    out << "</select>"
    out << '<input class="switch" type="submit" value="switch"/></span>'
    out << "</form>"        
}

我在上一篇文章中描述了开关逻辑。

票数 2
EN

Stack Overflow用户

发布于 2011-07-21 20:22:31

首先,您必须实现自己的user details类和服务。http://burtbeckwith.github.com/grails-spring-security-core/docs/manual/index.html。在您自己的UserDetailsService中,您可以激活loadUserByUsername方法中的authorities属性,以便该属性同时只包含一个角色。然后,您可以通过再次修改此属性来在角色之间切换。例如

代码语言:javascript
复制
// imagine the following code would be in your switch controller action
// fetch your user detail service bean (registered via resources.groovy and not as a service)
UserDetailsService userDetailsService = grailsApplication.mainContext.getBean("userDetailsService");
UserCache userCache = grailsApplication.mainContext.getBean("userCache");
// update authorities in user details. only one role should exists in authorities list
UserDetails userDetails = userDetailsService.loadUserByUsername(springSecurityService.principal.username, <place here the rolel to switch>);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(),userDetails.getAuthorities()));
// refresh user detail
userCache.removeUserFromCache(springSecurityService.principal.username);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6772466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档