gpt4 book ai didi

Spring Security LDAP 配置

转载 作者:行者123 更新时间:2023-12-05 00:23:14 28 4
gpt4 key购买 nike

我正在研究 Spring Security,想知道使用注释的 Spring Active Directory LDAP 的配置。我需要将我的项目与工作场所的 LDAP 服务器连接起来。

最佳答案

@Configuration
@EnableWebSecurity
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {


@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
auth
.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
}



/** To configure LDAP SERVER **/

@Bean
public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {

ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(null, URL);

provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
provider.setUserDetailsContextMapper(userDetailsContextMapper());


return provider;
}

@Bean
public UserDetailsContextMapper userDetailsContextMapper() {
UserDetailsContextMapper contextMapper = new AttributesLDAPUserDetailsContextMapper();
return contextMapper;
}

/** End configuration of LDAP SERVER **/


}``

公共(public)类 LdapSecuredUser 扩展用户实现 LdapUserDetails {
/**
*
*/


@Autowired
private IUserService userService;

User newUser=new User();



public LdapSecuredUser(User u) {
newUser=u;
if (u != null) {

this.setEmailId(u.getEmailId());
this.setUserGroups(u.getUserGroups());
System.out.println(this.getEmailId() + " " + this.getUsername() +" " + this.getAuthorities()
+" ");

}
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {

Collection<GrantedAuthority> authorities = new ArrayList<>();


Set<Permission> permissions = new HashSet<Permission>(0);
for (UserGroup userGroup : newUser.getUserGroups()){
System.out.println(userGroup.getUserGroupName());
for(Permission permission : userGroup.getPermissions()){
permissions.add(permission);
}
}

if (permissions != null) {
for (Permission permission : permissions) {
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(
permission.getPermissionName());
authorities.add(authority);
}
}
return authorities;
}

@Override
public String getUsername() {
return super.getEmailId();
}

@Override
public boolean isAccountNonExpired() {
return true;
}

@Override
public boolean isAccountNonLocked() {
return true;
}

@Override
public boolean isCredentialsNonExpired() {
return true;
}

@Override
public boolean isEnabled() {
return true;
}

@Override
public String getDn() {
return null;
}

}

公共(public)类 AttributesLDAPUserDetailsContextMapper
实现 UserDetailsContextMapper {
/**
*
*/


private InetOrgPersonContextMapper ldapUserDetailsMapper = new InetOrgPersonContextMapper();

@Autowired
private IUserService userService;

@Autowired
private IUserGroupService usergroupService;

@Override
public UserDetails mapUserFromContext(DirContextOperations arg0, String arg1, Collection<? extends GrantedAuthority> arg2)
{
InetOrgPerson userLdap = (InetOrgPerson) ldapUserDetailsMapper.mapUserFromContext(arg0, arg1, arg2);
User u = userService.findByEmailIdEquals(userLdap.getUsername());

String databaseUserNameCheching=userLdap.getUsername();



if (u == null)
{
u = new User();
List<UserGroup> myGroupList=new ArrayList<UserGroup>();
UserGroup usergroup=usergroupService.findByUserGroupNameEquals("CANDIDATE_GROUP");
myGroupList.add(usergroup);
Set<UserGroup> userGroups=new HashSet<UserGroup>(myGroupList);
u.setUserGroups(userGroups);
u.setEmailId(userLdap.getUsername());
userService.save(u);
return new LdapSecuredUser(u);
}
u.setEmailId(userLdap.getUsername());
String emailId=userLdap.getUsername();
u.setUserGroups(userService.getAllUserGroupsByEmailId(emailId));

userService.save(u);
for (UserGroup grantedAuthoritya : u.getUserGroups()) {
System.out.println(grantedAuthoritya.getUserGroupName());
};

return new LdapSecuredUser(u);
}

@Override
public void mapUserToContext(UserDetails arg0, DirContextAdapter arg1)
{
ldapUserDetailsMapper.mapUserToContext(arg0, arg1);
}

}

上面的代码是针对 Active Directory 完成的,其中不需要 contextsource。搜索 ldap 属性不需要显式查询。对我来说,它奏效了。

关于Spring Security LDAP 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28536141/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com