用户登录与AD域集成[通俗易懂]

用户登录与AD域集成[通俗易懂]1.关于AD域的介绍AD的全称是ActiveDirectory:活动目录域(Domain):1)域是Windows网络中独立运行的单位,域之间相互访问则需要建立信任关系(即TrustRelation)。信任关系是连接在域与域之间的桥梁。当一个域与其他域建立了信任关系后2)两个域之间不但可以按需要相互进行管理,还可以跨网分配文件和打印机等设备资源,使不同的域之间实现网络资源的共享与管理,…

大家好,又见面了,我是你们的朋友全栈君。

1.关于AD域的介绍

AD的全称是Active Directory:活动目录
域(Domain):
1)域是Windows网络中独立运行的单位,域之间相互访问则需要建立信任关系(即Trust Relation)。信任关系是连接在域与域之间的桥梁。当一个域与其他域建立了信任关系后
2)两个域之间不但可以按需要相互进行管理,还可以跨网分配文件和打印机等设备资源,使不同的域之间实现网络资源的共享与管理,以及相互通信和数据传输
域控制器(DC)
域控制器就是一台服务器,负责每一台联入网络的电脑和用户的验证工作。
组织单元(OU)
用户名服务器名(CN)

2.相关工具

LDAP管理工具                下载

相关的安装连接步骤就不详细介绍,连接成功截图,即可获取整个集团服务器域的信息

用户登录与AD域集成[通俗易懂]

3.与shiro集成开发登录验证

3.1 全局配置

ldap.url = ldap://10.0.58.222:389/
ldap.org = ou\=\u6F4D\u6fF4\u7328\u6237,dc\=weichai,dc\=com
ldap.user = ####
ldap.password = #####
ldap.ldapfactory = com.sun.jndi.ldap.LdapCtxFactory
ldap.authentication =simple

3.2 获取AD全局配置信息

/**
 * Created by llhy n on 2019/7/21.
 * AD域参数配置
 */
public class AdConfig {
    private String url;
    private String ldapfactory;
    private String authentication;
    private String org;
    private String user;
    private String password;
    private static AdConfig adConfig = null;

