java ntlm,使用当前用户的凭据进行javamail NTLM身份验证

本文探讨如何使用JavaMail API通过NTLM认证与Exchange服务器进行邮件发送,目标是实现无须显式提供用户名和密码的单点登录功能,仅允许网络内部计算机发送邮件并解决安全问题。作者提供了使用空密码失败及不指定密码引发的异常示例,并暗示可能需要利用Windows API获取当前用户凭证。
摘要由CSDN通过智能技术生成

How can I use the JavaMail API with NTLM authentication to an Exchange server without having to specify user name and password but instead automatically use the credentials of the currently logged-in user? ("single sign on")

My intention is to let my client program (which runs on Windows machines in my company's network) to be able to send email without having to specify credentials and without having to allow relay for computers in the network due to security concerns. (In the end I would like to do this with log4j's SmtpAppender in combination with system property overrides, but I creating the SSCCE below to debug the issue.)

I AM able to successfully use NTLM authentication to send email if I specify the user name and the valid Windows password corresponding to it:

import java.util.Properties;

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 class MailTest {

public static void main(String[] args) {

String from = "myusername@mydomain.nl";

String to = "anotherusername@mydomain.nl";

Properties props = new Properties();

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.host", "myserver.mydomain");

props.put("mail.debug", "true");

props.put("mail.smtp.auth.mechanisms", "NTLM");

props.put("mail.smtp.auth.ntlm.domain", "mydomain");

final String username = "myusername";

final String password = "mypassword";

Session session =

Session.getInstance(props, new javax.mail.Authenticator() {

@Override

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(username, password);

}

});

try {

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(to));

message.setSubject("Testing!");

message.setText("Hello world!");

Transport.send(message);

System.out.println("Sent message successfully");

} catch (MessagingException e) {

throw new RuntimeException(e);

}

}

}

When I use a blank password this fails with javax.mail.Authentication

FailedException: 535 5.7.3 Authentication unsuccessful.

When I replace the code to create a Session with simply:

Session session = Session.getInstance(props);

then the following exception is thrown and I can see from the debug output that no attempt is made to connect to the server:

Exception in thread "main" java.lang.RuntimeException: javax.mail.Authentication

FailedException: failed to connect, no password specified?

at MailTest.main(MailTest.java:51)

Caused by: javax.mail.AuthenticationFailedException: failed to connect, no passw

ord specified?

at javax.mail.Service.connect(Service.java:329)

at javax.mail.Service.connect(Service.java:176)

at javax.mail.Service.connect(Service.java:125)

at javax.mail.Transport.send0(Transport.java:194)

at javax.mail.Transport.send(Transport.java:124)

解决方案

I'm not sure there's any way to do this without having the username and password. Possibly you can write some Windows-specific code that can retrieve the username and password from the local authentication service and then pass that in to JavaMail. JavaMail itself certainly has no way to do this.

Also, you might want to upgrade to JavaMail 1.5.2, although it won't help you with this problem.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值