Java Network LAN Manager Authentication

Java Network LAN Manager Authentication (NTLM Authentication)

Java URL connections works fine from home/public networks. But these won't work from corporate networks due to proxy issues.

To overcome this we set proxy properties in the system properties as below.

Proxy system properties

System.setProperty("http.proxySet", "true");
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty("http.proxyHost", "host");
System.setProperty("http.proxyPort", "port");
System.setProperty("http.proxyUser", "user");
System.setProperty("http.proxyPassword", "password");

This works for some, and in some networks this will not work
When you end up with the IOException : Bad response: (407Proxy Authentication required) it means a proxy authentication is required.

Java provides a solution to this using java.net.Autneticator
Write your own Authenticator to serve username and password of you closed network.


Custom Proxy Aunthenticator

ProxyAuthenticator.java


public class ProxyAuthenticator extends java.net.Authenticator{
 
 private String user, password;

    public ProxyAuthenticator(String user, String password) {
        this.user = user;
        this.password = password;
    }

    protected java.net.PasswordAuthentication getPasswordAuthentication() {
     System.out.println("Authentication required.");     
        return new PasswordAuthentication(user, password.toCharArray());
    }

}

Before you open a URL connection add the below code

//set Authenticator
Authenticator.setDefault(new ProxyAuthenticator("user", "password"));

//Then set the usual stuff
System.setProperty("http.proxyHost", "proxy.host");
System.setProperty("http.proxyPort", "port");

//Here goes URL processing
URLConnection conn = url.openConnection();


This will pass through proxy authentication and connection will work fine.
In case you have an error in username/password - you can see the sop "Authentication required" will keep printed and you will end up with 403 Forbidden (bad password).
Make sure to include your domain in the username (domain\user). On a string it should be "domain\\user"

Comments

  1. domain\\user - useful info

    ReplyDelete
  2. CC98F04492
    Many websites now focus on providing high-quality content tailored to their audience. For example, dtfhub.com offers valuable resources for digital transfer fans. By exploring such platforms, users can enhance their knowledge and skills effortlessly. Staying updated with the latest trends is essential for anyone interested in this field.

    ReplyDelete

  3. Das BSI-Gesetz schreibt im § 8 vor, dass Unternehmen ihre IT-Infrastruktur gegen unbefugten Zugriff absichern müssen. Active Directory bildet dabei das Herzstück der Netzwerkverwaltung und ist häufig Ziel von Angriffen. Um die Sicherheit zu erhöhen, empfiehlt sich das Tier-Modell für Hardening, bei dem verschiedene Schutzstufen definiert werden. Ergänzend sorgen Tools wie LAPS für eine sichere Verwaltung der Passwörter und Kerberos-Schutzmechanismen verhindern die Nutzung gestohlener Tickets; diese Maßnahmen reduzieren die Angriffsfläche deutlich und sind in der Praxis nachweislich wirksam. Auf https://csvisor.de/ wird deutlich, wie diese Techniken im Alltag umgesetzt werden können.

    ReplyDelete

Post a Comment

Popular posts from this blog

Log4j multiple WAR files in single EAR configuration

Java NIO2 - Watching a directory for changes. FileWatcherService.

Ubuntu / kubuntu - Laptop key board cursor jumping issue