Java httpclient实现CAS单点登录

最近项目中遇到CAS授权的系统,客户端需要调用https接口获取数据,下面给出具体怎么通过CAS授权的代码示例。

public class SSLClient extends DefaultHttpClient {
	public SSLClient() throws Exception {
		super();
		SSLContext ctx = SSLContext.getInstance("TLS");
		X509TrustManager tm = new X509TrustManager() {
			@Override
			public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
			}

			@Override
			public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
			}

			@Override
			public X509Certificate[] getAcceptedIssuers() {
				return null;
			}
		};
		ctx.init(null, new TrustManager[] { tm }, null);
		SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		ClientConnectionManager ccm = this.getConnectionManager();
		SchemeRegistry sr = ccm.getSchemeRegistry();
		sr.register(new Scheme("https", 443, ssf));
	}

}
	private static String doCasLoginRequest(DefaultHttpClient httpclient, String url) throws IOException {
		try {
			String result = "";
			HttpGet httpget = new HttpGet(url);
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
			String tempLine = rd.readLine();
			String s = "<input type=\"hidden\" name=\"lt\" value=\"";
			while (tempLine != null) {
				int index = tempLine.indexOf(s);
				if (index != -1) {
					String s1 = tempLine.substring(index + s.length());
					int index1 = s1.indexOf("\"");
					if (index1 != -1)
						result = s1.substring(0, index1);
				}
				tempLine = rd.readLine();
			}
			if (entity != null) {
				entity.getContent().close();
			}
			return result;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static DefaultHttpClient casLogin(String url) throws Exception {
		DefaultHttpClient httpClient = new SSLClient();
		httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/5.0");
		HttpPost post = new HttpPost(url);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("username", "admin"));
		nvps.add(new BasicNameValuePair("password", "xxxx"));
		nvps.add(new BasicNameValuePair("lt", doCasLoginRequest(httpClient, url)));
		nvps.add(new BasicNameValuePair("execution", "e1s1"));
		nvps.add(new BasicNameValuePair("_eventId", "submit"));
		nvps.add(new BasicNameValuePair("submit", "登录"));
		post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
		HttpResponse response = httpClient.execute(post);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			httpClient.getCookieStore().getCookies().forEach(c -> {
				System.out.println(c.getName() + "=>" + c.getValue());
			});
			entity.getContent().close();
		}
		return httpClient;
	}

执行过程中可以发现,控制台如果能正常打印出cookie中的seesionID等授权信息,就说明通过cas认证了,这个时候,用这个

httpclient就可以进行各种post/get请求了。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Java实现SmartBI的单点登录LoginByToken可以按照以下步骤进行: 1. 首先,确保你已经获得了SmartBI系统的相关配置信息,包括登录URL、用户名和令牌。 2. 在Java应用程序中,你可以使用HttpURLConnection或者HttpClient等库来发送HTTP请求,并获取响应。 3. 构建一个GET请求的URL,包含以下参数: - SmartBI系统的登录页面URL - 用户名 - 令牌(token) 例如: ``` String loginUrl = "http://smartbi.example.com/login"; String username = "your_username"; String token = "your_token"; String url = loginUrl + "?username=" + username + "&token=" + token; ``` 4. 发送HTTP GET请求到该URL,并获取响应。 使用HttpURLConnection的示例代码: ```java URL urlObj = new URL(url); HttpURLConnection con = (HttpURLConnection) urlObj.openConnection(); con.setRequestMethod("GET"); int responseCode = con.getResponseCode(); // 处理响应结果 if (responseCode == HttpURLConnection.HTTP_OK) { // 登录成功 // 进行后续操作 } else { // 登录失败 // 处理错误情况 } con.disconnect(); ``` 使用HttpClient的示例代码: ```java HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); // 处理响应结果 if (statusCode == HttpStatus.SC_OK) { // 登录成功 // 进行后续操作 } else { // 登录失败 // 处理错误情况 } ``` 5. 根据响应的状态码和内容,判断登录是否成功。如果状态码为200,表示登录成功,可以进行后续操作;否则,登录失败,需要处理错误情况。 请注意,以上代码仅为示例,实际实现中可能需要根据具体情况进行适当调整。另外,还需要注意处理异常和安全性方面的考虑,例如SSL证书验证等。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值