【问题标题】:How to get custom property from LDAP in Grails application using JOSSO and Spring Security?如何使用 JOSSO 和 Spring Security 从 Grails 应用程序中的 LDAP 获取自定义属性?
【发布时间】:2012-07-12 08:45:45
【问题描述】:

我已经成功地将 JOSSO 和 Spring Security 应用程序集成到我的 Grails 应用程序中(使用 LDAP 进行用户控制)。

由于 JOSSO 已经管理身份验证,我正在使用“预身份验证场景”进行 Spring Security 集成。这是我的resources.groovy Spring Security 配置相关的内容:

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 方面获得这些属性?

【问题讨论】:

  • 可能我应该重写 UserDetails 和 UserDetailsS​​ervice 类来添加我自己的自定义属性。问题是如何在 UserDetailsS​​ervice 类的 loadUserByUsername(..) 方法下获取用户数据。

标签: grails spring-security ldap josso


【解决方案1】:

这样的自定义属性应该放在你的 UserDetails 接口的实现中,或者 User 类的扩展中。 在http://static.springsource.org/spring-security/site/docs/3.1.x/reference/preauth.html 你可以找到这个场景应该如何实现AuthenticationUserDetailsS​​ervice。

完成此操作后,您可以查询 SecurityContextHolder 以获取您的 UserDetails 实现

SecurityContextHolder.getContext().getAuthentication().getPrincipal()

【讨论】:

    猜你喜欢
    • 2017-09-05
    • 2018-03-02
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2020-04-06
    • 2019-07-02
    • 2013-05-03
    相关资源
    最近更新 更多