来自 spring mvc 的 LDAP 身份验证
Posted
技术标签:
【中文标题】来自 spring mvc 的 LDAP 身份验证【英文标题】:LDAP authentication from spring mvc 【发布时间】: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”:需要有一个非空返回 输入!
【问题讨论】:
【参考方案1】:从您的public void configureGlobal(AuthenticationManagerBuilder auth)
方法中删除@Bean
注释(并添加一个@Override
注释)
【讨论】:
以上是关于来自 spring mvc 的 LDAP 身份验证的主要内容,如果未能解决你的问题,请参考以下文章
来自不同项目的使用 Spring LDAP 的 JSF 用户身份验证
我们如何使用 spring security Ldap 以 angularjs 作为客户端进行身份验证
使用 Spring Security 的 ldap 身份验证