spring boot security Active Directory 登录(自定义角色、权限)

spring boot 版本:2.0.4

spring security 版本:5.07

spring security 的验证,需要做的是下面三个:

实现AuthenticationProvider(验证业务等)

实现UserDetailsService(获取用户的业务)

实现UserDetails(用户实体)

但是,查询过spring的文档,只有简单几行的写了,如果使用AD域,需要用ActiveDirectoryLdapAuthenticationProvider做验证业务。而ActiveDirectoryLdapAuthenticationProvider没有setUserDetailsService这个方法,继续查询源码,ActiveDirectoryLdapAuthenticationProvider是继承抽象类AbstractLdapAuthenticationProvider,在AbstractLdapAuthenticationProvider继续找authenticate()方法研究,这个是验证过程的方法。找到了关键的几行代码,如下:

UserDetails user = this.userDetailsContextMapper.mapUserFromContext(userData,
				authentication.getName(),
				loadUserAuthorities(userData, authentication.getName(),
						(String) authentication.getCredentials()));

this.userDetailsContextMapper是接口UserDetailsContextMapper,实例化的类是LdapUserDetailsMapper

protected UserDetailsContextMapper userDetailsContextMapper = new LdapUserDetailsMapper();

继续查看LdapUserDetailsMapper的mapUserFromContext方法

public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
			Collection<? extends GrantedAuthority> authorities) {
		String dn = ctx.getNameInNamespace();

		this.logger.debug("Mapping user details from context with DN: " + dn);

		LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();
		essence.setDn(dn);

		Object passwordValue = ctx.getObjectAttribute(this.passwordAttributeName);

		if (passwordValue != null) {
			essence.setPassword(mapPassword(passwordValue));
		}

		essence.setUsername(username);

		// Map the roles
		for (int i = 0; (this.roleAttributes != null)
				&& (i < this.roleAttributes.length); i++) {
			String[] rolesForAttribute = ctx.getStri
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值