含义:用户直接向客户端注册,客户端以自己的名义要求"服务提供商"提供服务,其实不存在授权问题
步骤:
(A)客户端向认证服务器进行身份认证,并要求一个访问令牌(token)。
(B)认证服务器确认无误后,向客户端提供访问令牌。
(C)用令牌请求资源服务器的资源
摘要:
1,JAVA代码中访问
2,Postman中访问
3,Swagger中访问
正文:
1,JAVA代码中访问
我的环境是Spring Boot,实现代码:
pom.xml
<dependency> <groupId>org.apache.oltu.oauth2</groupId> <artifactId>org.apache.oltu.oauth2.client</artifactId> <version>0.31</version> </dependency>
OAuthClientUtil.java
public class OAuthClientUtil { private static Logger logger = LoggerFactory.getLogger(OAuthClientUtil.class.getName()); //Config.ACCESS_TOKEN_URL 认证服务器获取token的地址;Config.OAUTH_USERNAME 账号;Config.OAUTH_PASSWORD:密码 public static String getApiToken()throws OAuthProblemException{ logger.info("api getApiToken"); String accessToken = null; OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); try { OAuthClientRequest request = OAuthClientRequest .tokenLocation(Config.ACCESS_TOKEN_URL) .setGrantType(GrantType.CLIENT_CREDENTIALS) .setUsername(Config.OAUTH_USERNAME) .setPassword(Config.OAUTH_PASSWORD) .buildQueryMessage(); request.addHeader("Accept", "application/json"); request.addHeader("Content-Type", "application/json"); String auth = Base64Util.encode(Config.OAUTH_USERNAME + ':' + Config.OAUTH_PASSWORD); request.addHeader("Authorization", "Basic " + auth); OAuthAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request, OAuth.HttpMethod.POST); //去服务端请求access_token,并返回响应 accessToken = oAuthResponse.getAccessToken(); //获取服务端返回过来的access_token logger.info("api token: " + accessToken); } catch (OAuthSystemException e) { e.printStackTrace(); } return accessToken; } }
Base64Util.java
public class Base64Util { //编码 public static String encode(String s) { if (s == null) return null; String res = ""; try { res = new sun.misc.BASE64Encoder().encode(s.getBytes("GBK")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return res; } //解码 public static String decode(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b,"GBK"); } catch (Exception e) { return null; } } }
使用授权访问接口
public String scanTrainQrode(String param) { String respContent = ""; //返回的内容 String apiToken = null; try { apiToken = OAuthClientUtil.getApiToken(); //获取token String requestUrl = String.format(Config.URL, param, apiToken); //拼接接口地址(例:https://cn.bing.com?param=param&api_token=apiToken) String result = HttpHelper.SendPOST(requestUrl); //访问接口地址 if (StringUtils.isNotBlank(result)) { //解析返回的结果 JSONObject jsonObject = JSONObject.fromObject(result); if(jsonObject.containsKey("msg")) respContent = jsonObject.getString("msg"); } } catch (OAuthProblemException e) { e.printStackTrace(); } return respContent; }
2,Postman中访问
Postman为模拟网络请求的工具,见参考博客2
-------------------------------------------
3,Swagger中访问
value中填写bearer + 空格 + token(例:bearer 49305daf-e566-4f9d-8f83-e74af5ae47e1)
参考博客:
1,Java的oauth2.0 服务端与客户端的实现 - jing12062011的博客 - CSDN博客
https://blog.csdn.net/jing12062011/article/details/78147306
2,Oauth2授权模式访问之客户端模式(client_credentials)访问 - 面朝大海,春暖花开 - CSDN博客