@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>