http://www.2cto.com/kf/201401/270088.html
周六的时候去参观了36小时编程比赛现场, 气氛很是激烈, 里面有一个团队要做了一个移动应用,需要接入公司内域账号登录,可是最终页没有接入。联想到我做的就是cas啊,这方面我比较熟悉啊, 随着使用域账号登录公司内账号的场景增多,为什么不写一个demo呢。
说干就干, 周六晚上回到住处就开始了我的“36小时”编程。
首先,思考一下整体思路,其实cas的整体流程还算比较简单,我只需要将在浏览器中的整个流程在android上面实现即可,于是一顿狂写,主要如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
package
com.glodon.cas.model;
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.io.OutputStream;
import
java.net.HttpURLConnection;
import
java.net.URL;
import
java.net.URLEncoder;
import
org.apache.http.HttpEntity;
import
org.apache.http.HttpResponse;
import
org.apache.http.client.HttpClient;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.client.methods.HttpPost;
import
org.apache.http.impl.client.DefaultHttpClient;
public
class
GlodonCas {
private
static
DefaultHttpClient httpClient =
new
DefaultHttpClient();
private
static
boolean
casLogined =
false
;
public
final
static
String CAS_DOMAIN =
"http://cas.grandsoft.com.cn"
;
public
String appCallback =
null
;
private
boolean
isLogined =
false
;
public
GlodonCas(String appCallback) {
this
.appCallback = appCallback;
}
public
boolean
login(String username, String password)
throws
Exception {
String service = appCallback;
String lt = getLt();
String loginApi = CAS_DOMAIN +
"/login"
;
String params =
"username="
+ URLEncoder.encode(username)
+
"&password="
+ URLEncoder.encode(password) +
"<="
+ URLEncoder.encode(lt) +
"&service="
+ service;
byte
[] entitydata = params.getBytes();
HttpURLConnection c = (HttpURLConnection)
new
URL(loginApi)
.openConnection();
c.setInstanceFollowRedirects(
false
);
c.setRequestMethod(
"POST"
);
c.setDoOutput(
true
);
c.setRequestProperty(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
c.setRequestProperty(
"Content-Length"
,
String.valueOf(entitydata.length));
OutputStream outStream = c.getOutputStream();
outStream.write(entitydata);
outStream.flush();
outStream.close();
int
responeCode = -
1
;
try
{
responeCode = c.getResponseCode();
}
catch
(java.io.IOException e) {
if
(e.getMessage().contains(
"authentication challenge"
)) {
responeCode =
401
;
}
else
{
throw
e;
}
}
if
(responeCode ==
303
) {
get(c.getHeaderField(
"Location"
));
isLogined =
true
;
casLogined =
true
;
return
true
;
}
else
{
isLogined =
false
;
casLogined =
true
;
return
false
;
}
}
public
boolean
isLogined() {
return
isLogined;
}
public
static
boolean
casLogined() {
return
casLogined;
}
public
static
DefaultHttpClient getHttpClient() {
return
httpClient;
}
private
static
String getLt()
throws
IOException {
String httpUrl = CAS_DOMAIN +
"/loginTicket"
;
HttpPost httpPost =
new
HttpPost(httpUrl);
HttpResponse httpRespone = httpClient.execute(httpPost);
HttpEntity httpEntity = httpRespone.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(
inputStream));
String str =
""
;
String line =
null
;
while
((line = reader.readLine()) !=
null
) {
str += line;
}
return
str;
}
private
static
String get(String httpUrl)
throws
Exception {
HttpGet httpGet =
new
HttpGet(httpUrl);
InputStream inputStream =
null
;
String bodyStr =
""
;
HttpResponse httpRespone = httpClient.execute(httpGet);
HttpEntity httpEntity = httpRespone.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(
inputStream));
String line =
""
;
while
((line = reader.readLine()) !=
null
) {
bodyStr += line;
}
return
bodyStr;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
package
com.glodon.cas.activities;
import
java.io.BufferedReader;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.util.List;
import
org.apache.http.HttpEntity;
import
org.apache.http.HttpResponse;
import
org.apache.http.client.HttpClient;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.cookie.Cookie;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.AsyncTask;
import
android.os.Bundle;
import
android.view.View;
import
android.webkit.CookieManager;
import
android.webkit.CookieSyncManager;
import
android.webkit.WebView;
import
android.webkit.WebViewClient;
import
android.widget.Button;
import
android.widget.TextView;
import
android.widget.Toast;
import
com.glodon.cas.model.GlodonCas;
public
class
MainActivity
extends
Activity {
private
Button btnGetThinkList;
private
static
int
LOGIN_REQUEST_CODE =
1
;
private
TextView tvThinkList;
private
WebView webView;
private
Button btnRefresh;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGetThinkList = (Button) findViewById(R.id.btnGetThinkList);
tvThinkList = (TextView) findViewById(R.id.tvThinkList);
webView = (WebView) findViewById(R.id.webView);
btnGetThinkList.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
if
(!GlodonCas.casLogined()) {
startActivityForResult(
new
Intent(MainActivity.
this
,
LoginActivity.
class
), LOGIN_REQUEST_CODE);
return
;
}
new
ThinkListTask().execute();
}
});
webView.getSettings().setJavaScriptEnabled(
true
);
webView.setWebViewClient(
new
WebViewClient() {
public
boolean
shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return
false
;
}
});
webView.loadUrl(
"http://think.grandsoft.com.cn"
);
btnRefresh = (Button) findViewById(R.id.btnRefresh);
btnRefresh.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
List<cookie> cookies = GlodonCas.getHttpClient()
.getCookieStore().getCookies();
if
(!cookies.isEmpty()) {
Cookie sessionCookie =
null
;
for
(
int
i =
0
; i < cookies.size(); i++) {
sessionCookie = cookies.get(i);
}
CookieSyncManager.createInstance(MainActivity.
this
);
CookieManager cookieManager = CookieManager.getInstance();
if
(sessionCookie !=
null
) {
cookieManager.removeSessionCookie();
String cookieString = sessionCookie.getName() +
"="
+ sessionCookie.getValue() +
"; domain="
+ sessionCookie.getDomain();
cookieManager.setCookie(
"think.grandsoft.com.cn"
,
cookieString);
CookieSyncManager.getInstance().sync();
}
System.out.println(
"finish"
);
webView.loadUrl(
"http://think.grandsoft.com.cn"
);
}
}
});
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
if
(requestCode == LOGIN_REQUEST_CODE) {
Toast.makeText(
this
, R.string.login_success, Toast.LENGTH_LONG)
.show();
}
}
class
ThinkListTask
extends
AsyncTask<
void
,
void
,=
""
string=
""
> {
@Override
protected
String doInBackground(Void... params) {
try
{
HttpGet httpGet =
new
HttpGet(
"http://think.grandsoft.com.cn/api/weekly_journals"
);
HttpClient httpClient = GlodonCas.getHttpClient();
HttpResponse httpRespone = httpClient.execute(httpGet);
StringBuilder sb =
new
StringBuilder();
HttpEntity httpEntity = httpRespone.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(inputStream));
String line =
""
;
while
((line = reader.readLine()) !=
null
) {
sb.append(line);
}
return
sb.toString();
}
catch
(Exception e) {
e.printStackTrace();
return
null
;
}
}
@Override
protected
void
onPostExecute(String thinkList) {
tvThinkList.setText(thinkList);
}
}
}</
void
,></cookie>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package
com.glodon.cas.activities;
import
java.io.BufferedReader;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
org.apache.http.HttpEntity;
import
org.apache.http.HttpResponse;
import
org.apache.http.client.HttpClient;
import
org.apache.http.client.methods.HttpGet;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.AsyncTask;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
import
com.glodon.cas.model.GlodonCas;
public
class
LoginActivity
extends
Activity {
private
static
Button btnLogin;
private
static
EditText etUsername;
private
static
EditText etPassword;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin = (Button) findViewById(R.id.btnLogin);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
btnLogin.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
new
LoginTask().execute(etUsername.getText().toString(),
etPassword.getText().toString());
}
});
}
public
class
LoginTask
extends
AsyncTask<string,
void
,=
""
boolean
=
""
> {
@Override
protected
Boolean doInBackground(String... args) {
try
{
return
new
GlodonCas(
"http://think.grandsoft.com.cn"
)
.login(args[
0
], args[
1
]);
}
catch
(Exception e) {
e.printStackTrace();
}
return
null
;
}
@Override
protected
void
onPostExecute(Boolean login_success) {
if
(login_success ==
null
) {
Toast.makeText(LoginActivity.
this
, R.string.network_error_tip,
Toast.LENGTH_LONG).show();
return
;
}
if
(login_success) {
Intent intent =
new
Intent();
LoginActivity.
this
.setResult(RESULT_OK, intent);
LoginActivity.
this
.finish();
}
else
{
Toast.makeText(LoginActivity.
this
, R.string.login_error_tip,
Toast.LENGTH_LONG).show();
}
}
}
}</string,>
|