页面树结构

版本比较

标识

  • 该行被添加。
  • 该行被删除。
  • 格式已经改变。

新版Excel插件支持通过添加扩展包的方式自定义CAS登录表单内容

需要满足:

  • 使用新版的Excel插件

  • 使用新版的CAS扩展包

以下是一些自定义CAS登录表单的实例

1、登录CAS时密码加密

1、新增扩展包,依赖CAS扩展包:

Image Added

2、创建一个类继承CASClientConnector,重写open方法,在open中对密码进行加密,比如本示例中MyCASClientConnector重写open方法,并对密码进行倒转加密:

代码块
languagejava
linenumberstrue
collapsetrue
package smartbi.cas.client;
import smartbi.sdk.CASClientConnector;
public class MyCASClientConnector extends CASClientConnector{
	public MyCASClientConnector(String smartbiUrl) {
		super(smartbiUrl);
	}
	
	@Override
	public boolean open(String user, String password) {
		return super.open(user, encodePassowrd(password));
	}
	
	private String encodePassowrd(String password) {
		return new StringBuilder(password).reverse().toString();
	}
}

3、实现ICASClientConnectorCreator接口,在createCASClientConnector返回2中connector的实例对象

代码块
languagejava
linenumberstrue
collapsetrue
package smartbi.cas.client;

import smartbi.sdk.CASClientConnector;

public class MyCASClientConnectorCreator implements ICASClientConnectorCreator {
	private static final MyCASClientConnectorCreator INSTANCE = new MyCASClientConnectorCreator();
	
	private MyCASClientConnectorCreator() {
	}
	
	public static MyCASClientConnectorCreator getInstance() {
		return INSTANCE;
	}
  
	public CASClientConnector createCASClientConnector(String smartbiUrl) {
		return new MyCASClientConnector(smartbiUrl);
	}
}

4、新增Module类,在activate方法中注册creator:

代码块
languagejava
linenumberstrue
collapsetrue
package smartbi.cas;

import smartbi.cas.client.MyCASClientConnectorCreator;
import smartbi.cas.client.PluginCasLoginModule;
import smartbi.framework.IModule;

public class CasPwdEncoderModule implements IModule {

	private static final CasPwdEncoderModule INSTANCE = new CasPwdEncoderModule();
	
	private CasPwdEncoderModule() {
	}
	
	public static CasPwdEncoderModule getInstance() {
		return INSTANCE;
	}
	
	@Override
	public void activate() {
		PluginCasLoginModule.getInstance().setCASClientConnectorCreator(MyCASClientConnectorCreator.getInstance());
	}

}

2、登录CAS时提交表单字段名称修改

除了CASClientConnector的实现不同之外,其他步骤与上面相同

比如,客户的cas服务器返回的表单,execution,lt等信息名称不同,用户名不是标准的username等,那么可以参考CASClientConnector的实现自己实现与客户CAS相匹配的connector

例如:客户的cas服务器execution变成了execute_id,用户名变成了login_name:

代码块
languagejava
linenumberstrue
collapsetrue
package smartbi.cas.client;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import smartbi.SmartbiException;
import smartbi.sdk.CASClientConnector;
import smartbi.util.CommonErrorCode;
import smartbi.util.StringUtil;

public class MyCASClientConnector extends CASClientConnector{
	/**
	 * 
	 */
	private String lt;
	/**
	 * 
	 */
	private String execute_id;
	/**
	 * 
	 */
	private String eventId;
	
	public MyCASClientConnector(String smartbiUrl) {
		super(smartbiUrl);
	}	

	/** {@inheritDoc} */
	@Override
	public boolean open(String user, String password) {
		try {
			parseToken();
			loginCas(user, password);
			return loginSmartbi(user, password);

		} catch (IOException e) {
			throw new SmartbiException(CommonErrorCode.UNKOWN_ERROR, e);
		}
	}

