我正在尝试使用我的数据库中的角色在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':需要具有非空返回
  类型!

最佳答案

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

07-24 20:17