导航:首页 > 操作系统 > androidtoken机制

androidtoken机制

发布时间:2022-08-07 20:10:55

Ⅰ 为什么 APP 要用 token 而不用 session 认证

android也有,http client有CookieStore接口,但重启应用就没了,如果需要高级一点,需要对SharePrefrense或者数据库之类的存储进行一下存储操作,当然,只需要实现接口,set进去即可。简单的很。
UrlConection也有类似的cookie。。说正经的,token机制是为了防止cookie被清除,另外cookie是会在所有域名请求都携带上,无意中增加了服务端的请求量,token只需要在有必要的时候携带。token的中文名字就是令牌。。。。不是个多么高大上的东西

Ⅱ android 客户端为什么每次调用api都会产生一个新的token

这个token可能是标记是否被更改的标记吧,你可以debug看看是否哪里写错了。

Ⅲ android 中的token有什么用

BadTokenException经常出现在这样的错误中, "Unable to add window — token null is not for an application” 。一般出现这样的错误语句中

Dialog dialog = new Dialog(getApplicationContext());

或者

Dialog dialog = new Dialog(getApplication());

getApplicationContext在有些场合可以直接用来获取上下文,但是对于Dialog及其相关类型,这样使用就会报错,也就是token null。token其实就是和window对应的令牌。
正确的使用方法是

AlertDialog.Builder builder = new AlertDialog.Builder(this);

Ⅳ android 登录为什么要使用token

身份验证,安全机制

Ⅳ android token 超时,异步回调怎么实现会比较优雅

PhoneGap的js回调有几种实现方式。其中一种是ajax。
我们先来看一下js端相关代码:

// file: lib/android/plugin/android/callback.js
define("cordova/plugin/android/callback", function(require, exports, mole) {

var port = null,
token = null,
xmlhttp;

function startXhr() {
// cordova/exec depends on this mole, so we can't require cordova/exec on the mole level.
var exec = require('cordova/exec'),
xmlhttp = new XMLHttpRequest();

// Callback function when XMLHttpRequest is ready
xmlhttp.onreadystatechange=function(){
if (!xmlhttp) {
return;
}
if (xmlhttp.readyState === 4){
// If callback has javaScript statement to execute
if (xmlhttp.status === 200) {

// Need to url decode the response
var msg = decodeURIComponent(xmlhttp.responseText);
setTimeout(startXhr, 1);
exec.processMessages(msg);
}

// If callback ping (used to keep XHR request from timing out)
else if (xmlhttp.status === 404) {
setTimeout(startXhr, 10);
}

// 0 == Page is unloading.
// 400 == Bad request.
// 403 == invalid token.
// 503 == server stopped.
else {
console.log("JSCallback Error: Request failed with status " + xmlhttp.status);
exec.setNativeToJsBridgeMode(exec.nativeToJsModes.POLLING);
}
}
};

if (port === null) {
port = prompt("getPort", "gap_callbackServer:");
}
if (token === null) {
token = prompt("getToken", "gap_callbackServer:");
}
xmlhttp.open("GET", "http://127.0.0.1:"+port+"/"+token , true);
xmlhttp.send();
}

mole.exports = {
start: function() {
startXhr();
},

stop: function() {
if (xmlhttp) {
var tmp = xmlhttp;
xmlhttp = null;
tmp.abort();
}
},

isAvailable: function() {
return ("true" != prompt("usePolling", "gap_callbackServer:"));
}
};

});

主要的处理是startXhr函数。它向java的server端发起了ajax请求,在onreadystatechange这个回调函数中等待server端返回结果。如果server端返回的结果正确,则再通过setTimeout(startXhr, 1)函数,1豪秒后再重新向server端发起ajax请求。如果从server返回的结果状态是404,则每隔10豪秒,重新向server端发起ajax请求。
接下来看看server端是如何处理的。server端的处理代码是CallbackServer.java中的run()方法中。
先看server端的源码
它通过serverSocket来模拟http的server端。其中,jsMessageQueue中是从java端发送的js消息,serverSocket在接收到客户端(js端)的请求后,会将jsMessageQueue中的js代码发送到客户端(js端)。

