所以我开始为我的SPA使用Tyra JWT包Laravel。一切顺利(添加用户,登录,获取Auth用户)但是当我发出API请求时它只能工作一个小时。我知道我登录时存储的令牌会过期,因此当我在60分钟后向API发出请求时它不起作用。我的问题是如何刷新令牌并在本地存储中恢复它?在新的要求?每59分钟一次?
AuthController.php
<li>
Login.vue
public function authenticate(Request $request)
{
// grab credentials from the request
$credentials = $request->only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'Sorry, we cant find you.']);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
// all good so return the token
$user = JWTAuth::toUser($token);
//Fire off the login event
event( new LoginSuccessful($user) );
return response()->json( compact('token', 'user') );
}
在我的头标记
axios.post('/api/authenticate',{
email: this.email,
password: this.password
})
.then(response => {
if(response.data.error){
//Show the error
this.error = response.data.error
this.loading = false;
} else {
this.loading = false;
//Store the items in storage
localStorage.setItem('jwt_token', response.data.token);
localStorage.setItem('user', JSON.stringify(response.data.user));
//Mutate the state
this.$store.commit('login');
//Now go to the dashboard
this.$router.push('dashboard');
}
})
.catch(error => {
});
在我的bootstrap.js
中<script>
window.hopbak = {
'jwt_token': localStorage.getItem('jwt_token'),
'csrfToken': {!! json_encode( csrf_token() ) !!},
};
</script>
答案 0 :(得分:0)
我认为您需要在import csv
import pandas as pd
df = pd.DataFrame(
data=[[ 0, 0, 2, 5, 0],
[1478, 3877, 3674, 2328, 2539],
[1613, 4088, 3991, 6461, 2691],
[1560, 3392, 3826, 4787, 2613],
[1608, 4802, 3932, 4477, 2705],
[1576, 3933, 3909, 4979, 2685],
[ 95, 229, 255, 496, 201],
[ 2, 0, 1, 27, 0],
[1438, 3785, 3589, 4174, 2215],
[1342, 4043, 4009, 4665, 3033]],
index=['05-01-11', '05-02-11', '05-03-11', '05-04-11', '05-05-11',
'05-06-11', '05-07-11', '05-08-11', '05-09-11', '05-10-11'],
columns=['R003', 'R004', 'R005', 'R006', 'R007']
)
myDATA=df.to_dict(orient='records') #Convert DataFrame to dictionary(records) : list like [{column -> value}, ... , {column -> value}]
header = list(df.columns.values) #get column name ['R003', 'R004', 'R005', 'R006', 'R007']
index = list(df.index) #get index name ['05-01-11', '05-02-11', '05-03-11', '05-04-11', '05-05-11', '05-06-11', '05-07-11', '05-08-11', '05-09-11', '05-10-11']
with open("TestTry.csv", "w") as g:
writer = csv.DictWriter(g, delimiter=",", fieldnames=['index']+header,lineterminator='\r\n')
writer.writeheader()
for i,row in enumerate(myDATA):
temp_row = row
temp_row['index'] = index[i] #add index into dict
writer.writerow(temp_row)
注册RefreshToken
中间件:
app/Http/Kernel.php
然后将其分配给要刷新令牌的路由。
查看source code,我可以看出这个中间件将通过向响应头添加新令牌来处理每个请求中令牌的刷新。
答案 1 :(得分:0)
JWT_TTL , JWT_REFRESH_TTL 和 JWT_BLACKLIST_GRACE_PERIOD 值在config / jwt.php文件中设置。
它是如何运作的:
有关详细信息,请参阅this my explanation。