2016-11-14 60 views
0

我使用Spring Security的核心在我的web應用程序的認證,但我無法弄清楚如何從我的User類 得到一個定製的信息,這裏是我的用戶等級:Grails Spring Security核心 - 自定義用戶信息?

package core 

import beans.Colaborador 
import groovy.transform.EqualsAndHashCode 
import groovy.transform.ToString 

@EqualsAndHashCode(includes = 'username') 
@ToString(includes = 'username', includeNames = true, includePackage = false) 
class Usuario implements Serializable { 

    private static final long serialVersionUID = 1 

    transient springSecurityService 
    String username 
    String password 
    boolean enabled = true 
    boolean accountExpired 
    boolean accountLocked 
    boolean passwordExpired 
    Colaborador colaborador 

    Usuario(String username, String password) { 
     this() 
     this.username = username 
     this.password = password 
    } 

    Set<Permissao> getAuthorities() { 
     UsuarioPermissao.findAllByUsuario(this)*.permissao 
    } 

    def beforeInsert() { 
     encodePassword() 
    } 

    def beforeUpdate() { 
     if (isDirty('password')) { 
      encodePassword() 
     } 
    } 

    protected void encodePassword() { 
     password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password 
    } 

    static transients = ['springSecurityService'] 

    static constraints = { 
     username blank: false, unique: true 
     password blank: false 
     colaborador nullable: true 
    } 

    static mapping = { 
     password column: '`password`' 
    } 
} 

我想要的信息如果我嘗試通過使用秒來訪問它:loggedInUserInfo並沒有成功,我讀過我需要創建一個自定義UserDetailsS​​ervice,但我無法找出如何以及在哪裏創造它,可以有人給我一隻手嗎?

謝謝!

@Edit

我試過下面的文檔和我創建兩個類

import beans.Colaborador 
import grails.plugin.springsecurity.userdetails.GrailsUser 
import org.springframework.security.core.GrantedAuthority 

class MyUserDetails extends GrailsUser { 

    final Colaborador colaborador 

    MyUserDetails(String username, String password, boolean enabled, 
        boolean accountNonExpired, boolean credentialsNonExpired, 
        boolean accountNonLocked, 
        Collection<GrantedAuthority> authorities, 
        long id, Colaborador colaborador) { 
     super(username, password, enabled, accountNonExpired, 
       credentialsNonExpired, accountNonLocked, authorities, id) 
     this.colaborador = colaborador 
    } 
} 

package core  
import grails.plugin.springsecurity.SpringSecurityUtils 
import grails.plugin.springsecurity.userdetails.GrailsUserDetailsService 
import grails.plugin.springsecurity.userdetails.NoStackUsernameNotFoundException 
import grails.transaction.Transactional 
import org.springframework.security.core.authority.SimpleGrantedAuthority 
import org.springframework.security.core.userdetails.UserDetails 
import org.springframework.security.core.userdetails.UsernameNotFoundException 

class MyUserDetailsService implements GrailsUserDetailsService { 
    static final List NO_ROLES = [new SimpleGrantedAuthority(SpringSecurityUtils.NO_ROLE)] 

    UserDetails loadUserByUsername(String username, boolean loadRoles) 
      throws UsernameNotFoundException { 
     return loadUserByUsername(username) 
    } 

    @Transactional(readOnly = true, noRollbackFor = [IllegalArgumentException, UsernameNotFoundException]) 
    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 

     Usuario user = Usuario.findByUsername(username) 
     if (!user) throw new NoStackUsernameNotFoundException() 
     def roles = user.authorities 
     def authorities = roles.collect { 
      new SimpleGrantedAuthority(it.authority) 
     } 

     return new MyUserDetails(user.username, user.password, user.enabled, 
       !user.accountExpired, !user.passwordExpired, 
       !user.accountLocked, authorities ?: NO_ROLES, user.id, 
       user.colaborador) 
    } 
} 

,當我在我的應用登錄我得到這個錯誤:

2016-11-14 11:56:46,061 [http-bio-80-exec-4] ERROR authentication.GrailsUsernamePasswordAuthenticationFilter - An internal error occurred while trying to authenticate the user. 
Message: Could not find matching constructor for: java.lang.Object(java.lang.String, java.lang.String, java.lang.Boolean, java.lang.Boolean, java.lang.Boolean, java.lang.Boolean, java.util.ArrayList, java.lang.Long) 
+0

你只是想要訪問用戶類通知到另一種方法或類權利? –

+0

我想從另一個類或視圖訪問自定義的用戶信息,在這種情況下,來自該Colaborador對象的數據 – xSlok

回答

1

documentation概述(一步一步)如何創建自定義UserDetailsService。它會引導您完成創建並在應用程序中註冊的過程。

+0

我試過了,現在我收到這個錯誤:'2016-11-14 11:56:46,061 [http-bio-80-exec-4]錯誤authentication.GrailsUsernamePasswordAuthenticationFilter - 嘗試驗證用戶時發生內部錯誤。 消息:找不到匹配的構造函數:java.lang.Object(java.lang.String,java.lang.String,java.lang.Boolean,java.lang.Boolean,java.lang.Boolean,java.lang。 Boolean,java.util.ArrayList,java.lang.Long)'當我在我的應用程序登錄(我會編輯我的問題,並添加新的代碼) – xSlok

+0

你有'UserDetailsS​​ervice'在'resources.groovy'註冊嗎? –

+0

我做到了,這是我的resources.groovy'進口core.MyUserDetailsS​​ervice 豆= { 的UserDetailsS​​ervice(MyUserDetailsS​​ervice) }' – xSlok

相關問題