九游会官网登录入口网页-ag8九游会j9登录入口提供多种sso单点认证方式,比如约定密钥,oauth2,smap等等。本文主要演示如何通过登录用户名,和sso相关的配置,使用单点认证的方式进行o2server的登录认证,获取xtoken信息。
此示例演示如何通过登录用户名,和sso相关的配置,使用单点认证的方式进行o2server的登录认证,获取xtoken信息
注意:涉及到加密解密,请使用非中文的唯一标识进行登录 ,中文登录 有可能会有找不到用户的问题。
添加一个sso配置
部分代码内容,loginwithsso.java
package net.o2oa.demos; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.url; import java.net.urlconnection; import java.util.date; import org.apache.commons.codec.binary.stringutils; import org.json.jsonobject; import net.o2oa.util.crypto; /** * 此示例演示如何通过登录用户名,和sso相关的配置,使用单点认证的方式进行o2server的登录认证,获取xtoken信息 * 涉及到加密解密,请使用非中文的唯一标识进行登录 ,中文登录 有可能会有找不到用户的问题。 * @author 九游会官网登录入口网页-ag8九游会j9登录入口 */ public class demo_loginwithsso { static final string url_ssologin="/x_organization_assemble_authentication/jaxrs/sso"; public static void main( string[] args ) { string applicationserver = "127.0.0.1"; integer applicationport = 20020; string username = "13533441287"; string ssoclient = "sso_demo"; string key = "sso123456"; try { loginresult result = login(applicationserver, applicationport, username, ssoclient, key); if( stringutils.equals( "success", result.gettype() )) { system.out.println("xtoken=" result.gettoken() ); }else { system.out.println("message:" result.getmessage() ); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } //服务地址:http://127.0.0.1:20020/x_organization_assemble_authentication/jaxrs/sso //{"token":"xadmin","client":"sso_demo"} /** * 使用登录认证的接口进行服务器登录,获取xtoken信息 * @param applicationserver 127.0.0.1 * @param applicationport 20020 * @param username 张三 * @param client sso_demo * @param key sso123456 * @return * @throws exception */ public static loginresult login( string applicationserver, integer applicationport, string username, string client, string key ) throws exception { //参数 string loginurl = "http://" applicationserver ":" applicationport url_ssologin ; string xtoken = crypto.encrypt( username "#" new date().gettime(), key ); string loginparams = string.format("{'token':'%s','client':'%s'}", xtoken, client ); string responsedata = sendpost( loginurl, loginparams ); ...... } }
成功响应结果
{ "type": "success", "data": { "token": "xb9xtojiqja5avrfhfibnmfvhydvflgaipzbzbiuf7anhelrq4vou9ygprwek2e1ysxape_z4f1mvqvstfqi5cw7pk31ulrouvaer5juybq", "rolelist": [], "id": "1cb47e12-18ad-4363-a55f-4514edb76215", "gendertype": "m", "signature": "", "pinyin": "lisi", "pinyininitial": "ls", "description": "", "name": "李四", "employee": "", "unique": "c93b7fb8-6820-466c-ab9c-f0637b8a3682", "distinguishedname": "李四@c93b7fb8-6820-466c-ab9c-f0637b8a3682@p", "ordernumber": 56649305, "controllerlist": [], "superior": "", "changepasswordtime": "2019-10-18", "mail": "", "weixin": "", "qq": "", "mobile": "13533441287", "officephone": "", "createtime": "2019-10-18 15:55:05", "updatetime": "2019-10-18 15:55:05" }, "message": "", "date": "2019-10-19 15:12:10", "spent": 141, "size": -1, "count": 0, "position": 0 }
失败响应结果
{ "readystate": 4, "responsetext": "{ "type": "error", "message": "用户不存在或者密码错误.", "date": "2019-10-19 14:34:34", "spent": 9, "size": -1, "count": 0, "position": 0, "prompt": "com.x.organization.assemble.authentication.jaxrs.authentication.exceptionpersonnotexistorinvalidpassword" }", "responsejson": { "type": "error", "message": "用户不存在或者密码错误.", "date": "2019-10-19 14:34:34", "spent": 9, "size": -1, "count": 0, "position": 0, "prompt": "com.x.organization.assemble.authentication.jaxrs.authentication.exceptionpersonnotexistorinvalidpassword" }, "status": 500, "statustext": "internal server error" }
获取响应的x-token信息
jsonobject result = new jsonobject(responsedata); string type = result.getstring("type"); if( stringutils.equals( "success", type )) { //登录成功 jsonobject data = result.getjsonobject("data"); string token = data.getstring( "token" ); return new loginresult("success", token, "登录成功!"); }else { //登录失败 return new loginresult("error", null, "用户登录失败!"); }
sendpost代码:
/** * 发送post请求 * @param url 地址 * @param param 传入的数据 * @return */ public static string sendpost( string url, string param ) { printwriter out = null; bufferedreader in = null; string result = ""; try { url realurl = new ; urlconnection conn = realurl.openconnection(); conn.setrequestproperty("accept", "*/*"); conn.setrequestproperty("connection", "keep-alive"); conn.setrequestproperty("content-type", "application/json; charset=utf-8"); conn.setrequestproperty("user-agent", "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)"); conn.setdooutput(true); conn.setdoinput(true); out = new printwriter(conn.getoutputstream()); out.print(param); out.flush(); in = new bufferedreader( new inputstreamreader(conn.getinputstream())); string line; while ((line = in.readline()) != null) { result = line; } } catch (exception e) { e.printstacktrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (ioexception ex) { ex.printstacktrace(); } } return result; } public static class loginresult{ private string type; private string token; private string message; public loginresult(string type, string token, string message) { super(); this.type = type; this.token = token; this.message = message; } public string gettype() { return type; } public void settype(string type) { this.type = type; } public string gettoken() { return token; } public void settoken(string token) { this.token = token; } public string getmessage() { return message; } public void setmessage(string message) { this.message = message; } } }
数据加密辅助类,crypto.java
package net.o2oa.util; import java.io.ioexception; import java.net.urldecoder; import java.net.urlencoder; import java.security.securerandom; import javax.crypto.cipher; import javax.crypto.secretkey; import javax.crypto.secretkeyfactory; import javax.crypto.spec.deskeyspec; import org.apache.commons.codec.binary.base64; import org.apache.commons.lang3.stringutils; /** * 数据加密辅助类 */ public class crypto { private static final string utf8 = "utf-8"; private final static string des = "des"; private final static string cipher_init = "des"; public static string encrypt(string data, string key) throws exception { byte[] bt = encrypt(data.getbytes(), key.getbytes()); string str = base64.encodebase64urlsafestring(bt); return urlencoder.encode( str, utf8 ); } public static byte[] encrypt(byte[] data, byte[] key) throws exception { // 生成一个可信任的随机数源 securerandom sr = new securerandom(); // 从原始密钥数据创建deskeyspec对象 deskeyspec dks = new deskeyspec(key); // 创建一个密钥工厂,然后用它把deskeyspec转换成secretkey对象 secretkeyfactory keyfactory = secretkeyfactory.getinstance(des); secretkey securekey = keyfactory.generatesecret(dks); // cipher对象实际完成加密操作 cipher cipher = cipher.getinstance(cipher_init); // 用密钥初始化cipher对象 cipher.init(cipher.encrypt_mode, securekey, sr); return cipher.dofinal(data); } public static string decrypt(string data, string key) throws ioexception, exception { if (stringutils.isempty(data)) { return null; } string str = urldecoder.decode(data, utf8); byte[] buf = base64.decodebase64(str); byte[] bt = decrypt(buf, key.getbytes()); return new string(bt); } public static byte[] decrypt(byte[] data, byte[] key) throws exception { // 生成一个可信任的随机数源 securerandom sr = new securerandom(); // 从原始密钥数据创建deskeyspec对象 deskeyspec dks = new deskeyspec(key); // 创建一个密钥工厂,然后用它把deskeyspec转换成secretkey对象 secretkeyfactory keyfactory = secretkeyfactory.getinstance(des); secretkey securekey = keyfactory.generatesecret(dks); // cipher对象实际完成解密操作 cipher cipher = cipher.getinstance(cipher_init); // 用密钥初始化cipher对象 cipher.init(cipher.decrypt_mode, securekey, sr); return cipher.dofinal(data); } }
pom.xml内容:
4.0.0 net.o2oa.demos test_o2oa_java_demo 0.0.1-snapshot jar test_start_process_demo http://maven.apache.org utf-8 org.apache.httpcomponents httpclient 4.5.10 org.apache.httpcomponents httpcore-nio 4.4.12 org.apache.httpcomponents httpcore 4.4.12 org.apache.httpcomponents httpmime 4.5.10 org.apache.commons commons-lang3 3.9 com.google.code.gson gson 2.8.5 org.json json 20190722 junit junit 3.8.1 test