Adding JWT authentication token to the OkHttp , Dagger 2 & Retrofit
following post Dagger + Retrofit. Adding auth headers at runtime i'm trying to configure the okHttp & adding the jwt auth key to the okHttp by adding interceptor,for this i have created separate interceptor & added it to the Dagger's component so that it can be exposed anywhere.
Now once i hit the login i get the token,setting it using the setJwtToken() method of JwtAuthenticationInterceptor class & when i'm trying with the next endpoints i'm getting 401 error since jwtToken is coming null even though i have setted it.
Below i'm attaching my interceptor,component & module code snaps.
Module
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache)
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(provideHeaderInterceptor());
client.cache(cache);
return client.build();
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient)
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
@Provides
@Singleton
JwtAuthenticationInterceptor provideHeaderInterceptor()
return new JwtAuthenticationInterceptor();
Component
@Component(modules = AppModule.class, ApiModule.class, StorageModule.class)
@Singleton
public interface NetComponent
Retrofit retrofit();
OkHttpClient okHttpClient();
SharedPreferences sharedPreferences();
Gson gson();
Cache cache();
KRITILog log();
JwtAuthenticationInterceptor headerInterceptor();
JwtAuthenticationInterceptor.java
@Singleton
public class JwtAuthenticationInterceptor implements Interceptor
private String jwtToken;
@Inject
public JwtAuthenticationInterceptor()
public void setJwtToken(String jwtToken)
this.jwtToken = jwtToken;
@Override
public Response intercept(Chain chain) throws IOException
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.header("Authorization","Bearer " +jwtToken);
//String.format("Bearer %s", jwtToken));
Request request = builder.build();
return chain.proceed(request);
android dagger-2 okhttp
add a comment |
following post Dagger + Retrofit. Adding auth headers at runtime i'm trying to configure the okHttp & adding the jwt auth key to the okHttp by adding interceptor,for this i have created separate interceptor & added it to the Dagger's component so that it can be exposed anywhere.
Now once i hit the login i get the token,setting it using the setJwtToken() method of JwtAuthenticationInterceptor class & when i'm trying with the next endpoints i'm getting 401 error since jwtToken is coming null even though i have setted it.
Below i'm attaching my interceptor,component & module code snaps.
Module
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache)
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(provideHeaderInterceptor());
client.cache(cache);
return client.build();
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient)
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
@Provides
@Singleton
JwtAuthenticationInterceptor provideHeaderInterceptor()
return new JwtAuthenticationInterceptor();
Component
@Component(modules = AppModule.class, ApiModule.class, StorageModule.class)
@Singleton
public interface NetComponent
Retrofit retrofit();
OkHttpClient okHttpClient();
SharedPreferences sharedPreferences();
Gson gson();
Cache cache();
KRITILog log();
JwtAuthenticationInterceptor headerInterceptor();
JwtAuthenticationInterceptor.java
@Singleton
public class JwtAuthenticationInterceptor implements Interceptor
private String jwtToken;
@Inject
public JwtAuthenticationInterceptor()
public void setJwtToken(String jwtToken)
this.jwtToken = jwtToken;
@Override
public Response intercept(Chain chain) throws IOException
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.header("Authorization","Bearer " +jwtToken);
//String.format("Bearer %s", jwtToken));
Request request = builder.build();
return chain.proceed(request);
android dagger-2 okhttp
add a comment |
following post Dagger + Retrofit. Adding auth headers at runtime i'm trying to configure the okHttp & adding the jwt auth key to the okHttp by adding interceptor,for this i have created separate interceptor & added it to the Dagger's component so that it can be exposed anywhere.
Now once i hit the login i get the token,setting it using the setJwtToken() method of JwtAuthenticationInterceptor class & when i'm trying with the next endpoints i'm getting 401 error since jwtToken is coming null even though i have setted it.
Below i'm attaching my interceptor,component & module code snaps.
Module
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache)
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(provideHeaderInterceptor());
client.cache(cache);
return client.build();
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient)
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
@Provides
@Singleton
JwtAuthenticationInterceptor provideHeaderInterceptor()
return new JwtAuthenticationInterceptor();
Component
@Component(modules = AppModule.class, ApiModule.class, StorageModule.class)
@Singleton
public interface NetComponent
Retrofit retrofit();
OkHttpClient okHttpClient();
SharedPreferences sharedPreferences();
Gson gson();
Cache cache();
KRITILog log();
JwtAuthenticationInterceptor headerInterceptor();
JwtAuthenticationInterceptor.java
@Singleton
public class JwtAuthenticationInterceptor implements Interceptor
private String jwtToken;
@Inject
public JwtAuthenticationInterceptor()
public void setJwtToken(String jwtToken)
this.jwtToken = jwtToken;
@Override
public Response intercept(Chain chain) throws IOException
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.header("Authorization","Bearer " +jwtToken);
//String.format("Bearer %s", jwtToken));
Request request = builder.build();
return chain.proceed(request);
android dagger-2 okhttp
following post Dagger + Retrofit. Adding auth headers at runtime i'm trying to configure the okHttp & adding the jwt auth key to the okHttp by adding interceptor,for this i have created separate interceptor & added it to the Dagger's component so that it can be exposed anywhere.
Now once i hit the login i get the token,setting it using the setJwtToken() method of JwtAuthenticationInterceptor class & when i'm trying with the next endpoints i'm getting 401 error since jwtToken is coming null even though i have setted it.
Below i'm attaching my interceptor,component & module code snaps.
Module
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache)
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(provideHeaderInterceptor());
client.cache(cache);
return client.build();
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient)
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
@Provides
@Singleton
JwtAuthenticationInterceptor provideHeaderInterceptor()
return new JwtAuthenticationInterceptor();
Component
@Component(modules = AppModule.class, ApiModule.class, StorageModule.class)
@Singleton
public interface NetComponent
Retrofit retrofit();
OkHttpClient okHttpClient();
SharedPreferences sharedPreferences();
Gson gson();
Cache cache();
KRITILog log();
JwtAuthenticationInterceptor headerInterceptor();
JwtAuthenticationInterceptor.java
@Singleton
public class JwtAuthenticationInterceptor implements Interceptor
private String jwtToken;
@Inject
public JwtAuthenticationInterceptor()
public void setJwtToken(String jwtToken)
this.jwtToken = jwtToken;
@Override
public Response intercept(Chain chain) throws IOException
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.header("Authorization","Bearer " +jwtToken);
//String.format("Bearer %s", jwtToken));
Request request = builder.build();
return chain.proceed(request);
android dagger-2 okhttp
android dagger-2 okhttp
asked Nov 13 '18 at 13:04
Pranesh sawPranesh saw
186
186
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
problem is this line
client.addInterceptor(provideHeaderInterceptor());
there you are creating a new instance of the JwtAuthenticationInterceptor
, different from the one provided by dagger. JwtAuthenticationInterceptor
should be a dependency of that method. Eg
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache, JwtAuthenticationInterceptor interceptor)
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(interceptor);
client.cache(cache);
return client.build();
yes,it worked.just wondering if session got expired then again i need to hit login url to get the new token.
– Pranesh saw
Nov 13 '18 at 13:28
are you using open connect / oauth ?
– Blackbelt
Nov 13 '18 at 13:35
I'm using JSON Web Token(JWT)
– Pranesh saw
Nov 13 '18 at 13:46
right. you should also get a refresh token iirc. You can use that to renew your jwt token in case of 401
– Blackbelt
Nov 13 '18 at 13:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281653%2fadding-jwt-authentication-token-to-the-okhttp-dagger-2-retrofit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
problem is this line
client.addInterceptor(provideHeaderInterceptor());
there you are creating a new instance of the JwtAuthenticationInterceptor
, different from the one provided by dagger. JwtAuthenticationInterceptor
should be a dependency of that method. Eg
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache, JwtAuthenticationInterceptor interceptor)
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(interceptor);
client.cache(cache);
return client.build();
yes,it worked.just wondering if session got expired then again i need to hit login url to get the new token.
– Pranesh saw
Nov 13 '18 at 13:28
are you using open connect / oauth ?
– Blackbelt
Nov 13 '18 at 13:35
I'm using JSON Web Token(JWT)
– Pranesh saw
Nov 13 '18 at 13:46
right. you should also get a refresh token iirc. You can use that to renew your jwt token in case of 401
– Blackbelt
Nov 13 '18 at 13:47
add a comment |
problem is this line
client.addInterceptor(provideHeaderInterceptor());
there you are creating a new instance of the JwtAuthenticationInterceptor
, different from the one provided by dagger. JwtAuthenticationInterceptor
should be a dependency of that method. Eg
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache, JwtAuthenticationInterceptor interceptor)
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(interceptor);
client.cache(cache);
return client.build();
yes,it worked.just wondering if session got expired then again i need to hit login url to get the new token.
– Pranesh saw
Nov 13 '18 at 13:28
are you using open connect / oauth ?
– Blackbelt
Nov 13 '18 at 13:35
I'm using JSON Web Token(JWT)
– Pranesh saw
Nov 13 '18 at 13:46
right. you should also get a refresh token iirc. You can use that to renew your jwt token in case of 401
– Blackbelt
Nov 13 '18 at 13:47
add a comment |
problem is this line
client.addInterceptor(provideHeaderInterceptor());
there you are creating a new instance of the JwtAuthenticationInterceptor
, different from the one provided by dagger. JwtAuthenticationInterceptor
should be a dependency of that method. Eg
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache, JwtAuthenticationInterceptor interceptor)
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(interceptor);
client.cache(cache);
return client.build();
problem is this line
client.addInterceptor(provideHeaderInterceptor());
there you are creating a new instance of the JwtAuthenticationInterceptor
, different from the one provided by dagger. JwtAuthenticationInterceptor
should be a dependency of that method. Eg
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache, JwtAuthenticationInterceptor interceptor)
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(interceptor);
client.cache(cache);
return client.build();
answered Nov 13 '18 at 13:18
BlackbeltBlackbelt
128k22216242
128k22216242
yes,it worked.just wondering if session got expired then again i need to hit login url to get the new token.
– Pranesh saw
Nov 13 '18 at 13:28
are you using open connect / oauth ?
– Blackbelt
Nov 13 '18 at 13:35
I'm using JSON Web Token(JWT)
– Pranesh saw
Nov 13 '18 at 13:46
right. you should also get a refresh token iirc. You can use that to renew your jwt token in case of 401
– Blackbelt
Nov 13 '18 at 13:47
add a comment |
yes,it worked.just wondering if session got expired then again i need to hit login url to get the new token.
– Pranesh saw
Nov 13 '18 at 13:28
are you using open connect / oauth ?
– Blackbelt
Nov 13 '18 at 13:35
I'm using JSON Web Token(JWT)
– Pranesh saw
Nov 13 '18 at 13:46
right. you should also get a refresh token iirc. You can use that to renew your jwt token in case of 401
– Blackbelt
Nov 13 '18 at 13:47
yes,it worked.just wondering if session got expired then again i need to hit login url to get the new token.
– Pranesh saw
Nov 13 '18 at 13:28
yes,it worked.just wondering if session got expired then again i need to hit login url to get the new token.
– Pranesh saw
Nov 13 '18 at 13:28
are you using open connect / oauth ?
– Blackbelt
Nov 13 '18 at 13:35
are you using open connect / oauth ?
– Blackbelt
Nov 13 '18 at 13:35
I'm using JSON Web Token(JWT)
– Pranesh saw
Nov 13 '18 at 13:46
I'm using JSON Web Token(JWT)
– Pranesh saw
Nov 13 '18 at 13:46
right. you should also get a refresh token iirc. You can use that to renew your jwt token in case of 401
– Blackbelt
Nov 13 '18 at 13:47
right. you should also get a refresh token iirc. You can use that to renew your jwt token in case of 401
– Blackbelt
Nov 13 '18 at 13:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281653%2fadding-jwt-authentication-token-to-the-okhttp-dagger-2-retrofit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown