For more nbcio-boot functions, please see the demo system
gitee source code address
Backend code: https://gitee.com/nbacheng/nbcio-boot
Front-end code: https://gitee.com/nbacheng/nbcio-vue.git
Online demo (including H5): http://122.227.135.243:9888
1. After gitee is authorized to log in, the user can directly bind the corresponding mobile phone number to log in directly.
For example, the following example
/** * After gitee third-party binding, the token is returned directly through the admin user * * @param jsonObject * @return */ @ApiOperation("gitee login interface") @PostMapping("/bindingGitee") @ResponseBody public Result<String> bindingGitee(@RequestBody JSONObject jsonObject) { Result<String> result = new Result<String>(); //Fixed use of admin’s mobile phone number to log in SysUser sysUser = sysUserService.getUserByPhone("13988888888"); String token = saveToken(sysUser); result.setSuccess(true); result.setResult(token); return result; }
2. Create a general gitee operation class
import java.io.IOException; import java.net.URISyntaxException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; @Component public class GiteeHttpClient { /** * Get Access Token *post */ public static JSONObject getAccessToken(String url) throws IOException { HttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); // httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); HttpResponse response = client.execute(httpPost); HttpEntity entity = response.getEntity(); if (null != entity) { String result = EntityUtils.toString(entity, "UTF-8"); return JSONObject.parseObject(result); } httpPost.releaseConnection(); return null; } /** * Get user information *get */ public static JSONObject getUserInfo1(String url) throws IOException { JSONObject jsonObject = null; CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); HttpResponse response = client.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = JSONObject.parseObject(result); } httpGet.releaseConnection(); return jsonObject; } // https://gitee.com/api/v5/user /** * Obtain authorized user information * Obtain user information using gitee get method */ public static JSONObject getUserInfo(String token,String url) throws IOException, URISyntaxException { JSONObject jsonObject = null; CloseableHttpClient client = HttpClients.createDefault(); URIBuilder builder = new URIBuilder(url); builder.setParameter("access_token", token); HttpGet httpGet = new HttpGet(builder.build()); httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); HttpResponse response = client.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = JSONObject.parseObject(result); } httpGet.releaseConnection(); return jsonObject; } public static JSONArray getArray(String token,String url) throws IOException, URISyntaxException { JSONArray jsonArray = null; CloseableHttpClient client = HttpClients.createDefault(); URIBuilder builder = new URIBuilder(url); builder.setParameter("access_token", token); HttpGet httpGet = new HttpGet(builder.build()); httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); HttpResponse response = client.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); jsonArray = JSONObject.parseArray(result); } httpGet.releaseConnection(); return jsonArray; } public static JSONObject put(String token,String url,JSONObject json) throws IOException, URISyntaxException { JSONObject jsonObject = null; CloseableHttpClient client = HttpClients.createDefault(); URIBuilder builder = new URIBuilder(url); builder.setParameter("access_token", token); json.keySet().forEach((k)->{ builder.setParameter(k,json.getString(k)); }); HttpPut httpPut = new HttpPut(builder.build()); httpPut.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); HttpResponse response = client.execute(httpPut); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = JSONObject.parseObject(result); } httpPut.releaseConnection(); return jsonObject; } public static JSONObject post(String token,String url,JSONObject json) throws IOException, URISyntaxException { JSONObject jsonObject = null; CloseableHttpClient client = HttpClients.createDefault(); URIBuilder builder = new URIBuilder(url); builder.setParameter("access_token", token); json.keySet().forEach((k)->{ builder.setParameter(k,json.getString(k)); }); HttpPost httpPost = new HttpPost(builder.build()); httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); HttpResponse response = client.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = JSONObject.parseObject(result); } httpPost.releaseConnection(); return jsonObject; } }
3. You can also consider creating a third-party gitee service similar to the following, such as
/** * Third-party (GITEE) login service */ @Service public class ThirdLoginService { @Autowired public SysUserService userService; @Value("${vlife.gitee.client_id}") public String client_id; @Value("${vlife.gitee.redirect_uri}") public String redirect_uri; @Value("${vlife.gitee.client_secret}") public String client_secret; @Value("${vlife.email.from}") public String emailFrom; @Value("${vlife.email.pwd}") public String emailPwd; public Boolean openCheckCode(){ if(StringUtils.isNotEmpty(emailFrom) & amp; & amp;StringUtils.isNotEmpty(emailPwd)) { return true; }else{ return false; } } /** * Obtain third-party account user token * @param code */ public String thirdToken(String code,String from){ String gitToken=null; try { JSONObject jsonObject= GiteeHttpClient.getAccessToken("https://gitee.com/oauth/token?" + "grant_type=authorization_code & amp;code=" + code + " & amp;client_id=" + client_id + " & amp;redirect_uri=" + redirect_uri + " & amp;client_secret=" + client_secret); gitToken=jsonObject.getString("access_token"); }catch (Exception exception){ exception.printStackTrace(); } return gitToken; } /** * Obtain third-party user information */ public ThirdAccountDto account(String thirdToken, String from){ ThirdAccountDto account=null; try { JSONObject userObj=GiteeHttpClient.getUserInfo(thirdToken,"https://gitee.com/api/v5/user"); account=new ThirdAccountDto(); account.setId(userObj.getString("id")); account.setName(userObj.getString("name")); account.setEmail(userObj.getString("email")); account.setUsername(userObj.getString("login")); account.setThirdToken(thirdToken); account.setAvatar(userObj.getString("avatar_url")); account.setFrom(from); }catch (Exception exception){ exception.printStackTrace(); } return account; } /** * Obtain third-party user information */ public String email(String token, String from){ String email=null; try { JSONObject userObj=GiteeHttpClient.getArray(token,"https://gitee.com/api/v5/emails").getJSONObject(0); email= userObj.getString("email"); }catch (Exception exception){ exception.printStackTrace(); } return email; } public SysUser createUser(ThirdAccountDto accountDto){ SysUser user=new SysUser(); // user.setSysOrgId(sysOrgId); user.setSysGroupId("super"); user.setSysDeptId("4028b8818747df52018747dfdf780000"); user.setState("1"); // user.setUsername(accountDto.getUsername()); user.setThirdId(accountDto.getId()); user.setName(accountDto.getName()); user.setEmail(accountDto.getEmail()); user.setSource(accountDto.getFrom()); user.setThirdToken(accountDto.getThirdToken()); user.setAvatar(accountDto.getAvatar()); return user; } /* Send verification information email */ public String sendMail(String to,String code) { if( emailFrom==null|| emailPwd==null){ return "The email service information server is not set up and cannot send emails"; } Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.163.com"); props.put("mail.smtp.host", "smtp.163.com"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props); session.setDebug(true); MimeMessage message = new MimeMessage(session); try { message.setSentDate(new Date()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day HH:mm:ss"); message.setFrom(new InternetAddress(emailFrom)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Load recipient address message.setSubject("Low-code platform verification code","utf-8"); Multipart multipart = new MimeMultipart(); // Add various parts of the email to the multipart object, including text content and attachments BodyPart contentPart = new MimeBodyPart(); // Set the text content of the email String str = "<!DOCTYPE html><html><head><meta charset='UTF-8'></head><body><p style='font-size: 20px;font-weight:bold;' >Dear user, hello!</p>" + "<p style='text-indent:2em; font-size: 20px;'>Welcome to use the vlife low-code platform, your registration code this time is " + "<span style='font-size:30px;font-weight:bold;color:red'>" + code + "</span>, valid within 10 minutes, please use it as soon as possible!</p>" + "<p style='text-align:right; padding-right: 20px;'" + "<a href='http://www.vlife.cc' style='font-size: 18px'>For more information, please visit vlife User Guide</a></p>" + "<span style='font-size: 18px; float:right; margin-right: 60px;'>" + sdf.format(new Date()) + "</span></body></html> "; contentPart.setContent(str, "text/html;charset=utf-8"); multipart.addBodyPart(contentPart); message.setContent(multipart); message.saveChanges(); // Save changes Transport transport = session.getTransport("smtp"); // Connect to the server's mailbox transport.connect("smtp.163.com", emailFrom, emailPwd); // Send the email transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (MessagingException e) { e.printStackTrace(); return "Failed to send:" + e.getMessage(); } return null; } }