Angular 6 - 如何在每个请求中维护jwt会话?

时间:2018-05-30 12:31:47

标签: node.js angular6

我正在使用Angular 6和NodeJS(API)。

对于身份验证我正在使用JWT会话令牌。登录时我可以授权令牌并登录主页。但是当连续使用应用程序时,它不会维护会话。令牌过期30秒。它没有为每个请求重置令牌。

使用HttpInterceptor为每个请求发送令牌。

角度代码:

private BroadcastReceiver mBluetoothScoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
        System.out.println("ANDROID Audio SCO state: " + state);
        if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
            /*
             * Now the connection has been established to the bluetooth device.
             * Record audio or whatever (on another thread).With AudioRecord you can record with an object created like this:
             * new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
             * AudioFormat.ENCODING_PCM_16BIT, audioBufferSize);
             *
             * After finishing, don't forget to unregister this receiver and
             * to stop the bluetooth connection with am.stopBluetoothSco();
             */
        }
    }
};

 @Override
protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
    registerReceiver(mBluetoothScoReceiver, intentFilter);
    audioManager = (AudioManager) getApplicationContext().getSystemService(getApplicationContext().AUDIO_SERVICE);
    // Start Bluetooth SCO.
    audioManager.setMode(audioManager.MODE_NORMAL);
    audioManager.setBluetoothScoOn(true);
    audioManager.startBluetoothSco();
    // Stop Speaker.
    audioManager.setSpeakerphoneOn(false);

}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(mBluetoothScoReceiver);
    // Stop Bluetooth SCO.
    audioManager.stopBluetoothSco();
    audioManager.setMode(audioManager.MODE_NORMAL);
    audioManager.setBluetoothScoOn(false);
    // Start Speaker.
    audioManager.setSpeakerphoneOn(true);
}

1 个答案:

答案 0 :(得分:0)

您的后端需要处理此问题,要么每次请求都返回一个新令牌,要么增加到期所需的时间