WeaverHttpAuthenticationHandler.java
5.08 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at the following location:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package weaver.loginsso;
import org.jasig.cas.authentication.handler.AuthenticationHandler;
import org.jasig.cas.authentication.principal.Credentials;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
import org.jasig.cas.util.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import javax.validation.constraints.NotNull;
/**
* Class to validate the credentials presented by communicating with the web
* server and checking the certificate that is returned against the hostname,
* etc.
* <p>
* This class is concerned with ensuring that the protocol is HTTPS and that a
* response is returned. The SSL handshake that occurs automatically by opening
* a connection does the heavy process of authenticating.
*
* @author Scott Battaglia
* @version $Revision$ $Date$
* @since 3.0
*/
public final class WeaverHttpAuthenticationHandler implements
AuthenticationHandler {
/** The string representing the HTTPS protocol. */
private static final String PROTOCOL_HTTPS = "https";
/** Boolean variable denoting whether secure connection is required or not. */
private boolean requireSecure = true;
/** Log instance. */
private final Logger log = LoggerFactory.getLogger(getClass());
private String ts;
private String appid;
private String loginid;
public String getLoginid() {
return loginid;
}
public void setLoginid(String loginid) {
this.loginid = loginid;
}
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getTs() {
return ts;
}
public void setTs(String ts) {
this.ts = ts;
}
/** Instance of Apache Commons HttpClient */
@NotNull
private HttpClient httpClient;
public boolean authenticate(final Credentials credentials) {
final UsernamePasswordCredentials serviceCredentials = (UsernamePasswordCredentials) credentials;
final String username = serviceCredentials.getUsername();
String password = serviceCredentials.getPassword();
String url = serviceCredentials.getPassword();
String ts="";
try {
String[] info = AES.decrypt(password, "yjcust").split("-f-g-");
url = info[1];
ts= info[0];
setTs(ts);
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(username + " " + password + " " + url);
String re = senddata(url, username, ts);
if (re.indexOf("true_") >= 0) {
String realLoginid = re.replace("true_", "");
setLoginid(realLoginid);
return true;
}else{
setLoginid("");
return false;
}
}
public String senddata(String url, String username, String ts) {
log.info("url:" + url + " " + "username:" + username + ",ts:"
+ ts);
PostMethod postMethod = new PostMethod(url);
String response = "";
try {
NameValuePair[] params = { new NameValuePair("username", username),
new NameValuePair("ts", ts)
};
postMethod.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=utf-8");
postMethod.setRequestBody(params);
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == 200) {
response = postMethod.getResponseBodyAsString();
log.info("response:" + response);
} else {
log.info("response-->http resp code:" + statusCode);
}
} catch (Exception e) {
log.info("", e);
e.printStackTrace();
}
return response;
}
/**
* @return true if the credentials provided are not null and the credentials
* are a subclass of (or equal to) HttpBasedServiceCredentials.
*/
public boolean supports(final Credentials credentials) {
return true;
}
/** Sets the HttpClient which will do all of the connection stuff. */
public void setHttpClient(final HttpClient httpClient) {
this.httpClient = httpClient;
}
/**
* Set whether a secure url is required or not.
*
* @param requireSecure
* true if its required, false if not. Default is true.
*/
public void setRequireSecure(final boolean requireSecure) {
this.requireSecure = requireSecure;
}
}