【问题标题】:Appletviewer not running from command prompt: java.security.AccessControlException: access denied (java.net.SocketPermission smtp.gmail.com resolve)Appletviewer 未从命令提示符运行:java.security.AccessControlException:访问被拒绝(java.net.SocketPermission smtp.gmail.com 解决)
【发布时间】:2012-04-17 11:05:22
【问题描述】:

我创建了一个简单的小程序,通过单击“发送邮件”按钮使用 smtp.gmail.com 发送邮件。它可以从 Eclipse 完美运行。从 eclipse 我将它作为 Java Applet 运行,它发送邮件没有任何错误。
但是当从 appletviewer 运行时,在 eclipse 之外它会抛出一个错误:java.security.AccessControlException: access denied (java.net.SocketPermission smtp.gmail.com resolve)
我已经签署了我的程序的 JAR。签名后,如果小程序是从 Internet Explorer 运行的,它会发送邮件,但如果小程序是从谷歌 Chrome 浏览器或 appletviewer 运行的,则会抛出上述错误。

创建密钥库的命令: "c:\Program Files\Java\jre6\bin\keytool.exe" -genkey -alias -validity 365 -keystore -keyalg rsa

对 jar 进行签名的命令: \bin\jarsigner.exe -signedjar -keystore

JAR 仅通过从 Eclipse 中导出来形成。

运行小程序的命令: "c:\Program Files\Java\jdk1.6.0_27\bin\appletviewer.exe"

请查看代码,让我知道我做错了什么......

小程序代码

package in.appletmail;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class SendMailApplet extends JApplet
{
    boolean isStandalone = false;
    JPanel jPanel1 = new JPanel();
    JTextField jTextField1 = new JTextField();
    JButton jButton = new JButton("Send Mail");
    GridBagLayout gridBagLayout1 = new GridBagLayout();

    // Construct the applet
    public SendMailApplet()
    {
        // TODO Auto-generated constructor stub
    }

    // Initialize the applet
    public void init()
    {
        try
        {
            jbInit();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    // Initializing the components
    private void jbInit() throws Exception
    {
        this.setSize(new Dimension(500, 200));
        jPanel1.setLayout(gridBagLayout1);

        jTextField1.setText("First Applet");
        this.getContentPane().add(jPanel1, BorderLayout.CENTER);
        jPanel1.add(jTextField1, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                new Insets(140, 128, 139, 132), 77, 0));

        jPanel1.add(jButton, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                new Insets(140, 128, 200, 132), 77, 0));


        final String mailStatus = "Testing Applet Viewer";
        jTextField1.setText(mailStatus);

        jButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    SendMail.mailVariable("Test mail from applet");
                    jTextField1.setText("Mail Send");
                } catch (Exception e1)
                {
                    jTextField1.setText(e1.toString());
                }

            }
        });
        // jTextField1.setText("Mail Send");
    }

    // Start the applet
    public void start()
    {

    }

    // Stop the applet
    public void stop()
    {

    }

    // Delete the applet
    public void destroy()
    {

    }

    // Fetch applet information
    public String getAppletInfo()
    {
        return "Applet-Information";
    }
}

发送邮件的代码

package in.appletmail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public final class SendMail
{
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SMTP_PORT = "587";
    private static final String SMTP_AUTH_USER = "username";
    private static final String SMTP_AUTH_PWD = "pass";

    private static final String emailMsgTxt = "Testing mail from Applet, Test again";
    private static final String emailSubjectTxt = "Test mail from Applet via Google";
    private static final String emailFromAddress = "test.mail@abc.in";

    // Add List of Email address to who email needs to be sent to
    private static final String[] emailList = { "To@gmail.com" };

    public static String testFunctionCall()
    {
        return "Mailing function will be called";
    }

    public static String mailVariable(String variableValue)
            throws MessagingException
    {
        SendMail smtpMailSender = new SendMail();
        return smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt
                + "\n variable value:  " + variableValue, emailFromAddress);
    }

    public String postMail(String recipients[], String subject, String message,
            String from) throws MessagingException
    {
        StringBuffer status = new StringBuffer();
        boolean debug = false;

        // Set the host smtp address
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.auth", "true");

        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);

        session.setDebug(debug);
        status.append("Session set;");
        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++)
        {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);
        status.append("Recipients set;");

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        status.append("Subject and Content set;");
        Transport.send(msg);
        status.append("Mail send;");
        return status.toString();
    }

    /**
     * SimpleAuthenticator is used to do simple authentication when the SMTP
     * server requires it.
     */
    private class SMTPAuthenticator extends Authenticator
    {

        public PasswordAuthentication getPasswordAuthentication()
        {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }
}

调用小程序的HTML:

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>ABC</title>
</head>
<body bgcolor="#CCCCCC">

<table border="0" cellpadding="0" cellspacing="0" width="144" >
  <tr>
    <td width="10%" height="25" style= "height:25">Powered by Xpace :)
    </td>
    <td width="90%"style= "height:25">
    <APPLET CODEBASE="E:\Gunjan\Workspace\TestAppletExecution\Signed\"
        ARCHIVE="TestApplet.jar, mail.jar"
        CODE="in.appletmail.SendMailApplet.class"
        NAME="Send Mail"
        MAYSCRIPT
        WIDTH="750"
        HEIGHT="350"
        HSPACE="0" VSPACE="0" ALIGN="top">        
    </APPLET>
    </td>
  </tr>
</table>
</body>


</html>

【问题讨论】:

  • “我已经签署了 JAR .. 但如果从 Google Chrome 浏览器运行小程序,则会引发上述错误” 这就是您应该调查的内容,因为策略文件不是适合真正的 WWW 部署。
  • 能否请您进一步说明这一点?以及如何在实际的 Web 服务器上进行部署?这是共享服务器的问题还是专用服务器的问题?

标签: java security applet appletviewer


【解决方案1】:

小程序在沙箱中运行,有一些限制,您必须配置文件 jre/lib/security/java.policy 添加以下行:

permission java.net.SocketPermission "smtp.gmail.com:587", "listen,resolve";

【讨论】:

  • 感谢迭戈的帮助。但它仍然无法正常工作。事件尝试提供“授予 { 权限 java.security.AllPermission; };”在 java.policy 文件中。但没有奏效。知道 Eclipse 如何运行 appletviewer。知道 Eclipse 用来运行相同命令的确切命令吗??
  • 嘿,它成功了。我的坏:(。我在单独安装的 JRE 的 jre/lib/security 中进行了更改。虽然,我应该在安装了 JRE 的 JDK 中进行更改,因为它在环境变量中定义为 JAVA_HOME。非常感谢。不过,我还是想知道为什么它不能仅通过使用签名的小程序来工作。我想知道我们是否可以有一些可以设置权限级别的批处理脚本,因为这将有助于自动化构建过程。在此先感谢 :)
  • @Gun,您是否设法为此任务编写了一个批处理脚本来自动化?我们现在的情况一样吗?
猜你喜欢
  • 2012-11-27
  • 2015-02-23
  • 2015-06-30
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多