java 判断当前系统用户是不是管理员

eclipse 专栏收录该内容
3 篇文章 0 订阅

 

代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AdminUitl {
	private static final Logger LOG = LoggerFactory.getLogger(AdminUitl.class);

	public static void main(String[] args) {

		System.out.println(isAdmin());

	}

	public static boolean isAdmin() {
		String isAdmin = runcmd("WHOAMI /GROUPS");
		if (isAdmin.contains("Administrators")) {
			return true;
		}
		return false;
	}

	public static String runcmd(String cmd) {
		StringBuffer sBuffer = null;
		BufferedReader bReader = null;
		Process p = null;
		try {
			p = Runtime.getRuntime().exec(cmd);
			Thread.sleep(100);
			bReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
			sBuffer = new StringBuffer();
			String line = null;
			while ((line = bReader.readLine()) != null) {
				sBuffer.append(line);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LOG.info("Administrators error");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LOG.info("Administrators error");
		} finally {
			try {
				bReader.close();
				p.destroy();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return sBuffer.toString();
	}
}

 

 

  • 1
    点赞
  • 0
    评论
  • 3
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值