	private boolean loginSmartbi(String user, String password) throws IOException {
		String url = smartbiUrl;
		if (!smartbiUrl.endsWith("/")) {
			url = url + "/";
		}
		HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
		conn.setInstanceFollowRedirects(false);
		conn.getResponseCode();

		String smartbiCookie = "";
		try {
			for (int i = 0; i < 100; i++) {
				if ("Set-Cookie".equalsIgnoreCase(conn.getHeaderFieldKey(i))) {
					String v = conn.getHeaderField(i);
					smartbiCookie += " " + v.substring(0, v.indexOf(';') + 1);
				}
			}
		} catch (ArrayIndexOutOfBoundsException e) {
		}
		String location = conn.getHeaderField("Location");
		conn.disconnect();
		if (!location.startsWith("http")) {
			cookie = smartbiCookie;
			return super.open(user, password);
		}

		conn = (HttpURLConnection) new URL(location).openConnection();
		conn.setRequestProperty("Cookie", cookie);
		conn.setInstanceFollowRedirects(false);
		conn.getResponseCode();

		location = conn.getHeaderField("Location");
		conn.disconnect();
		
		if (location == null) {
			return false;
		}

		conn = (HttpURLConnection) new URL(location).openConnection();
		conn.setInstanceFollowRedirects(false);
		conn.setRequestProperty("Cookie", smartbiCookie);
		conn.getResponseCode();
		conn.disconnect();
		cookie = smartbiCookie;

		return super.open(user, password);
	}


	private void loginCas(String user, String password) throws IOException, MalformedURLException,
			UnsupportedEncodingException {
		HttpURLConnection conn = (HttpURLConnection) new URL(getCasLoginUrl()).openConnection();
		conn.setRequestProperty("Cookie", cookie);

		conn.setDoInput(true);
		conn.setDoOutput(true);
		OutputStream connOut = null;
		try {
			connOut = conn.getOutputStream();
			String postData = "login_name=" + URLEncoder.encode(user, "UTF-8") + "&password="
					+ URLEncoder.encode(password, "UTF-8") + "&execute_id=" + URLEncoder.encode(execute_id, "UTF-8") + "<="
					+ URLEncoder.encode(lt, "UTF-8") + "&_eventId=" + URLEncoder.encode(eventId, "UTF-8");
			connOut.write(postData.getBytes());
		} finally {
			if (connOut != null) {
				connOut.flush();
				connOut.close();
			}
		}
		conn.getResponseCode();
		try {
			for (int i = 0; i < 100; i++) {
				if ("Set-Cookie".equalsIgnoreCase(conn.getHeaderFieldKey(i))) {
					String v = conn.getHeaderField(i);
					cookie += " " + v.substring(0, v.indexOf(';') + 1);
				}
			}
		} catch (ArrayIndexOutOfBoundsException e) {
		}
		conn.disconnect();
	}

	private void parseToken() throws IOException {
		URL url = new URL(getCasLoginUrl());
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		String html = StringUtil.readFromStream(conn.getInputStream(), "UTF-8");
		int index = html.indexOf("name=\"lt\" value=\"");
		int endIndex = -1;
		if (index > -1) {
			index += "name=\"lt\" value=\"".length();
			endIndex = html.indexOf('"', index);
			lt = html.substring(index, endIndex);
		} else {
			lt = "lt";
		}
		
		index = html.indexOf("name=\"execute_id\" value=\"");
		index += "name=\"execute_id\" value=\"".length();
		endIndex = html.indexOf('"', index);
		execute_id = html.substring(index, endIndex);

		index = html.indexOf("name=\"_eventId\" value=\"");
		index += "name=\"_eventId\" value=\"".length();
		endIndex = html.indexOf('"', index);
		eventId = html.substring(index, endIndex);

		for (int i = 0; i < 100; i++) {
			if ("Set-Cookie".equalsIgnoreCase(conn.getHeaderFieldKey(i))) {
				cookie = conn.getHeaderField(i);
				break;
			}
		}
		if (cookie != null) {
			cookie = cookie.substring(0, cookie.indexOf(';') + 1);
		} else {
			cookie = "";
		}
		conn.disconnect();
	}
}

其中,主要修改的位置:

execution改为execute_id

Image Added

提交表单的时候,username改为login_name

Image Added