利用Web Service技术实现Token动态获取

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


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

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

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

调用地址:http://10.238.208.34:80/GetTokenService/GetTokenPort?WSDL

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

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

(2)C#调用代码

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







本文出自 “IT技术学习与交流” 博客,谢绝转载!

利用Web Service技术实现Token动态获取,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。