新增SPI程式讓本部程式可傳送使用者guid或帳號名稱給縣市端程式查詢使用者資訊並回傳更新
函式庫更新 cnclibs0.0.7.9 EduDataType0.2.3.7
1.修改mongo數值,增加client_credentials syncdata
進入所屬mongo資料庫xxxstore,使用指令更新數值
目前只允許本部程式使用client_credentials,先查詢教育體系身分認證服務的objectid對應再更新mongo數值
db.cncspstore.update({"_id" : ObjectId("find_your_objectid")},{$addToSet:{"allowingoauthflows":"client_credentials","allowingscopes":"syncdata"}})
2. merge tc-idp程式,就得到本地登入程式(LocalLoginPage)及自訂SP程式(DummyCreateSPPage)
也可參考commit紀錄
這邊基本上不用再做修改
3.修改縣市xxauth程式,新增syncdata方法,可用身分證號或縣市帳號查詢使用者資料
3-1:新增一個syncdata package下有xxSyncData.java,在META-INF.service新增syncdata.spi.CNCSyncData檔案
xxSyncData.java內容:實作CNCSyncData方法
package syncdata; import authentication.DAOxx; import models.edumodel.ExchangeDataModel; import syncdata.spi.CNCSyncData; import tools.IUniqueIDType; import java.util.Map;
/**
* SPI Implementation
*/
public class xxSyncData implements CNCSyncData {
@Override
public List<ExchangeDataModel> doSyncData(Map<IUniqueIDType, String> uniqueIDTypeStringMap) {
DAOxx daoxx = new DAOxx();
return (List<ExchangeDataModel>) daoxx.doSyncData(uniqueIDTypeStringMap);
}
}
syncdata.spi.CNCSyncData內容:
syncdata.xxSyncData
3-2 修改xxauth,如果是DAO資料庫程式需修改DAOxx程式
@Override public List<ExchangeDataModel> doSyncData(Map<IUniqueIDType, String> uniqueIDTypeStringMap) { List<ExchangeDataModel> exchangeDataModelList = new ArrayList<>(); uniqueIDTypeStringMap.keySet().stream().findFirst().ifPresent((type) -> { switch (type) { case PERSONID: exchangeDataModelList.add(getPhcSyncData(uniqueIDTypeStringMap.get(type))); break; case ACCOUNT: exchangeDataModelList.add(getPhcSyncData(uniqueIDTypeStringMap.get(type))); break; } }); return exchangeDataModelList; } /** * get sync data * * @param uniqueid * @return */ private ExchangeDataModel getPhcSyncData(String uniqueid) { PreparedStatement preparedStatement = null; ResultSet resultSet = null; ExchangeDataModel model = new ExchangeDataModel(); ObjectMapper mapper2 = new ObjectMapper(); String sqlSyncSt = "Select * from xxxx where pid=? limit 1"; try { //呼叫內建method,會開啟連線 open(); preparedStatement = getConnection().prepareStatement(sqlSyncSt); preparedStatement.setString(1, uniqueid); logger.debug("PID : {}", uniqueid); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { model.setUsername(resultSet.getString("username")); model.setPassword(resultSet.getString("password")); model.setPasswordcheck(resultSet.getString("password")); model.setFullname(resultSet.getString("truename")); String schoolid = resultSet.getString("schoolid"); //以下省略....最後retun model給syncdata
可參考cnctcauth commit紀錄
如果有縣市師生分流的程式需參考tcauth做法,分別修改師生登入驗證程式
4. merge cncresource/tc_resource to your own xx_resource
重新編譯發布cncresource: CncResource-0.0.2.war
5. 測試
使用postman basic auth放入client_id and client_secret
https://xxx.sso.edu.tw/cncresource/api/v1/oauth2/token 是否取得token
再拿token 使用oauth2.0去取回資料
https://xxx.sso.edu.tw/cncresource/api/v1/oauth2/syncdata
使用curl -u client_id:client_secret -d "type&scope" uri 取得access token
curl -X POST-u "xxxcf789350df91xxxc914ee027ce55f3ab5":"xxx97d12e23a83748xxx042601cd28226422af5c607f4b" -d "grant_type=client_credentials&scope=syncdata" https://xxx.sso.edu.tw/cncresource/api/v1/oauth2/token
使用token拿資料 curl -X POST(指定使用post method) "json資料格式" -H "Authorization Bearer access token" -d '{"key":"value"}' uri
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer n7B111kN21MkYBd6LRYzDi-hCdtYuiBO19jWqlPI" -d '{"STPERSONID":"07f7df7a6bfbb67c3ea7f1612b02ff338e37ec18c8e1cc71421bc5b05d7d"}' https://xxx.sso.edu.tw/cncresource/api/v1/oauth2/syncdata