我创建了一个登录窗口,其中包含两个用于用户名和密码的文本框,以及两个用于登录和取消的命令按钮。按下取消按钮后窗口关闭。登录凭据应作为参数传递给powershell脚本以登录网站..我无法链接PowerShell和java swing代码..
我的java swing代码是:
import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Credential extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public Credential() {
setUndecorated(true);
setBackground(new Color(0, 0, 0, 0));
setSize(new Dimension(400, 300));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (g instanceof Graphics2D) {
final int R = 220;
final int G = 220;
final int B = 250;
Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B,
0), 0.0f, getHeight(), new Color(R, G, B, 255),
true);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
Font font = new Font("Serif", Font.PLAIN, 45);
g2d.setFont(font);
g2d.setColor(Color.lightGray);
g2d.drawString("Get Credential", 60, 80);
}
}
};
setContentPane(panel);
setLayout(new FlowLayout());
placeComponents(panel);
}
public static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("User");
userLabel.setBounds(40, 100, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(130, 100, 160, 25);
userText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(40, 140, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(130, 140, 160, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 180, 80, 25);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel.add(loginButton);
JButton cancelButton = new JButton("cancel");
cancelButton.setBounds(220, 180, 80, 25);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(cancelButton);
}
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isPerPixelTranslucencySupported = gd
.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
if (!isPerPixelTranslucencySupported) {
System.out.println("Per-pixel translucency is not supported");
System.exit(0);
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Credential gtw = new Credential();
gtw.setVisible(true);
}
});
}
}
powershell脚本是这样的:
$url = "https://www.jabong.com/customer/account/login/"
$username = $cred.username
$username = $username.Replace("\", "")
$password = $cred.GetNetworkCredential().password
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.Busy -eq $true)
{
Start-Sleep 1;
}
$ie.Document.getElementById("LoginForm_email").value = $username
$ie.Document.getElementByID("LoginForm_password").value=$password
$ie.Document.getElementByID("qa-login-button").Click();
答案 0 :(得分:1)
有几种方法可以解决这个问题。例如,您可以使用用户名和密码作为参数运行PowerShell脚本:
Process process = new ProcessBuilder(
"powershell.exe",
"-File", "C:\\path\\to\\your.ps1",
"-Username", userText.getText(),
"-Password", passwordText.getText()
).start();
并制作脚本的$Username
和$Password
parameters:
[CmdletBinding()]
Param(
[Parameter()][string]$Username,
[Parameter()][string]$Password
)
$url = 'https://www.jabong.com/customer/account/login/'
$ie = New-Object -COM internetexplorer.application
...
但这是不好的做法,因为密码会以明文形式出现在流程列表中。
更好的选择是将用户名和密码作为environment variables传递:
ProcessBuilder pb = new ProcessBuilder(
"powershell.exe",
"-File", "C:\\path\\to\\your.ps1"
);
Map<String, String> env = pb.environment();
env.put("USER", userText.getText());
env.put("PASS", passwordText.getText());
Process process = pb.start();
可以在脚本中访问,如下所示:
$username = $env:USER
$password = $env:PASS
或者你可以反过来从PowerShell脚本运行Java程序:
$username, $password = & java -jar '.\Credential.jar'
并将凭据写入Java程序中的stdout:
System.out.println(userText.getText());
System.out.println(passwordText.getText());
请注意,无论哪种方式,您都需要制作类的userText
和passwordText
个实例变量,并将placeComponents()
更改为非静态方法。
话虽如此,如果Java / Swing不是硬性要求,使用Windows forms也可以提供一种构建自定义对话框的方法(允许您保持对话和脚本代码在同一个文件中):
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function New-Button($label, $action, $width, $height, $top, $left) {
$btn = New-Object System.Windows.Forms.Button
$btn.Location = New-Object System.Drawing.Size($left, $top)
$btn.Size = New-Object System.Drawing.Size($width, $height)
$btn.Text = $label
$btn.Add_Click($action)
$btn
}
function New-Label($label, $width, $height, $top, $left) {
$lbl = New-Object System.Windows.Forms.Label
$lbl.Location = New-Object System.Drawing.Size($left, $top)
$lbl.Size = New-Object System.Drawing.Size($width, $height)
$lbl.Text = $label
$lbl
}
function New-Input($width, $height, $top, $left, $mask=$false) {
$input = New-Object System.Windows.Forms.TextBox
$input.UseSystemPasswordChar = $mask
$input.Location = New-Object System.Drawing.Size($left, $top)
$input.Size = New-Object System.Drawing.Size($width, $height)
$input
}
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(250, 160)
$form.Controls.Add((New-Label 'Username:' 60 20 20 10))
$user = New-Input 150 20 20 70
$form.Controls.Add($user)
$form.Controls.Add((New-Label 'Password:' 60 20 50 10))
$pass = New-Input 150 20 50 70 $true
$form.Controls.Add($pass)
$form.Controls.Add((New-Button 'OK' {$form.Close()} 70 23 85 70))
$form.Controls.Add((New-Button 'Cancel' {$user.Text=''; $pass.Text=''; $form.Close()} 70 23 85 150))
[void]$form.ShowDialog()
$username = $user.Text
$password = $pass.Text