Home > Back-end >  Authentication real-name swagger
Authentication real-name swagger

Time:11-05

 
Package com. Yoyo. Authentication. The controller;

The import cn. Hutool. Core. Util. IdcardUtil;
The import com. Yoyo. Authentication. Service. TbUserAuthinfoService;
The import IO. Swagger. Annotations. Api;
The import IO. Swagger. Annotations. ApiOperation;
The import IO. Swagger. Annotations. ApiParam;
The import org. Springframework. Beans. Factory. The annotation. Autowired;
The import org. Springframework. Web. Bind. The annotation. PostMapping;
The import org. Springframework. Web. Bind. The annotation. RequestParam;
The import org. Springframework. Web. Bind. The annotation. RestController;
@ Api (tags="identity authentication related interface")
@ RestController
Public class AuthenticationController {
The @autowired
TbUserAuthinfoService TbUserAuthinfoService;
@ ApiOperation (" authentication information ")
@ PostMapping (value="https://bbs.csdn.net/topics/verifyIdentityInfo")
Public Object verifyIdentityInfo (@ RequestParam @ ApiParam (name="idCard," value="https://bbs.csdn.net/topics/id number", the required=true) String idCard,
@ RequestParam @ ApiParam (name="realName," value="real name" https://bbs.csdn.net/topics/, the required=true) String realName) throws the Exception {
if (! IdcardUtil. IsValidCard18 (idCard)) {
Throw new Exception (" id card format is not correct ");
}
Return tbUserAuthinfoService. VerifyIdentityInfo (idCard, realName);
}



Package com. Yoyo. Authentication. Service. Impl;

The import com. Alibaba. Fastjson. JSONObject;
The import com. Yoyo. Authentication. Service. TbUserAuthinfoService;
Import org. Springframework. Beans. Factory. The annotation, the Value;
import org.springframework.stereotype.Service;

Import the Java. IO. BufferedReader;
Import the Java. IO. InputStreamReader;
The import java.net.HttpURLConnection;
The import java.net.URL;

@ Service
Public class TbUserAuthinfoServiceImpl implements TbUserAuthinfoService {

@ Value (" ${RRK. Auth. Url} ")
Private String authurl;

@ Value (" ${RRK. Auth. Secret} ")
Private String authsecret;
@ Override
Public Object verifyIdentityInfo (String idCard, String realName) {
String returnStr=addAutoAuthentication (idCard, realName);
//the certification result
//{" error_code ": 0," "reason" : "approved by", "result" : {" realName ":" ", "cardNo" : ""," details ": {" addrCode" : ""," birth ":" ", "sex" : 0, "length", and "checkBit" : ""," addr ":" ", "province" : ""," city ":" ", "area" : ""}," isok ": 1}," ordersign ":" "}
//{" error_code ": 0," "reason" : "without the id card number", "result" : {" realName ":" test ", "cardNo" : "422202199511112325", "isok" : - 2}, "ordersign" : "20190826114737073025748529"}
JSONObject JSONObject=JSONObject. ParseObject (returnStr);
Object the Object=jsonObject. Get (" result ");
The Object reason=jsonObject. Get (" reason ");
If (((int) ((JSONObject) object). The get (" isok ")==1) | | (int) ((JSONObject) object). The get (" isok ")==2) {
Return "name and id number does not match";
} else if ((int) ((JSONObject) object). The get (" isok ")==1) {
The return reason;
} else {
Return "name and id number does not match";
}
}

//add automatic real-name certification
Private String addAutoAuthentication (String idCard, String realName) {
//structure need to request the content of the
StringBuffer sb=new StringBuffer();
Sb. Append (" cardNo="+ idCard);
Sb. Append (" & amp; RealName="+ realName);
//perform authentication request
Return requestGet (authurl +/idcard, sb. The toString (), authsecret);
}



/* *
* perform authentication request
*
* @ param strUrl
* @ param param
* @ param appcode
* @ return
*/
Public static String requestGet (String strUrl, String param, String appcode) {
String returnStr=null;//returns the definition
URL url=null;
HttpURLConnection HttpURLConnection=null;
Try {
Url=new url (strUrl + "?" + param);
HttpURLConnection=(httpURLConnection) url. OpenConnection ();
HttpURLConnection. SetRequestProperty (" Accept - Charset ", "utf-8");
HttpURLConnection. SetRequestProperty (" the content-type ", "application/x - WWW - form - urlencoded");
HttpURLConnection. SetRequestProperty (" Authorization ", "APPCODE" + APPCODE);
HttpURLConnection. SetDoOutput (true);
HttpURLConnection. SetDoInput (true);
HttpURLConnection. SetRequestMethod (" GET ");//get
HttpURLConnection. SetUseCaches (false);//no cache
HttpURLConnection. The connect ();
BufferedReader reader=new BufferedReader (
New InputStreamReader (httpURLConnection. GetInputStream (), "utf-8"));
StringBuffer buffer=new StringBuffer ();
String line="";
While ((line=reader. ReadLine ())!=null) {
Buffer. Append (line);
}
Reader. The close ();
ReturnStr=buffer. The toString ();
} the catch (Exception e) {
e.printStackTrace();

return null;
} the finally {
If (httpURLConnection!=null) {
HttpURLConnection. Disconnect ();
}
}
Return returnStr;
}
}

Package com. Yoyo. Authentication;

The import com. Spring4all. Swagger. EnableSwagger2Doc;
The import org. Springframework. Boot. SpringApplication;
The import org. Springframework. Boot. Autoconfigure. SpringBootApplication; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related