首页 文章 精选 留言 我的

精选列表

搜索[实名实人认证解决方案],共10000篇文章
优秀的个人博客,低调大师

Android HTTP2 + Oauth2 + Jwt 接口认证实例

本文节选自《Netkiller Java 手札》 Netkiller Java手札 Mr.NeoChan,陈景峯(BG7NYT) 中国广东省深圳市望海路半岛城邦三期 518067 +8613113668890<netkiller@msn.com> $Id: book.xml 606 2013-05-29 09:52:58Z netkiller $ 版权 © 2015-2018 Neo Chan 版权声明 转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。 http://www.netkiller.cn http://netkiller.github.io http://netkiller.sourceforge.net 我的系列文档 编程语言 Netkiller Architect 手札 Netkiller Developer 手札 Netkiller Java 手札 Netkiller Spring 手札 Netkiller PHP 手札 Netkiller Python 手札 Netkiller Testing 手札 Netkiller Cryptography 手札 Netkiller Perl 手札 Netkiller Docbook 手札 Netkiller Project 手札 Netkiller Database 手札 34.10.Android Oauth2 + Jwt example Oauth 返回数据,通过 Gson 的 fromJson 存储到下面类中。 package cn.netkiller.okhttp.pojo; public class Oauth { private String access_token; private String token_type; private String refresh_token; private String expires_in; private String scope; private String jti; public String getAccess_token() { return access_token; } public void setAccess_token(String access_token) { this.access_token = access_token; } public String getToken_type() { return token_type; } public void setToken_type(String token_type) { this.token_type = token_type; } public String getRefresh_token() { return refresh_token; } public void setRefresh_token(String refresh_token) { this.refresh_token = refresh_token; } public String getExpires_in() { return expires_in; } public void setExpires_in(String expires_in) { this.expires_in = expires_in; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } public String getJti() { return jti; } public void setJti(String jti) { this.jti = jti; } @Override public String toString() { return "Oauth{" + "access_token='" + access_token + '\'' + ", token_type='" + token_type + '\'' + ", refresh_token='" + refresh_token + '\'' + ", expires_in='" + expires_in + '\'' + ", scope='" + scope + '\'' + ", jti='" + jti + '\'' + '}'; } } Activity 文件 package cn.netkiller.okhttp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import com.google.gson.Gson; import java.io.IOException; import cn.netkiller.okhttp.pojo.Oauth; import okhttp3.Authenticator; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Credentials; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.Route; public class Oauth2jwtActivity extends AppCompatActivity { private TextView token; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_oauth2jwt); token = (TextView) findViewById(R.id.token); try { get(); } catch (IOException e) { e.printStackTrace(); } } public static Oauth accessToken() throws IOException { OkHttpClient client = new OkHttpClient.Builder().authenticator( new Authenticator() { public Request authenticate(Route route, Response response) { String credential = Credentials.basic("api", "secret"); return response.request().newBuilder().header("Authorization", credential).build(); } } ).build(); String url = "https://api.netkiller.cn/oauth/token"; RequestBody formBody = new FormBody.Builder() .add("grant_type", "password") .add("username", "blockchain") .add("password", "123456") .build(); Request request = new Request.Builder() .url(url) .post(formBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) { throw new IOException("服务器端错误: " + response); } Gson gson = new Gson(); Oauth oauth = gson.fromJson(response.body().string(), Oauth.class); Log.i("oauth", oauth.toString()); return oauth; } public void get() throws IOException { OkHttpClient client = new OkHttpClient.Builder().authenticator( new Authenticator() { public Request authenticate(Route route, Response response) throws IOException { return response.request().newBuilder().header("Authorization", "Bearer " + accessToken().getAccess_token()).build(); } } ).build(); String url = "https://api.netkiller.cn/misc/compatibility"; Request request = new Request.Builder() .url(url) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { call.cancel(); } @Override public void onResponse(Call call, Response response) throws IOException { final String myResponse = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { Log.i("oauth", myResponse); token.setText(myResponse); } }); } }); } public void post() throws IOException { OkHttpClient client = new OkHttpClient.Builder().authenticator( new Authenticator() { public Request authenticate(Route route, Response response) throws IOException { return response.request().newBuilder().header("Authorization", "Bearer " + accessToken().getAccess_token()).build(); } } ).build(); String url = "https://api.netkiller.cn/history/create"; String json = "{\n" + " \"assetsId\": \"5bced71c432c001c6ea31924\",\n" + " \"title\": \"添加信息\",\n" + " \"message\": \"信息内容\",\n" + " \"status\": \"录入\"\n" + "}"; final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { call.cancel(); } @Override public void onResponse(Call call, Response response) throws IOException { final String myResponse = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { // Gson gson = new Gson(); // Oauth oauth = gson.fromJson(myResponse, Oauth.class); // Log.i("oauth", oauth.toString()); token.setText(myResponse); } }); } }); } } 上面的例子演示了,怎样获取 access token 以及 HTTP 基本操作 GET 和 POST,再Restful接口中还可能会用到 PUT, DELETE 等等,原来相同,这里就不在演示。 34.11.HTTP/2 首先确认你的服务器是支持 HTTP2,HTTP2配置方法可以参考 《Netkiller Web 手札》 下面是我的例子仅供参考,Nginx 开启 http2 代理后面的 Spring Cloud 接口。 server { listen 80; listen 443 ssl http2; server_name api.netkiller.cn; ssl_certificate ssl/netkiller.cn.crt; ssl_certificate_key ssl/netkiller.cn.key; ssl_session_cache shared:SSL:20m; ssl_session_timeout 60m; charset utf-8; access_log /var/log/nginx/api.netkiller.cn.access.log; error_page 497 https://$host$uri?$args; if ($scheme = http) { return 301 https://$server_name$request_uri; } location / { proxy_pass http://127.0.0.1:8000; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } 安卓程序如下 String url = "https://api.netkiller.cn/member/json"; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d("okhttp", "Connect timeout. " + e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { String text = response.body().string(); Log.d("okhttp", "HTTP Code " + response.code() + " TEXT " + text); Log.d("okhttp", "Protocol: " + response.protocol()); } }); 运行结果 D/okhttp: HTTP Code 200 TEXT {"status":false,"reason":"","code":0,"data":{"id":null,"username":"12322228888","mobile":"12322228888","password":"123456","wechat":"微信ID","role":"Organization","captcha":null,"createDate":"2018-10-25 09:25:23","updateDate":null}} Protocol: h2 输出 h2 表示当前接口与服务器连接是通过 http2 完成。 Oauth2 + Jwt 的 SpringBoot 实现方法可以参考作者的《Netkiller Spring Cloud 手札》

优秀的个人博客,低调大师

反欺诈、聊天机器人、算法机器人:人工智能在银行业

随着人工智能 (AI) 的出现,银行业被公认为是正在经历重大变革的主要行业之一。凭借雄厚的财力,该行业有资源将大笔资金投入到机器学习的各个领域,从股票交易到客户服务,再到移动银行。 消除恐惧但在向消费者传达信息方面确实存在一个问题。据英国的一项调查,当人工智能涉及到钱时,多达 80% 的人表示不信任。考虑到有关自主计算机的危言耸听,这或许是可以理解的,更不用提关于AI的基本功能有很多神秘之处。然而,正如该领域的专家经常强调的那样,危险被大大夸大了。 实现飞跃:人工智能在银行业人工智能在银行业的潜力是巨大的。根据 Business Insider 的一份报告,到 2020 年,它可能帮助节省多达 4430 亿美元的成本。 反欺诈的应用程序这份报告还估计,通过 “人工智能了解你的客户” (AI Know Your Customer) 程序,

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册