2012-07-12 23 views
3

我已經成功地將JOSSO和Spring Security應用程序集成到我的Grails應用程序中(使用LDAP進行用戶控制)。如何在使用JOSSO和Spring Security的Grails應用程序中從LDAP獲取定製屬性?

由於JOSSO已經管理身份驗證,因此我使用Spring Security集成的「預驗證方案」。這是我與春季安全配置resources.groovy內容:

def developmentEnvironment = { 
    if (grailsApplication.config.grails.plugins.springsecurity.active) { 

    preAuthenticatedAuthenticationProvider(PreAuthenticatedAuthenticationProvider) { 
     preAuthenticatedUserDetailsService = ref('preAuthenticatedUserDetailsService') 
    } 

    preAuthenticatedUserDetailsService(PreAuthenticatedGrantedAuthoritiesUserDetailsService) { 
    } 

    j2eePreAuthFilter(J2eePreAuthenticatedProcessingFilter) { 
     authenticationManager = ref('authenticationManager') 
     authenticationDetailsSource = { 
     J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource authenticationDetailsSource -> 
     mappableRolesRetriever = { 
      SimpleMappableAttributesRetriever mappableAttributesRetriever -> 
      mappableAttributes = ['app_admin', 'app_user', 'app_report', 'app_access'] as Set 
     } 
     userRoles2GrantedAuthoritiesMapper = { 
      SimpleAttributes2GrantedAuthoritiesMapper grantedAuthoritiesMapper -> 
      convertAttributeToUpperCase = "true" 
     } 
     } 
    } 

    preAuthenticatedProcessingFilterEntryPoint(Http403ForbiddenEntryPoint) { 
    } 

    preAuthenticatedExceptionTranslationFilter(ExceptionTranslationFilter) { 
     authenticationEntryPoint = ref('preAuthenticatedProcessingFilterEntryPoint') 
    } 
    } 
} 

一切工作正常,我可以訪問Grails的側面默認屬性(例如使用springSecurityService)。

但現在我有一個新的要求,從LDAP獲取自定義屬性(例如ownership)。所以,我將這些屬性添加到LDAP下的我的用戶,據我所知JOSSO會自動獲得這些屬性,但我無法在grails應用程序端獲得這些屬性。 有什麼辦法可以在Grails端獲得這些屬性?

+1

也許我應該重寫的UserDetails和UserDetailsS​​ervice的類來添加自己的自定義屬性。問題是我如何獲取UserDetailsS​​ervice類的loadUserByUsername(..)方法下的用戶數據。 – 2012-07-13 06:23:08

回答

0

這樣的自定義屬性應該放在你的UserDetails接口的實現或者User類的擴展中。 在http://static.springsource.org/spring-security/site/docs/3.1.x/reference/preauth.html中,您可以找到該場景如何實現AuthenticationUserDetailsS​​ervice。

一旦你這樣做,你可以查詢SecurityContextHolder中,讓您的UserDetails實現

SecurityContextHolder.getContext().getAuthentication().getPrincipal() 
相關問題