首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SpringSecurity3.1: Active身份验证和本地DB授权

SpringSecurity3.1: Active身份验证和本地DB授权
EN

Stack Overflow用户
提问于 2013-09-17 08:17:39
回答 1查看 5.9K关注 0票数 1

我使用SpringSecurity3.1进行Active身份验证,使用本地db加载权限。我见过类似的例子,但我仍然不清楚我到底应该使用什么。我在spring-security.xml中的当前设置是:

代码语言:javascript
复制
  <!-- LDAP server details -->
  <security:authentication-manager>
    <security:authentication-provider ref="ldapActiveDirectoryAuthProvider" />
  </security:authentication-manager>


  <beans:bean id="ldapActiveDirectoryAuthProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
    <beans:constructor-arg value="${ldap.domain}" />
    <beans:constructor-arg value="${ldap.url}" />
    <beans:property name="useAuthenticationRequestCredentials" value="true" />
    <beans:property name="convertSubErrorCodesToExceptions" value="true" />
  </beans:bean>

我有一门课让我们称之为"BookStoreDbAuthPopulator.java“。在这个类中,我调用这个方法:

代码语言:javascript
复制
    // Load additional authorities and create an Authentication object
    final List<GrantedAuthority> authorities = loadRolesFromDatabaseHere();

对我来说还不清楚的是:应该实现哪个接口"BookStoreDbAuthPopulator.java“才能将加载的权限从db添加到UserDetails?"UserDetailsContextMapper“或"GrantedAuthoritiesMapper”或"AuthenticationProvider"?

基于这个解决方案:Spring Security 3 Active Directory Authentication, Database Authorization "BookStoreDbAuthPopulator.java“应该实现"AuthenticationProvider”。我怀疑是否应该使用"BookStoreDbAuthPopulator.java“作为"ldapActiveDirectoryAuthProvider”bean的属性?

在此之前,非常感谢您。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-02 14:01:35

我的最后一个解决方案是"BookStoreDbAuthPopulator.java“实现"UserDetailsContextMapper”。

代码语言:javascript
复制
public class BookStoreDbAuthPopulator implements UserDetailsContextMapper {

   // populating roles assigned to the user from AUTHORITIES table in DB
   private List<SimpleGrantedAuthority> loadRolesFromDatabase(String username) {

      //"SELECT ROLE FROM AUTHORITIES WHERE LCASE(USERNAME) LIKE ?"
      ...
   }

   @Override
   public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
      List<SimpleGrantedAuthority> allAuthorities = new ArrayList<SimpleGrantedAuthority>();
      for (GrantedAuthority auth : authorities) {
        if (auth != null && !auth.getAuthority().isEmpty()) {
           allAuthorities.add((SimpleGrantedAuthority) auth);
        }
      }
      // add additional roles from the database table
      allAuthorities.addAll(loadRolesFromDatabase(username));
      return new User(username, "", true, true, true, true, allAuthorities);
   }

   @Override
   public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {
   }

}

然后在Springsecurity.xml中

代码语言:javascript
复制
  <!-- AuthenticationManager: AuthenticationProvider, LDAP server details -->
     <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider ref="ldapActiveDirectoryAuthProvider" />
     </security:authentication-manager>

  <beans:bean id="ldapActiveDirectoryAuthProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
     <!-- the domain name (may be null or empty). If no domain name is configured, it is assumed that the username will always contain the domain name. -->
     <beans:constructor-arg value="${ldap.domain}" />
     <!-- an LDAP url (or multiple URLs) -->
     <beans:constructor-arg value="${ldap.url}" />
     <!-- Determines whether the supplied password will be used as the credentials in the successful authentication token. -->
     <beans:property name="useAuthenticationRequestCredentials" value="true" />
     <!-- by setting this property to true, when the authentication fails the error codes will also be used to control the exception raised. -->
     <beans:property name="convertSubErrorCodesToExceptions" value="true" />
     <!-- for customizing user authorities -->
     <beans:property name="userDetailsContextMapper" ref="myUserDetailsContextMapper" />
  </beans:bean>
     <!-- Customizing UserDetail -->
  <beans:bean id="myUserDetailsContextMapper" class="com.mybookstore.mywebcomp.w.BookStoreDbAuthPopulator">
  </beans:bean>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18844699

复制
相关文章

相似问题

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