@ApiOperation(value="受權用戶信息") @PostMapping(value="/authorization") public String authorization(@RequestParam Map<String,String> params){ StringBuffer sbf = new StringBuffer(16); try { String callbackUrl = params.get("callbackUrl"); //回調地址不爲空 if(StringUtils.isNotBlank(callbackUrl)){ //用戶信息 LoginInfo loginInfo = AuthManagerFactory.currentAuthManager().getLoginInfo(); if (loginInfo == null) { throw new RuntimeException("當前用戶爲空!"); } String encode = MD5.encode(String.valueOf(loginInfo.getUser()),""); URL url = new URL(callbackUrl); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setConnectTimeout(60000); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod(RequestMethod.POST.name()); connection.connect(); OutputStream outputStream = connection.getOutputStream(); outputStream.write(encode.getBytes()); outputStream.flush(); outputStream.close(); if (connection.getResponseCode()>=HttpStatus.OK.value() && connection.getResponseCode()<HttpStatus.MULTIPLE_CHOICES.value()) { InputStream inputStream = connection.getInputStream(); byte []bt = new byte[1024]; int i = 0; while ((i=inputStream.read(bt)) != -1){ sbf.append(new String(bt, 0, i)); } inputStream.close(); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if(StringUtils.isNotBlank(sbf.toString())){ JSONObject json = JSONObject.parseObject(sbf.toString()); String redirectUrl1 = json.getString("redirectUrl"); //若是成功 String success = "SUCCESS",status = "status"; if(success.equals(json.getString(status))){ return String.format("redirect:%s",redirectUrl1); } } return "redirect:/grant/error.html"; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用戶受權</title> </head> <link rel="stylesheet" href="/web/webjars/bootstrap/3.3.7/css/bootstrap.css"/> <script type="application/javascript" src="/web/webjars/jquery/3.2.1/jquery.js"></script> <script type="application/javascript" src="/web/webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script type="application/javascript" > function loadPage() { var eles = document.getElementsByTagName("form"); for (var i = 0; i < eles.length; i++) { eles[i].action='/web/authorization?callbackUrl='+getUrlParameter("callbackUrl"); } if (document.cookie) { var strs = document.cookie.split(";"); for (var i = 0; i < strs.length; i++) { var ar = strs[i].split("="); if (ar[0] && ar[0].trim() == 'userId' && ar[1]) { $("#authorization").show(); return $("#login").hide(); } } } $("#authorization").hide(); $("#login").show(); } //獲取URL參數信息 function getUrlParameter(name){ if(location.search==''){ return ''; } var o={}; var search=location.search.replace(/\?/,'');//只替換第一個問號,若是參數中帶有問號,看成普通文本 var s=search.split('&'); for(var i=0;i<s.length;i++){ o[s[i].split('=')[0]]=s[i].split('=')[1]; } return o[name]==undefined?'':o[name]; } </script> <body onload="loadPage();"> <div class="flex-auto jumbotron " style="height:900px;" > <div class="row" style="position:relative;top:200px;" > <div id="login" class="col-lg-offset-4 panel panel-default align-content-center" style="width: 400px;"> <div class="panel-heading"> <h3 class="panel-title"> SHB登陸 </h3> </div> <div class="panel-body"> <form class="form-horizontal" method="post" action=""> <div class="form-group"> <label for="userName" class="col-sm-4 control-label">用戶名</label> <div class="col-sm-8"> <input type="userName" class="form-control" id="userName" placeholder="請輸入用戶名"> </div> </div> <div class="form-group"> <label for="password" class="col-sm-4 control-label">用戶密碼</label> <div class="col-sm-8"> <input type="password" class="form-control" id="password" placeholder="請輸入用戶密碼"> </div> </div> <div class="form-group text-center"> 使用SBH帳號訪問 https://gitee.com ,並容許網站進行以下操做: <br/> <input type="checkbox" checked="checked" id="checkbox_user_info"><label for="checkbox_user_info">訪問用戶信息</label> </div> <div class="form-group"> <div class="col-sm-offset-5 col-sm-7"> <button type="button" class="btn btn-default">登陸</button> </div> </div> </form> </div> </div> <div id="authorization" class="col-lg-offset-4 panel panel-default align-content-center" style="width: 400px;display: none;"> <div class="panel-heading"> <h3 class="panel-title"> SHB登陸 </h3> </div> <div class="panel-body"> <form class="form-horizontal" method="post" action="/web/authorization"> <div class="form-group text-center"> 使用SBH帳號訪問 https://gitee.com ,並容許網站進行以下操做: <br/> <input type="hidden" value=""> <input type="checkbox" checked="checked" id="authorization_user_info"> <label for="checkbox_user_info">訪問用戶信息</label> </div> <div class="form-group"> <div class="col-sm-offset-3 col-sm-9"> <button type="submit" class="btn btn-default" >受權</button> <button type="button" class="btn btn-default">取消</button> </div> </div> </form> </div> </div> </div> </div> </body> </html>