【问题标题】:LDAP authentication from spring mvc来自 spring mvc 的 LDAP 身份验证
【发布时间】:2016-05-29 13:28:53
【问题描述】:

我正在尝试使用我的数据库中的角色在 java config ldap 授权中进行设置。我的设置是

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity( prePostEnabled = true, securedEnabled = true )
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
.
.
.
    @Bean
    public UserDetailsContextMapper userDetailsContextMapper() {
        return new UserDetailsContextMapper() {
            @Override
            public UserDetails mapUserFromContext(
                    DirContextOperations ctx, String username,
                    Collection<? extends GrantedAuthority> authorities) {
                String lowercaseLogonName = username.toLowerCase();
                Optional<PtolUser> userFromDatabase =
                        ptolUserRepository.findOneByLogonName(lowercaseLogonName);
                return userFromDatabase.map(user ->
                    {
                        if (!user.isAccountNonExpired()) {
                            throw new UserNotActivatedException(
                                    "User " + lowercaseLogonName + " was not activated");
                        }
                        List<GrantedAuthority> grantedAuthorities = user.getUserAuthorities().parallelStream()
                                .map(authority -> new SimpleGrantedAuthority(authority.getRole().getName()))
                                .collect(Collectors.toList());
                        return new org.springframework.security.core.userdetails.User(lowercaseLogonName,
                                user.getPassword(), true, user.isAccountNonExpired(), true,
                                user.isAccountNonLocked(), grantedAuthorities);
                    }).orElseThrow(
                            () -> new UsernameNotFoundException(
                                    "User " + lowercaseLogonName + " was not found in the AD"));
            }

            @Override
            public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {
                throw new IllegalStateException("Only retrieving data from LDAP is currently supported");
            }

        };
    }
.
.
.
    @Bean
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {       
        auth//
                .ldapAuthentication()//
                // .userDetailsService(userDetailsService)//
                .userDetailsContextMapper(userDetailsContextMapper())//
                .userDnPatterns(env.getRequiredProperty("ldap.user_dn_patterns"))//
                .groupSearchBase(env.getRequiredProperty("ldap.group_search_base"))//
                .groupSearchFilter(env.getRequiredProperty("ldap.group_search_filter"))//
                .contextSource()//
                .ldif("ptolemaios.ldif");
    }
.
.
.
}

但我有以下警告/错误(2 次)

上下文初始化期间遇到异常 - 取消 刷新尝试: org.springframework.beans.factory.BeanCreationException:错误 创建在类路径中定义的名称为“configureGlobal”的bean 资源 [com/ppc/ptol2/config/SecurityConfiguration.class]:无效 工厂方法“configureGlobal”:需要有一个非空返回 输入!

【问题讨论】:

    标签: java spring spring-mvc ldap roles


    【解决方案1】:

    从您的public void configureGlobal(AuthenticationManagerBuilder auth) 方法中删除@Bean 注释(并添加一个@Override 注释)

    【讨论】:

      猜你喜欢
      • 2016-10-07
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 2012-02-18
      相关资源
      最近更新 更多