0

我需要使用对象列表进行访问控制。获取该列表的查询非常复杂且冗长,因此我想在用户向服务器进行身份验证时将其缓存在用户对象中。我决定尝试通过自定义实现 UserDetailService 和这个 WebSecurityConfig 来解决这个问题:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

...


        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            String ldapURI = "ldap://" + ldaHost + ":" + ldapPort + "/" + ldapBasis;

            DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(ldapURI);
            contextSource.setUserDn(ldapUser);
            contextSource.setPassword(ldapPassword);
            contextSource.afterPropertiesSet();

            LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthenticationProviderConfigurer = auth
                    .ldapAuthentication().userDetailsContextMapper(new LdapUserDetailsContextMapper());

            ldapAuthenticationProviderConfigurer
                    .userSearchFilter("(&(criteria={0}))")
                    .userSearchBase("ou=usersearchbase").contextSource(contextSource);
        }
}

ContextMapper 用数据填充用户对象:

public class LdapUserDetailsContextMapper implements UserDetailsContextMapper {

private final org.slf4j.Logger log = LoggerFactory.getLogger(this.getClass());

private DataService dataService;

public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
                                      Collection<? extends GrantedAuthority> authorities) {

    AppUser user = new AppUser();
    user.setUsername(ctx.getStringAttribute("uid"));
    user.setName(ctx.getStringAttribute("cn"));
    user.setSurname(ctx.getStringAttribute("sn"));
    user.setMail(ctx.getStringAttribute("mail"));
    user.setRoleUser();    

user.setManaged(dataService.getManagedData(user.getMail()));

                    return user;
                }

问题是我看不到将我的 DataService 注入此进程的方法 - ContextMapper 和 @Configure 类都不是托管 bean,这使得 @Autowired 不可能。我想避免 LoadTimeWeaving,因为使用它会使部署变得非常困难——我还有其他选择吗?

我发现了这两个类似的问题:为什么我的 Spring @Autowired 字段为空?在一个Controller类中也有同样的问题,可以转成托管bean;其他两个提到的解决方案都对我不起作用(dataService 始终为空)和 Spring Boot,@Autowire 使用 @Configurable 进入非托管类,并且加载时间编织再次推荐 LoadTimeWeaving。

我还发现了在 init 方法中提到使用 @Autowired 的明显解决方案,但我也无法让它工作(尝试使用注入的 dataService 调用 ContextMapper-constructor)

4

1 回答 1

1

只需将其设为春季管理的 bean。

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

...


        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            String ldapURI = "ldap://" + ldaHost + ":" + ldapPort + "/" + ldapBasis;

            DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(ldapURI);
            contextSource.setUserDn(ldapUser);
            contextSource.setPassword(ldapPassword);
            contextSource.afterPropertiesSet();

            LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthenticationProviderConfigurer = auth
                    .ldapAuthentication().userDetailsContextMapper(userDetailsContextMapper());

            ldapAuthenticationProviderConfigurer
                    .userSearchFilter("(&(criteria={0}))")
                    .userSearchBase("ou=usersearchbase").contextSource(contextSource);
        }
    @Bean
    public LdapUserDetailsContextMapper userDetailsContextMapper() {
        return new LdapUserDetailsContextMapper();
    }
}

LdapUserDetailsContextMapper并在with中注释您的字段@Autowired

于 2016-07-18T10:38:08.337 回答