1、创建数据库连接类DBConn.java

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
package 
com.xju.ws;
import 
java.sql.*;
public 
class 
DBConn {
    
// 存储机器的名称
        
private 
String DBDRIVER;
        
// 存储数据库连接路径
        
private 
String DBURL;
        
// 数据库用户名称
        
private 
String DBUSER;
        
// 数据库密码
        
private 
String DBPASS;
        
/* 设置连接数据库相关参数 */
        
public DBConn(){ 
            
DBDRIVER = "oracle.jdbc.driver.OracleDriver";
            
DBURL = "jdbc:oracle:thin:@10.238.208.33:1521:XjMobileGIS";    
            
DBUSER = "用户名"; 
            
DBPASS = "密码";
        
}
                                                                                                                 
        
/* 获取Conncetion对象并返回 */
        
public  
Connection getConnection() {
            
Connection con=
null
;
            
try 
{
                    
Class.forName(DBDRIVER);     
// 加载Jdbc驱动程序
                    
con = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
            
}
            
catch 
(Exception e) {
                
e.printStackTrace();
            
}    
            
return 
con; 
        
}
}

2、创建业务实现类GetTokenImpl.java

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
package 
com.xju.ws;
import 
java.sql.Connection;
import 
java.sql.PreparedStatement;
import 
java.sql.ResultSet;
import 
java.sql.SQLException;
public 
class 
GetTokenImpl {
    
public 
String[] getResult(String usr, String pwd) {
        
String[] str = 
new 
String[
4
];
        
Connection connection = 
null
;
        
PreparedStatement ps = 
null
;
        
ResultSet rs = 
null
;
        
String sql = 
"select tb_user.selectmodel as selecttype,tb_user.domain as domain,"
                
"tb_authority.username  as username,tb_authority.upassword as upassword from tb_user,"
                
"tb_authority where tb_user.selectarea= tb_authority.statename and "
                
"tb_user.selectservice= tb_authority.servicename and tb_user.userid=? and tb_user.userpwd=?"
;
                                                                                                     
        
DBConn dbcon = 
new 
DBConn();
        
connection = dbcon.getConnection();
        
try 
{
            
ps = connection.prepareStatement(sql);
            
ps.setString(
1
, usr);
            
ps.setString(
2
, pwd);
            
rs = ps.executeQuery();
            
if 
(rs.next()) {
                
str[
0
] = rs.getString(
"selecttype"
);
                
str[
1
] = rs.getString(
"domain"
);
                
str[
2
] = rs.getString(
"username"
);
                
str[
3
] = rs.getString(
"upassword"
);
            
else 
{
                
str[
0
] = 
""
;
                
str[
1
] = 
""
;
                
str[
2
] = 
""
;
                
str[
3
] = 
""
;
            
}
        
catch 
(SQLException e) {
            
// TODO Auto-generated catch block
            
e.printStackTrace();
        
finally 
{
            
closeResultSet(rs);
            
closeStatement(ps);
            
closeConnection(connection);
        
}
        
return 
str;
    
}
    
public 
void 
closeStatement(PreparedStatement ps) {
        
if 
(ps != 
null
) {
            
try 
{
                
ps.close();
                
ps = 
null
;
            
catch 
(SQLException sqle) {
                
sqle.printStackTrace();
            
}
        
}
    
}
    
public 
void 
closeResultSet(ResultSet rs) {
        
if 
(rs != 
null
) {
            
try 
{
                
rs.close();
                
rs = 
null
;
            
catch 
(SQLException sqle) {
                
sqle.printStackTrace();
            
}
        
}
    
}
    
public 
void 
closeConnection(Connection connection) {
        
try 
{
            
if 
(connection != 
null 
&& !connection.isClosed()) {
                
connection.close();
                
connection = 
null
;
            
}
        
catch 
(SQLException e) {
            
// TODO Auto-generated catch block
            
e.printStackTrace();
        
}
    
}
}

3、创建Token获取类GetToken.java

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
package 
com.xju.ws;
import 
java.io.IOException;
import 
java.util.ArrayList;
import 
java.util.List;
import 
org.apache.http.HttpEntity;
import 
org.apache.http.HttpResponse;
import 
org.apache.http.NameValuePair;
import 
org.apache.http.client.ClientProtocolException;
import 
org.apache.http.client.HttpClient;
import 
org.apache.http.client.entity.UrlEncodedFormEntity;
import 
org.apache.http.client.methods.HttpPost;
import 
org.apache.http.impl.client.DefaultHttpClient;
import 
org.apache.http.message.BasicNameValuePair;
import 
org.apache.http.util.EntityUtils;
public 
class 
GetToken {
    
public 
String queryToken(String usr, String pwd) {
        
String[] str = 
new 
String[
4
];
        
// GetViewService test=new GetViewService();
        
GetTokenImpl tokenPara = 
new 
GetTokenImpl();
        
str = tokenPara.getResult(usr, pwd);
        
// str[0]服务类型:存储服务类型Client/Server
        
// str[1]服务网址:http://10.236.23.2/mapsercer
        
// str[2]后台用户名
        
// str[3]后台密码
        
// 存储令牌环
        
String agsToken = 
""
;
        
String referer_ip = 
""
;
        
// 创建一个http客户端
        
HttpClient client = 
new 
DefaultHttpClient();
        
// 创建一个POST请求
        
HttpPost request = 
new 
HttpPost(
                
"http://10.238.208.34/arcgis/admin/generateToken"
);
        
// 设置HTTP POST请求参数必须用NameValuePair
        
List<NameValuePair> params = 
new 
ArrayList<NameValuePair>();
        
if 
(str[
0
].equals(
"Server"
)) {
            
referer_ip = str[
1
];
            
params.add(
new 
BasicNameValuePair(
"client"
"referer"
));
            
params.add(
new 
BasicNameValuePair(
"referer"
, referer_ip));
            
params.add(
new 
BasicNameValuePair(
"expiration"
"1440"
));
        
else 
{
            
referer_ip = str[
1
];
            
params.add(
new 
BasicNameValuePair(
"client"
"ip"
));
            
params.add(
new 
BasicNameValuePair(
"ip"
, referer_ip));
            
params.add(
new 
BasicNameValuePair(
"expiration"
"480"
));
        
}
        
params.add(
new 
BasicNameValuePair(
"username"
, str[
2
]));
        
params.add(
new 
BasicNameValuePair(
"password"
, str[
3
]));
        
params.add(
new 
BasicNameValuePair(
"f"
"json"
));
        
// 设置http Post请求参数
        
try 
{
            
HttpEntity entity = 
new 
UrlEncodedFormEntity(params);
            
request.setEntity(entity);
            
HttpResponse response = client.execute(request);
            
if 
(response.getStatusLine().getStatusCode() == 
200
) {
// 如果状态码为200,就是正常返回
                
String result = EntityUtils.toString(response.getEntity());
                
result.replace(
"\n"
""
).replace(
"\r"
""
).replace(
"\t"
""
);
                
String[] splitInfos = result.split(
"\""
);
                
if 
(splitInfos != 
null 
&& splitInfos.length > 
2
) {
                    
if 
(splitInfos[
3
].length() > 
15
                            
&& splitInfos[
1
].trim().equals(
"token"
)) {
                        
agsToken = splitInfos[
3
].trim();
                    
}
                
}
            
}
        
catch 
(ClientProtocolException e) {
            
e.printStackTrace();
            
// 进行处理操作
        
catch 
(IOException e) {
            
// 进行处理操作
        
}
        
return 
agsToken;
    
}
}

备注:Client模式Token有效期为8小时,Server模式Token有效期为24小时。

4、由下向上模式基于GetToken.java类创建Web Server

调用地址:

(1)Java调用方式关键代码

1
2
3
GetTokenService service = 
new 
GetTokenService();
GetTokenDelegate delegate = service.getGetTokenPort();
String token=delegate.queryToken(
"username"
"123456"
);

(2)C#调用代码

1
2
3
--添加Web引用,Web引用名为:TokenService
TokenService.GetTokenService service = 
new 
TokenService.GetTokenService();
string token = service.queryToken(
"username"
"123456"
);