    public AdConfig() {
        url = Global.getConfig("ldap.url");
        ldapfactory = Global.getConfig("ldap.ldapfactory");
        authentication = Global.getConfig("ldap.authentication");
        org = Global.getConfig("ldap.org");
        user = Global.getConfig("ldap.user");
        password = Global.getConfig("ldap.password");
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getLdapfactory() {
        return ldapfactory;
    }

    public void setLdapfactory(String ldapfactory) {
        this.ldapfactory = ldapfactory;
    }

    public String getAuthentication() {
        return authentication;
    }

    public void setAuthentication(String authentication) {
        this.authentication = authentication;
    }

    public String getOrg() {
        return org;
    }

    public void setOrg(String org) {
        this.org = org;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static AdConfig getAdConfig() {
        if (adConfig == null){
            adConfig = new AdConfig();
        }
        return adConfig;
    }


}

3.3 shiro文件的配置

<!--AD 更改-->
	<!-- 3.1 直接配置继承了org.apache.shiro.realm.AuthorizingRealm的bean -->
	<bean id="userRealm" class="com.weichai.modules.sys.security.SystemAuthorizingRealm">
		<!-- 配置密码匹配器 -->
		<property name="credentialsMatcher" ref="credentialsMatcher"/>
	</bean>



	<!-- 凭证匹配器 -->
	<bean id="credentialsMatcher" class="com.weichai.modules.sys.security.CustomCredentialsMatcher">
	</bean>

3.4 登录身份认证后端

/**
 * 系统安全认证实现类
 * @author lhy
 * @version 2019-7-5
 */
public class SystemAuthorizingRealm extends AuthorizingRealm {

	private Logger logger = LoggerFactory.getLogger(getClass());

	private SystemService systemService;

	public SystemAuthorizingRealm() {
		this.setCachingEnabled(false);
	}

	/**
	 * 认证回调函数, 登录时调用
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) {
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

		/*int activeSessionSize = getSystemService().getSessionDao().getActiveSessions(false).size();
		if (logger.isDebugEnabled()){
			logger.debug("login submit, active session size: {}, username: {}", activeSessionSize, token.getUsername());
		}*/


		//获取图片二维码
		Session session = UserUtils.getSession();
		String code = (String)session.getAttribute(ValidateCodeServlet.VALIDATE_CODE);
		// 校验登录验证码
		if (LoginController.isValidateCodeLogin(token.getUsername(), false, false)){
			if (token.getCaptcha() == null || !token.getCaptcha().toUpperCase().equals(code)){
				throw new AuthenticationException("msg:验证码错误, 请重试.");
			}
		}
		if(StringUtils.isBlank(String.valueOf(token.getPassword()))){
            throw new AuthenticationException("msg:密码不能为空");
        }

		// 校验用户名密码
        User user = getSystemService().getUserByLoginNameLogin(token.getUsername());
		if (user != null) {
			//特权账户
			if ("1".equals(user.getIsPriv())) {
				if (StringUtils.isBlank(token.getCaptcha()) || StringUtils.isBlank(token.getSmsCode())) {
					throw new AuthenticationException("msg:请使用短信验证码登陆.");
				}
				//特权账户校验图片验证码
				if (!token.getCaptcha().toUpperCase().equals(code)) {
					throw new AuthenticationException("msg:图片码验证码验证失败, 请重试.");
				}

				try {
					validateSMS(token.getUsername(), token.getSmsCode(), token.getHost());
				} catch (SmsCodeValidateException e) {
					throw new AuthenticationException("msg:" + e.getMessage());
				}

			}
			if (Global.NO.equals(user.getLoginFlag())) {
				throw new AuthenticationException("msg:该已帐号禁止登录.");
			}
			if (user.getIsAd() != null && 1 == user.getIsAd()) {
				openAD(token.getUsername(), String.valueOf(token.getPassword()));
				token.setPass(true);
			}
			byte[] salt = Encodes.decodeHex(user.getPassword().substring(0, 16));
			return new SimpleAuthenticationInfo(new Principal(user, token.isMobileLogin()),
					user.getPassword().substring(16), ByteSource.Util.bytes(salt), getName());
		} else {
			return null;
		}
	}

    /**
     * 校验短信验证码
     * @param userName 用户名
     * @param smsCode 短信验证码
     * @param host 用户ip
     * @return
     */
    protected boolean validateSMS(String userName, String smsCode, String host) throws SmsCodeValidateException {
        String proName = Global.getConfig("cas.appid");
        String sendUrl = Global.getConfig("priv.validate");
        Map<String, Object> map = new HashMap<>();
        map.put("userName", userName);
        map.put("proName", proName);
        map.put("ip", host);
        map.put("valCode", smsCode);
        String result = null;

        try {
            result = HttpUtil.post(sendUrl, map);
            logger.error("[短信验证码校验结果]" + result);
        } catch (Exception e) {
            logger.error("[特权账户短信验证接口调用失败]" + e);
            return false;
        }
        JSONObject obj = JSONObject.parseObject(result);
        int code = (int) obj.get("code");
        String msg = (String)obj.get("msg");
        if (code == 0) {
            logger.error("[短信验证码校验通过]");
            return true;
        }

        logger.error("[短信验证码校验未通过,原因:"+msg+"]");
        throw new SmsCodeValidateException(msg);
    }

    private void openAD(String username, String password) {
        DirContext ctx = null;
        AdConfig adf = AdConfig.getAdConfig();
        Hashtable env = new Hashtable();
        if(StringUtils.isBlank(password)||StringUtils.isBlank(username)){
            throw new AuthenticationException("msg:用户名或者密码不正确!");
        }
        try {
            env.put(Context.PROVIDER_URL, adf.getUrl() + URLEncoder.encode(adf.getOrg(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String org=adf.getOrg();
        String dc=org.replaceFirst(",dc=","@");
        dc=dc.substring(dc.indexOf("@"));
        dc=dc.replaceFirst(",dc=",".");

        env.put(Context.SECURITY_PRINCIPAL, username+dc);
        env.put(Context.SECURITY_CREDENTIALS,password);
        env.put(Context.INITIAL_CONTEXT_FACTORY,adf.getLdapfactory());
        env.put(Context.SECURITY_AUTHENTICATION, adf.getAuthentication());

        try {
            ctx = new InitialDirContext(env);// 初始化上下文
        } catch (javax.naming.AuthenticationException e) {
            logger.error(e.toString());
            throw new AuthenticationException("msg:AD域身份验证失败!");
        } catch (javax.naming.CommunicationException e) {
            logger.error(e.toString());
            throw new AuthenticationException("msg:AD域连接失败!");
        } catch (Exception e) {
            logger.error(e.toString());
            throw new AuthenticationException("msg:AD域身份验证未知异常!");
        } finally{
            if(null!=ctx){
                try {
                    ctx.close();
                    ctx=null;
                } catch (Exception e) {
                    logger.error(e.toString());
                }
            }
        }
    }


    /**
	 * 获取权限授权信息,如果缓存中存在,则直接从缓存中获取,否则就重新获取, 登录成功后调用
	 */
	protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {
		if (principals == null) {
            return null;
        }

        AuthorizationInfo info = null;

        info = (AuthorizationInfo)UserUtils.getCache(UserUtils.CACHE_AUTH_INFO);

        if (info == null) {
            info = doGetAuthorizationInfo(principals);
            if (info != null) {
            	UserUtils.putCache(UserUtils.CACHE_AUTH_INFO, info);
            }
        }

        return info;
	}

	/**
	 * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		Principal principal = (Principal) getAvailablePrincipal(principals);
		// 获取当前已登录的用户
		if (!Global.TRUE.equals(Global.getConfig("user.multiAccountLogin"))){
			Collection<Session> sessions = getSystemService().getSessionDao().getActiveSessions(true, principal, UserUtils.getSession());
			if (sessions.size() > 0){
				// 如果是登录进来的,则踢出已在线用户
				if (UserUtils.getSubject().isAuthenticated()){
					for (Session session : sessions){
						getSystemService().getSessionDao().delete(session);
					}
				}
				// 记住我进来的,并且当前用户已登录,则退出当前用户提示信息。
				else{
					UserUtils.getSubject().logout();
					throw new AuthenticationException("msg:账号已在其它地方登录,请重新登录。");
				}
			}
		}
		User user = getSystemService().getUserByLoginName(principal.getLoginName());
		if (user != null) {
			SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
			List<Menu> list = UserUtils.getMenuList();
			for (Menu menu : list){
				if (StringUtils.isNotBlank(menu.getPermission())){
					// 添加基于Permission的权限信息
					for (String permission : StringUtils.split(menu.getPermission(),",")){
						info.addStringPermission(permission);
					}
				}
			}
			// 添加用户权限
			info.addStringPermission("user");
			// 添加用户角色信息
			for (Role role : user.getRoleList()){
				info.addRole(role.getEnname());
			}
			// 更新登录IP和时间
			getSystemService().updateUserLoginInfo(user);
			// 记录登录日志
			LogUtils.saveLog(Servlets.getRequest(), "系统登录");
			return info;
		} else {
			return null;
		}
	}

	@Override
	protected void checkPermission(Permission permission, AuthorizationInfo info) {
		authorizationValidate(permission);
		super.checkPermission(permission, info);
	}

	@Override
	protected boolean[] isPermitted(List<Permission> permissions, AuthorizationInfo info) {
		if (permissions != null && !permissions.isEmpty()) {
            for (Permission permission : permissions) {
        		authorizationValidate(permission);
            }
        }
		return super.isPermitted(permissions, info);
	}

	@Override
	public boolean isPermitted(PrincipalCollection principals, Permission permission) {
		authorizationValidate(permission);
		return super.isPermitted(principals, permission);
	}

	@Override
	protected boolean isPermittedAll(Collection<Permission> permissions, AuthorizationInfo info) {
		if (permissions != null && !permissions.isEmpty()) {
            for (Permission permission : permissions) {
            	authorizationValidate(permission);
            }
        }
		return super.isPermittedAll(permissions, info);
	}
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }

	/**
	 * 授权验证方法
	 * @param permission
	 */
	private void authorizationValidate(Permission permission){
		// 模块授权预留接口
	}

	/**
	 * 设定密码校验的Hash算法与迭代次数
	 */
	@PostConstruct
	public void initCredentialsMatcher() {
        CustomCredentialsMatcher matcher = new CustomCredentialsMatcher(SystemService.HASH_ALGORITHM);
		matcher.setHashIterations(SystemService.HASH_INTERATIONS);
		setCredentialsMatcher(matcher);
	}

//	/**
//	 * 清空用户关联权限认证,待下次使用时重新加载
//	 */
//	public void clearCachedAuthorizationInfo(Principal principal) {
//		SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
//		clearCachedAuthorizationInfo(principals);
//	}

	/**
	 * 清空所有关联认证
	 * @Deprecated 不需要清空,授权缓存保存到session中
	 */
	@Deprecated
	public void clearAllCachedAuthorizationInfo() {
//		Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
//		if (cache != null) {
//			for (Object key : cache.keys()) {
//				cache.remove(key);
//			}
//		}
	}

	/**
	 * 获取系统业务对象
	 */
	public SystemService getSystemService() {
		if (systemService == null){
			systemService = SpringContextHolder.getBean(SystemService.class);
		}
		return systemService;
	}

	/**
	 * 授权用户信息
	 */
	public static class Principal implements Serializable {

		private static final long serialVersionUID = 1L;

		private String id; // 编号
		private String loginName; // 登录名
		private String name; // 姓名
		private boolean mobileLogin; // 是否手机登录

//		private Map<String, Object> cacheMap;

		public Principal(User user, boolean mobileLogin) {
			this.id = user.getId();
			this.loginName = user.getLoginName();
			this.name = user.getName();
			this.mobileLogin = mobileLogin;
		}

        /***
         *
         * @param user
         * @param mobileLogin
         * @param pass 是否跳过验证
         */
        public Principal(User user, boolean mobileLogin,boolean pass) {
            this.id = user.getId();
            this.loginName = user.getLoginName();
            this.name = user.getName();
            this.mobileLogin = mobileLogin;
        }


		public String getId() {
			return id;
		}

		public String getLoginName() {
			return loginName;
		}

		public String getName() {
			return name;
		}

		public boolean isMobileLogin() {
			return mobileLogin;
		}

//		@JsonIgnore
//		public Map<String, Object> getCacheMap() {
//			if (cacheMap==null){
//				cacheMap = new HashMap<String, Object>();
//			}
//			return cacheMap;
//		}

		/**
		 * 获取SESSIONID
		 */
		public String getSessionid() {
			try{
				return (String) UserUtils.getSession().getId();
			}catch (Exception e) {
				return "";
			}
		}

		@Override
		public String toString() {
			return id;
		}

	}

}

3.5 AD域认证测试类

/**
 * AD域本地连接测试
 * @author linhaiy
 * @version 2019.07.20
 */
public class AdTest {

	private static Logger logger = LoggerFactory.getLogger(AdTest.class);

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        String username = "admin";
        String password = "Admin12356!!";
        openAD(username,password);
	}

	@SuppressWarnings("unchecked")
	private static void openAD(String username, String password) {
		DirContext ctx = null;
		AdConfig adf = AdConfig.getAdConfig();
		Hashtable env = new Hashtable();
		if (StringUtils.isBlank(password) || StringUtils.isBlank(username)) {
			throw new AuthenticationException("msg:用户名或者密码不正确!");
		}
		try {
			env.put(Context.PROVIDER_URL, adf.getUrl() + URLEncoder.encode(adf.getOrg(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String org = adf.getOrg();
		String dc = org.replaceFirst(",dc=", "@");
		dc = dc.substring(dc.indexOf("@"));
		dc = dc.replaceFirst(",dc=", ".");

		env.put(Context.SECURITY_PRINCIPAL, username + dc);
		env.put(Context.SECURITY_CREDENTIALS, password);
		env.put(Context.INITIAL_CONTEXT_FACTORY, adf.getLdapfactory());
		env.put(Context.SECURITY_AUTHENTICATION, adf.getAuthentication());

		try {
			ctx = new InitialDirContext(env);// 初始化上下文
		} catch (javax.naming.AuthenticationException e) {
			logger.error(e.toString());
			throw new AuthenticationException("msg:AD域身份验证失败!");
		} catch (javax.naming.CommunicationException e) {
			logger.error(e.toString());
			throw new AuthenticationException("msg:AD域连接失败!");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new AuthenticationException("msg:AD域身份验证未知异常!");
		} finally {
			if (null != ctx) {
				try {
					ctx.close();
					ctx = null;
				} catch (Exception e) {
					logger.error(e.toString());
				}
			}
		}
	}

}

这样就增加了域用户验证登录功能,即用户在登录时既可以使用数据库里的用户,也可以使用域服务器上的域用户和密码登录

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/145452.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 建造者模式

    问题:相同的过程经过不同的处理方法得到不同的结果注意:1.基类保护属性的使用(该属性在子类中同为保护属性,只能类的成员变量调用)2.公共属性和公共方法全放在基类3.泛化、依赖、组合关系的

    2021年12月18日
    40
  • windows-install-python-and-sphinx(*.rst file)

    windows-install-python-and-sphinx(*.rst file)

    2022年1月4日
    35
  • java 舆情分析_基于Java实现网络舆情分析系统研究与实现.doc[通俗易懂]

    java 舆情分析_基于Java实现网络舆情分析系统研究与实现.doc[通俗易懂]基于Java实现网络舆情分析系统研究与实现基于Java实现网络舆情分析系统研究与实现摘要:通过对各大门户网站、论坛和贴吧的留言和评论的爬取,录入后台数据库。用户可根据主题、内容进行搜索查看。通过利用中科院分词算法进行实现对爬去下来的内容进行分词处理,分词处理后的结果利用自行研究出来的基于权值算法实现的中文情感分析进行评论的倾向性分析,通过对句子结构和主张词以及情感副词的判断来对评论的情感倾向性做出…

    2022年9月21日
    1
  • 接口定义规则

    接口定义规则写了五年代码,打算总结一下接口一下规范.  刚刚进公司时,写的接口返回过map,string,object,json等等,回头看看写的乱七八糟的. 1.接口最好是统一返回自定义实体.  如:ResultMessages.    privateString   resultCode,//返回code如:200(成功) 错误代码自己定义   message; 

    2022年5月29日
    42
  • 国际企业邮箱优势有哪些?国际邮箱申请方法教学「建议收藏」

    国际企业邮箱优势有哪些?国际邮箱申请方法教学「建议收藏」相信很多小伙伴都用过邮箱,但是大家只会用邮箱收发邮件,处理工作的事务,事实上并不了解邮箱。企业邮箱的优势都有哪些,如何更好地使用邮箱呢?那么下面,小编就以TOM企业邮箱为例,为大家详细讲解邮箱的优势吧!国际企业邮箱优势市场上主流邮箱产品有普通、VIP和企业邮箱三种,针对于国际业务邮件,其中以企业邮箱为首的服务最为全面,其次是VIP邮箱。优势一:海外通道企业邮箱最大优势在于,可以和国外邮箱进行信件往来,原因来自于企业邮箱设有独立的海外通道,将国内和国外的邮箱通道很好的进行连通,有效保障收件收发稳定。

    2022年9月15日
    0
  • 机器学习——决策树模型:Python实现

    机器学习——决策树模型:Python实现机器学习——决策树模型:Python实现欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML图表FLowchart流程图导出与导入导出导入欢迎使用Markdown编辑器你好!这是你第一次使用Markdown编辑器所展示的欢迎页。如果你

    2022年10月19日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号