直接上代码(oauthUrl在配置文件中写好http://ip:端口):
/**
* 调用授权中心销毁 Token
* @param token
* @return
*/
public boolean logoutToken(String token) {
try {
HttpResponse response = Request.Get(MessageFormat.format("{0}/oauth/logout", this.oauthUrl))
.addHeader(“Authorization”, token)
.execute()
.returnResponse();
if (response.getStatusLine().getStatusCode() == 200) {
return true;
} else {
return false;
}
} catch (IOException e) {
return false;
}
}
/**
* 调用授权中心获取 Token
* @param username
* @param password
* @return
*/
public String accessToken(String username, String password) {
try {
JSONObject result = JSON.parseObject(Request.Get(MessageFormat.format("{0}/oauth/token?username={1}&password={2}&grant_type=password&scope=all&client_id=client1&client_secret=908090909090e", this.oauthUrl, username, password))
.execute().returnContent().asString());
return result.getString(“access_token”);
} catch (IOException e) {
throw new UnauthorizedException();
}
}