如何通过Chrome 2021访问windows用户名和域名(从Active Directory)



我读过关于从chrome获取windows用户名和域名的文章,其中一个api是npapi,现在不推荐使用。我还看到javascript不是一个选项,因为很明显,如果它被允许,那么它就是一个巨大的安全风险。有没有办法在Chrome中做到这一点?

好吧,因为我认为没有直接的方法可以做到这一点。但我设法找到了一种通过硒网络驱动程序的方法。因此,我只需创建一个web驱动程序,并通过java:将值写入chrome的本地存储

WebDriverManager.getInstance(ChromeDriver.class).setup();
WebDriver driver = new ChromeDriver();
driver.get("http://yourapp_url");
LocalStorage local = ((WebStorage) driver).getLocalStorage();
local.setItem("win_domain", System.getenv().get("USERDOMAIN"));
local.setItem("win_username", System.getenv().get("USERNAME"));

pom是:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>WindowsChromeLauncher</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-devtools-v86</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.4.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>SeleniumLocalStorage</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

您需要运行此命令并创建一个可执行的jar:mvn干净编译程序集:单个

最新更新