java - spring mvc 的 LDAP 身份验证

标签 java spring spring-mvc ldap roles

我正在尝试使用我的数据库中的角色在 java 配置 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次)

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configureGlobal' defined in class path resource [com/ppc/ptol2/config/SecurityConfiguration.class]: Invalid factory method 'configureGlobal': needs to have a non-void return type!

最佳答案

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

关于java - spring mvc 的 LDAP 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35463554/

相关文章:

java - 有时在 exoplayer 中的视频缓冲速度很慢?

hibernate - java.lang.IllegalStateException : LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context

java - Spring MVC - 我的域类是否应该为在线传输实现可序列化?

java - 使用 Spring Security 时,在 bean 中获取当前用户名(即 SecurityContext)信息的正确方法是什么?

java - Spring Security,REST 基本认证问题

java - Java 中与继承的 HAS-A 关系

java - 在 IntelliJ IDEA 项目中找不到主类 : Java Application

java - 排序方法无法正常工作

java - 即使无法联系发现服务器,是否有办法启动 Discovery 客户端 Spring 应用程序?

java - struts到spring迁移消息资源