一.代碼生成(此步驟爲代碼生成器的使用,如不清楚請查閱相關文檔視頻) 1.進入菜單【在線開發】-->【Online表單開發】,選中一張單表/主表,點擊代碼生成按鈕。 2.彈出頁面中填寫代碼生成目錄爲項目根目錄,填寫包名,勾選"是否支持Restful"爲"是",默認爲"否」,以下圖 二.swagger-ui 使用,在線調試接口 1.訪問http://localhost:8080/jeecg/swagger/index.html [此地址根據本身環境而定] 2.訪問【tokenAPI : 鑑權token接口】-->【POST /rest/tokens】,按下圖操做 3.在響應體中獲取token 4.調用接口-建立 5.查看 接口-建立測試結果 6.調用接口-分頁查詢 7.查看 接口-分頁查詢 測試結果 8.其餘接口相似,先填寫token,再填寫相關參數便可測試 三.接口安全,token原理講解和redis配置 1.機制說明 基於token的鑑權機制相似於http協議也是無狀態的,它不須要在服務端去保留用戶的認證信息或者會話信息。這就意味着基於token認證機制的應用不須要去考慮用戶在哪一臺服務器登陸了,這就爲應用的擴展提供了便利 2.基本流程 (1) 用戶使用用戶名密碼來請求服務器 (2) 服務器進行驗證用戶的信息 (3) 服務器經過驗證發送給用戶一個token (4) 客戶端存儲token,並在每次請求時附送上這個token值(存在head裏的參數X-AUTH-TOKEN) (5) 服務端驗證token值,並返回數據 3. redis配置(redis環境搭建參考相關文檔/視頻) JWT 驗證token採用redis進行緩存,redis配置文件:src/main/resources/redis.properties, 修改redis對應的IP和端口,以下: #redis redis.host=124.206.91.99 redis.port=6379 redis.pass= redis.adapter.maxIdle=100 redis.adapter.minIdle=10 redis.adapter.testOnBorrow=true redis.adapter.testOnReturn=true redis.adapter.testWhileIdle=true redis.adapter.numTestsPerEvictionRun=10 redis.adapter.timeBetweenEvictionRunsMillis=60000 四. 接口本地單元測試(單元測試環境搭建請參考相關文檔/視頻) import org.jeecgframework.jwt.util.JwtHttpUtil; import org.junit.Test; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.generator.test.entity.TestSingleEntity; public class RestfulTestSingle{ public String getToken(String userName,String password){ String url = "http://localhost:8080/jeecg/rest/tokens?username="+userName+"&password="+password; String token= JwtHttpUtil.httpRequest(url, "POST", null); System.out.println("獲取的token爲:"+token); return token; } public JSONObject getList(String token){ String url = "http://localhost:8080/jeecg/rest/testSingleController/list/1/10"; JSONObject resp= JwtHttpUtil.httpRequest(url, "GET", null,token); System.out.println(resp.toJSONString()); return resp; } public JSONObject delete(String token,String id){ String url = "http://localhost:8080/jeecg/rest/testSingleController/"+id; JSONObject resp= JwtHttpUtil.httpRequest(url, "DELETE", null,token); System.out.println(resp.toJSONString()); return resp; } public JSONObject create(String token,String json){ String url = "http://localhost:8080/jeecg/rest/testSingleController"; JSONObject resp= JwtHttpUtil.httpRequest(url, "POST", json,token); System.out.println(resp.toJSONString()); return resp; } public JSONObject update(String token,String json,String id){ String url = "http://localhost:8080/jeecg/rest/testSingleController/"+id; JSONObject resp= JwtHttpUtil.httpRequest(url, "PUT", json,token); System.out.println(resp.toJSONString()); return resp; } public JSONObject get(String token,String id){ String url = "http://localhost:8080/jeecg/rest/testSingleController/"+id; JSONObject resp= JwtHttpUtil.httpRequest(url, "GET", null,token); System.out.println(resp.toJSONString()); return resp; } @Test public void test(){ String token = "";//getToken調用一次便可將其返回的值保存下來,以便其餘接口可調用傳參 //getToken("admin", "123456"); //獲取列表 //getList(token); //刪除 //delete(token, "4028f6816588914f016588b24a8c0003"); //建立 /*TestSingleEntity entity = new TestSingleEntity(); entity.setName("李四"); create(token, JSON.toJSON(entity).toString());*/ //修改 /*String id = "4028f6816588f200016588f6e2950001"; TestSingleEntity entity = new TestSingleEntity(); entity.setId(id); entity.setName("李四4號"); update(token, JSONObject.toJSONString(entity),id);*/ //獲取單條記錄 /*String id = "4028f6816588f200016588f6e2950001"; get(token, id);*/ } } 五. 前段UI開發如何調用接口 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@include file="/context/mytags.jsp"%> <t:base type="jquery,easyui,tools,DatePicker"></t:base> <div class="easyui-layout" fit="true"> <div region="center" style="padding:0px;border:0px"> <t:datagrid name="testSingleList" checkbox="true" pagination="true" fitColumns="true" title="單表測試" actionUrl="testSingleController.do?datagrid" idField="id" sortName="createDate" fit="true" queryMode="group"> <t:dgCol title="主鍵" field="id" hidden="true" queryMode="single" width="120"></t:dgCol> <t:dgCol title="建立日期" field="createDate" formatter="yyyy-MM-dd" queryMode="single" width="120"></t:dgCol> <t:dgCol title="名臣" field="name" query="true" queryMode="single" width="120"></t:dgCol> <t:dgCol title="下拉" field="iselect" query="true" queryMode="single" dictionary="t_s_depart,id,departname" width="120"></t:dgCol> <t:dgCol title="單選" field="icheck" queryMode="single" dictionary="fieltype" width="120"></t:dgCol> <t:dgCol title="多選" field="iradio" queryMode="single" dictionary="s_type" width="120"></t:dgCol> <t:dgCol title="日期" field="idate" formatter="yyyy-MM-dd" query="true" queryMode="group" width="120"></t:dgCol> <t:dgCol title="文件" field="ifile" queryMode="single" formatterjs="btListFileFormatter" width="120"></t:dgCol> <t:dgCol title="輸入框" field="iterr" queryMode="single" image="true" imageSize="50,50" formatterjs="btListImgFormatter" width="120"></t:dgCol> <t:dgCol title="時間時分秒" field="idatetime" formatter="yyyy-MM-dd hh:mm:ss" queryMode="single" width="120"></t:dgCol> <t:dgCol title="操做" field="opt" width="100"></t:dgCol> <t:dgDelOpt title="刪除" url="testSingleController.do?doDel&id={id}" urlclass="ace_button" urlfont="fa-trash-o"/> <t:dgToolBar title="調用接口" icon="icon-redo" funname="testInterface"></t:dgToolBar> </t:datagrid> </div> </div> <script type="text/javascript"> //調用接口,先獲取token,後調用接口 function testInterface(){ var userName = "admin",password = "123456"; $.ajax({ url:"http://localhost:8080/jeecg/rest/tokens?username="+userName+"&password="+password, type:"POST", success:function(token){ //query(token); //creat(token); } }); } //不須要傳參數 function query(token){ $.ajax({ url:"http://localhost:8080/jeecg/rest/testSingleController/list/1/10", type:"GET", dataType:"JSON", beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("X-AUTH-TOKEN", token);//設置token }, success:function(data){ console.log(data); } }); } //須要傳參數 function creat(token){ var obj = { name:"張二", idate:"2018-08-29" }; $.ajax({ url:"http://localhost:8080/jeecg/rest/testSingleController", type:"POST", dataType:"JSON", contentType: "application/json;charset=utf-8", data :JSON.stringify(obj), beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("X-AUTH-TOKEN", token);//設置token }, success:function(data){ console.log(data); } }); } </script>
1.進入菜單【在線開發】-->【Online表單開發】,選中一張單表/主表,點擊代碼生成按鈕。javascript
2.彈出頁面中填寫代碼生成目錄爲項目根目錄,填寫包名,勾選"是否支持Restful"爲"是",默認爲"否」,以下圖html
1.訪問http://localhost:8080/jeecg/swagger/index.html [此地址根據本身環境而定]java
2.訪問【tokenAPI : 鑑權token接口】-->【POST /rest/tokens】,按下圖操做jquery
3.在響應體中獲取tokenajax
4.調用接口-建立redis
5.查看 接口-建立測試結果json
6.調用接口-分頁查詢緩存
7.查看 接口-分頁查詢 測試結果安全
8.其餘接口相似,先填寫token,再填寫相關參數便可測試bash
1.機制說明
基於token的鑑權機制相似於http協議也是無狀態的,它不須要在服務端去保留用戶的認證信息或者會話信息。這就意味着基於token認證機制的應用不須要去考慮用戶在哪一臺服務器登陸了,這就爲應用的擴展提供了便利
2.基本流程
(1) 用戶使用用戶名密碼來請求服務器
(2) 服務器進行驗證用戶的信息
(3) 服務器經過驗證發送給用戶一個token
(4) 客戶端存儲token,並在每次請求時附送上這個token值(存在head裏的參數X-AUTH-TOKEN)
(5) 服務端驗證token值,並返回數據
3. redis配置(redis環境搭建參考相關文檔/視頻)
JWT 驗證token採用redis進行緩存,redis配置文件:src/main/resources/redis.properties, 修改redis對應的IP和端口,以下:
#redis
redis.host=124.206.91.99
redis.port=6379
redis.pass=
redis.adapter.maxIdle=100
redis.adapter.minIdle=10
redis.adapter.testOnBorrow=true
redis.adapter.testOnReturn=true
redis.adapter.testWhileIdle=true
redis.adapter.numTestsPerEvictionRun=10
redis.adapter.timeBetweenEvictionRunsMillis=60000
import org.jeecgframework.jwt.util.JwtHttpUtil;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.generator.test.entity.TestSingleEntity;
public class RestfulTestSingle{
public String getToken(String userName,String password){
String url = "http://localhost:8080/jeecg/rest/tokens?username="+userName+"&password="+password;
String token= JwtHttpUtil.httpRequest(url, "POST", null);
System.out.println("獲取的token爲:"+token);
return token;
}
public JSONObject getList(String token){
String url = "http://localhost:8080/jeecg/rest/testSingleController/list/1/10";
JSONObject resp= JwtHttpUtil.httpRequest(url, "GET", null,token);
System.out.println(resp.toJSONString());
return resp;
}
public JSONObject delete(String token,String id){
String url = "http://localhost:8080/jeecg/rest/testSingleController/"+id;
JSONObject resp= JwtHttpUtil.httpRequest(url, "DELETE", null,token);
System.out.println(resp.toJSONString());
return resp;
}
public JSONObject create(String token,String json){
String url = "http://localhost:8080/jeecg/rest/testSingleController";
JSONObject resp= JwtHttpUtil.httpRequest(url, "POST", json,token);
System.out.println(resp.toJSONString());
return resp;
}
public JSONObject update(String token,String json,String id){
String url = "http://localhost:8080/jeecg/rest/testSingleController/"+id;
JSONObject resp= JwtHttpUtil.httpRequest(url, "PUT", json,token);
System.out.println(resp.toJSONString());
return resp;
}
public JSONObject get(String token,String id){
String url = "http://localhost:8080/jeecg/rest/testSingleController/"+id;
JSONObject resp= JwtHttpUtil.httpRequest(url, "GET", null,token);
System.out.println(resp.toJSONString());
return resp;
}
@Test
public void test(){
String token = "";//getToken調用一次便可將其返回的值保存下來,以便其餘接口可調用傳參
//getToken("admin", "123456");
//獲取列表
//getList(token);
//刪除
//delete(token, "4028f6816588914f016588b24a8c0003");
//建立
/*TestSingleEntity entity = new TestSingleEntity(); entity.setName("李四"); create(token, JSON.toJSON(entity).toString());*/
//修改
/*String id = "4028f6816588f200016588f6e2950001"; TestSingleEntity entity = new TestSingleEntity(); entity.setId(id); entity.setName("李四4號"); update(token, JSONObject.toJSONString(entity),id);*/
//獲取單條記錄
/*String id = "4028f6816588f200016588f6e2950001"; get(token, id);*/
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<t:base type="jquery,easyui,tools,DatePicker"></t:base>
<div class="easyui-layout" fit="true">
<div region="center" style="padding:0px;border:0px">
<t:datagrid name="testSingleList" checkbox="true" pagination="true" fitColumns="true" title="單表測試" actionUrl="testSingleController.do?datagrid" idField="id" sortName="createDate" fit="true" queryMode="group">
<t:dgCol title="主鍵" field="id" hidden="true" queryMode="single" width="120"></t:dgCol>
<t:dgCol title="建立日期" field="createDate" formatter="yyyy-MM-dd" queryMode="single" width="120"></t:dgCol>
<t:dgCol title="名臣" field="name" query="true" queryMode="single" width="120"></t:dgCol>
<t:dgCol title="下拉" field="iselect" query="true" queryMode="single" dictionary="t_s_depart,id,departname" width="120"></t:dgCol>
<t:dgCol title="單選" field="icheck" queryMode="single" dictionary="fieltype" width="120"></t:dgCol>
<t:dgCol title="多選" field="iradio" queryMode="single" dictionary="s_type" width="120"></t:dgCol>
<t:dgCol title="日期" field="idate" formatter="yyyy-MM-dd" query="true" queryMode="group" width="120"></t:dgCol>
<t:dgCol title="文件" field="ifile" queryMode="single" formatterjs="btListFileFormatter" width="120"></t:dgCol>
<t:dgCol title="輸入框" field="iterr" queryMode="single" image="true" imageSize="50,50" formatterjs="btListImgFormatter" width="120"></t:dgCol>
<t:dgCol title="時間時分秒" field="idatetime" formatter="yyyy-MM-dd hh:mm:ss" queryMode="single" width="120"></t:dgCol>
<t:dgCol title="操做" field="opt" width="100"></t:dgCol>
<t:dgDelOpt title="刪除" url="testSingleController.do?doDel&id={id}" urlclass="ace_button" urlfont="fa-trash-o"/>
<t:dgToolBar title="調用接口" icon="icon-redo" funname="testInterface"></t:dgToolBar>
</t:datagrid>
</div>
</div>
<script type="text/javascript"> //調用接口,先獲取token,後調用接口 function testInterface(){ var userName = "admin",password = "123456"; $.ajax({ url:"http://localhost:8080/jeecg/rest/tokens?username="+userName+"&password="+password, type:"POST", success:function(token){ //query(token); //creat(token); } }); } //不須要傳參數 function query(token){ $.ajax({ url:"http://localhost:8080/jeecg/rest/testSingleController/list/1/10", type:"GET", dataType:"JSON", beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("X-AUTH-TOKEN", token);//設置token }, success:function(data){ console.log(data); } }); } //須要傳參數 function creat(token){ var obj = { name:"張二", idate:"2018-08-29" }; $.ajax({ url:"http://localhost:8080/jeecg/rest/testSingleController", type:"POST", dataType:"JSON", contentType: "application/json;charset=utf-8", data :JSON.stringify(obj), beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("X-AUTH-TOKEN", token);//設置token }, success:function(data){ console.log(data); } }); } </script>