/**
* Start running the server.
* This is called automatically when the server thread is started.
*/
public void run() {

// Start server
try {
this.active = true;
String request;
waitSocket = new ServerSocket(0);
this.port = waitSocket.getLocalPort();
//Log.d(LOG_TAG, "CallbackServer -- using port " +this.port);
this.token = java.util.UUID.randomUUID().toString();
//Log.d(LOG_TAG, "CallbackServer -- using token "+this.token);

while (this.active) {
//Log.d(LOG_TAG, "CallbackServer: Waiting for data on socket");
Socket connection = waitSocket.accept();
BufferedReader xhrReader = new BufferedReader(new InputStreamReader(connection.getInputStream()), 40);
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
request = xhrReader.readLine();
String response = "";
//Log.d(LOG_TAG, "CallbackServerRequest="+request);
if (this.active && (request != null)) {
if (request.contains("GET")) {

// Get requested file
String[] requestParts = request.split(" ");

// Must have security token
if ((requestParts.length == 3) && (requestParts[1].substring(1).equals(this.token))) {
//Log.d(LOG_TAG, "CallbackServer -- Processing GET request");
String payload = null;

// Wait until there is some data to send, or send empty data every 10 sec
// to prevent XHR timeout on the client
while (this.active) {
if (jsMessageQueue != null) {
payload = jsMessageQueue.popAndEncode();
if (payload != null) {
break;
}
}
synchronized (this) {
try {
this.wait(10000); // prevent timeout from happening
//Log.d(LOG_TAG, "CallbackServer>>> break <<<");
break;
} catch (Exception e) {
}
}
}

// If server is still running
if (this.active) {

// If no data, then send 404 back to client before it times out
if (payload == null) {
//Log.d(LOG_TAG, "CallbackServer -- sending data 0");
response = "HTTP/1.1 404 NO DATA\r\n\r\n "; // need to send content otherwise some Android devices fail, so send space
}
else {
//Log.d(LOG_TAG, "CallbackServer -- sending item");
response = "HTTP/1.1 200 OK\r\n\r\n";
response += encode(payload, "UTF-8");
}
}
else {
response = "HTTP/1.1 503 Service Unavailable\r\n\r\n ";
}
}
else {
response = "HTTP/1.1 403 Forbidden\r\n\r\n ";
}
}
else {
response = "HTTP/1.1 400 Bad Request\r\n\r\n ";
}
//Log.d(LOG_TAG, "CallbackServer: response="+response);
//Log.d(LOG_TAG, "CallbackServer: closing output");
output.writeBytes(response);
output.flush();
}
output.close();
xhrReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
this.active = false;
//Log.d(LOG_TAG, "CallbackServer.startServer() - EXIT");
}

Ⅵ android怎样用token维持原登录状态

1.首次登录的时候,让服务端传会token给你,然后你把token保存。
2.下次在进入首页界面之前,通过接口把保存的token上传给服务端。
3.如果服务端验证token成功,直接进入首页,否则进入登录页。

Ⅶ Android 自动登入是采用的什么机制

1.第一次登陆getUserInfo里带一个长效token,该长效token用来判断用户是否登录和换取短token
2.通过sp存储将长效token保存起来。
3.接口请求用长效的token换取短token,短token服务端可以根据你的接口最后一次请求作为指示,超时时间为一天。
4.如果短小token失效在用长效token去替换
5.长效Token失效,提示用户再次登录

阅读全文

与androidtoken机制相关的资料

热点内容
android检查是否安装 浏览:371
苹果手机编辑pdf文件 浏览:454
android系统名字 浏览:963
安卓手机如何进去有求必应屋 浏览:432
指数除法运算法则底数不同 浏览:894
90压缩干粮09压缩干粮 浏览:516
android线程池框架 浏览:481
手机自带解压能解压哪些文件 浏览:804
linux安装hba驱动 浏览:119
java构造函数new 浏览:668
怎么查家里电器耗电量app 浏览:506
原神一直显示重新连接服务器怎么办 浏览:826
一般用途轴流式压缩机 浏览:926
没学历的怎么学编程 浏览:901
华为的隐藏相册无法加密 浏览:782
联通套餐app怎么设置 浏览:752
关于删除链表的算法描述 浏览:894
标准盘和压缩盘的区别 浏览:47
银行存款验证码JAVA编程 浏览:111
word转pdf软件免费版 浏览:139