⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spnegohttpurlconnection.java

📁 JAAS 例子代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 *  The orginal class is  "jcifs.http.NtlmHttpFilter", 
 *  SPNEGO protocol support is added to this class by Wayne Lou
 */

package com.tidalsoft.webconsole.sso.http;

import java.io.*;
import java.net.*;
import java.security.*;
import java.util.*;

import javax.security.auth.login.*;

import jcifs.*;
import jcifs.ntlmssp.*;
import jcifs.util.*;

import com.tidalsoft.webconsole.sso.*;

/**
 *Wraps an <code>HttpURLConnection</code> to provide Kerberos delegate and NTLM
 * authentication services.
 * 
 * Authentication for HTTP Connections</a>.
 */
public class SpnegoHttpURLConnection extends HttpURLConnection {

	private static final int MAX_REDIRECTS = Integer.parseInt(System
			.getProperty("http.maxRedirects", "20"));

	private static final int LM_COMPATIBILITY = Config.getInt(
			"jcifs.smb.lmCompatibility", 0);
	
	private HttpURLConnection connection;

	private Map requestProperties;

	private Map headerFields;

	private ByteArrayOutputStream cachedOutput;

	private String authProperty;

	private String authMethod;

	private boolean handshakeComplete;

	/**
	 * User credential, get it from thread local, added by wayne 
	 */
	private UserCredential userCred;

	public UserCredential getUserCred() {
		return userCred;
	}

	public void setUserCred(UserCredential userCred) {
		this.userCred = userCred;
	}

	public SpnegoHttpURLConnection(HttpURLConnection connection) {
		super(connection.getURL());
		this.connection = connection;
		requestProperties = new HashMap();
	}

	public void connect() throws IOException {
		if (connected)
			return;
		connection.connect();
		connected = true;
	}

	private void handshake() throws IOException {
		if (handshakeComplete)
			return;
		doHandshake();
		handshakeComplete = true;
	}

	public URL getURL() {
		return connection.getURL();
	}

	public int getContentLength() {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getContentLength();
	}

	public String getContentType() {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getContentType();
	}

	public String getContentEncoding() {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getContentEncoding();
	}

	public long getExpiration() {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getExpiration();
	}

	public long getDate() {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getDate();
	}

	public long getLastModified() {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getLastModified();
	}

	public String getHeaderField(String header) {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getHeaderField(header);
	}

	private Map getHeaderFields0() {
		if (headerFields != null)
			return headerFields;
		Map map = new HashMap();
		String key = connection.getHeaderFieldKey(0);
		String value = connection.getHeaderField(0);
		for (int i = 1; key != null || value != null; i++) {
			List values = (List) map.get(key);
			if (values == null) {
				values = new ArrayList();
				map.put(key, values);
			}
			values.add(value);
			key = connection.getHeaderFieldKey(i);
			value = connection.getHeaderField(i);
		}
		Iterator entries = map.entrySet().iterator();
		while (entries.hasNext()) {
			Map.Entry entry = (Map.Entry) entries.next();
			entry.setValue(Collections
					.unmodifiableList((List) entry.getValue()));
		}
		return (headerFields = Collections.unmodifiableMap(map));
	}

	public Map getHeaderFields() {
		if (headerFields != null)
			return headerFields;
		try {
			handshake();
		} catch (IOException ex) {
		}
		return getHeaderFields0();
	}

	public int getHeaderFieldInt(String header, int def) {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getHeaderFieldInt(header, def);
	}

	public long getHeaderFieldDate(String header, long def) {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getHeaderFieldDate(header, def);
	}

	public String getHeaderFieldKey(int index) {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getHeaderFieldKey(index);
	}

	public String getHeaderField(int index) {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getHeaderField(index);
	}

	public Object getContent() throws IOException {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getContent();
	}

	public Object getContent(Class[] classes) throws IOException {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getContent(classes);
	}

	public Permission getPermission() throws IOException {
		return connection.getPermission();
	}

	public InputStream getInputStream() throws IOException {
		try {
			handshake();
		} catch (IOException ex) {
		}
		return connection.getInputStream();
	}

	public OutputStream getOutputStream() throws IOException {
		try {
			connect();
		} catch (IOException ex) {
		}
		OutputStream output = connection.getOutputStream();
		cachedOutput = new ByteArrayOutputStream();
		return new CacheStream(output, cachedOutput);
	}

	public String toString() {
		return connection.toString();
	}

	public void setDoInput(boolean doInput) {
		connection.setDoInput(doInput);
		this.doInput = doInput;
	}

	public boolean getDoInput() {
		return connection.getDoInput();
	}

	public void setDoOutput(boolean doOutput) {
		connection.setDoOutput(doOutput);
		this.doOutput = doOutput;
	}

	public boolean getDoOutput() {
		return connection.getDoOutput();
	}

	public void setAllowUserInteraction(boolean allowUserInteraction) {
		connection.setAllowUserInteraction(allowUserInteraction);
		this.allowUserInteraction = allowUserInteraction;
	}

	public boolean getAllowUserInteraction() {
		return connection.getAllowUserInteraction();
	}

	public void setUseCaches(boolean useCaches) {
		connection.setUseCaches(useCaches);
		this.useCaches = useCaches;
	}

	public boolean getUseCaches() {
		return connection.getUseCaches();
	}

	public void setIfModifiedSince(long ifModifiedSince) {
		connection.setIfModifiedSince(ifModifiedSince);
		this.ifModifiedSince = ifModifiedSince;
	}

	public long getIfModifiedSince() {
		return connection.getIfModifiedSince();
	}

	public boolean getDefaultUseCaches() {
		return connection.getDefaultUseCaches();
	}

	public void setDefaultUseCaches(boolean defaultUseCaches) {
		connection.setDefaultUseCaches(defaultUseCaches);
	}

	public void setRequestProperty(String key, String value) {
		if (key == null)
			throw new NullPointerException();
		List values = new ArrayList();
		values.add(value);
		boolean found = false;
		Iterator entries = requestProperties.entrySet().iterator();
		while (entries.hasNext()) {
			Map.Entry entry = (Map.Entry) entries.next();
			if (key.equalsIgnoreCase((String) entry.getKey())) {
				entry.setValue(values);
				found = true;
				break;
			}
		}
		if (!found)
			requestProperties.put(key, values);
		connection.setRequestProperty(key, value);
	}

	public void addRequestProperty(String key, String value) {
		if (key == null)
			throw new NullPointerException();
		List values = null;
		Iterator entries = requestProperties.entrySet().iterator();
		while (entries.hasNext()) {
			Map.Entry entry = (Map.Entry) entries.next();
			if (key.equalsIgnoreCase((String) entry.getKey())) {
				values = (List) entry.getValue();
				values.add(value);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -