'How to validate bearer access token in spring boot using jwt public key
I have access token generated from websec using client id and secret.
My project app.properties have jwt public key.
I am developing rest api , call to Rest api will provide Bear token (generated one)that I wanted to validate using jwt public key.
But spring security internally use in memory token validator and return invalid token.
So how can l use jwt public key to validate the bearer token.
Or here I lack some understanding.
Solution 1:[1]
See here you will get a clear idea how validating token using spring security. https://www.devglan.com/spring-security/spring-boot-jwt-auth
Solution 2:[2]
You have quite some problems with your code. I will list them below:
Required fixes
- As @about14sheep said, you must use
awaitto get the actual response. - You are not supposed to use
JSON.parse()on a response, but ratherresponse.json()to get the JSON body. - Use of an incorrect operator, for numbers I'd suggest you use
==than=== - Before 10 seconds have passed, it will show undefined. To fix this, call the function before the
setInterval().
Optional things I added
- Formatted JS code
- Added semicolons when necessary
- Used ES6 template literals feature
- Removed async when un-needed
I have fixed all of these problems in the code below.
let jogadores;
client.on('ready', async () => {
async function players_online() {
const response = await fetch(`http://ip:port/dynamic.json`);
const data = await response.json();
jogadores = data.clients;
}
function autoconnect() {
if (jogadores == -1) {
console.log('Offline');
} else {
console.log(`Online with ${jogadores}/5`);
}
}
autoconnect();
players_online();
setInterval(() => {
client.user.setStatus('dnd');
client.user.setActivity(`Online with ${jogadores} players.`, {
type: 'PLAYING'
});
}, 10000);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LingarajSahoo |
| Solution 2 | MrMythical |
