Add JWT authorization
This commit is contained in:
parent
5dc11c6fec
commit
5116828ca3
@ -2,6 +2,10 @@ apply plugin: 'com.android.application'
|
|||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 27
|
compileSdkVersion 27
|
||||||
|
compileOptions {
|
||||||
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
|
}
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "com.uam.wmi.findmytutor"
|
applicationId "com.uam.wmi.findmytutor"
|
||||||
minSdkVersion 19
|
minSdkVersion 19
|
||||||
@ -31,5 +35,6 @@ dependencies {
|
|||||||
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
|
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
|
||||||
implementation "io.swagger:swagger-annotations:1.5.15"
|
implementation "io.swagger:swagger-annotations:1.5.15"
|
||||||
implementation "org.threeten:threetenbp:1.3.5"
|
implementation "org.threeten:threetenbp:1.3.5"
|
||||||
|
implementation "com.squareup.okhttp3:logging-interceptor:3.10.0"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -319,7 +319,7 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Boolean doInBackground(Void... params) {
|
protected Boolean doInBackground(Void... params) {
|
||||||
LdapService service = RetrofitClientInstance.getRetrofitLoginInstance().create(LdapService.class);
|
LdapService service = RetrofitClientInstance.createService(LdapService.class);
|
||||||
|
|
||||||
LdapUser user = new LdapUser("string",mPassword,"string","pracownik","string","string",mEmail);
|
LdapUser user = new LdapUser("string",mPassword,"string","pracownik","string","string",mEmail);
|
||||||
|
|
||||||
|
@ -5,7 +5,19 @@ import android.content.SharedPreferences;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.uam.wmi.findmytutor.R;
|
import com.uam.wmi.findmytutor.R;
|
||||||
|
import com.uam.wmi.findmytutor.model.Coordinate;
|
||||||
|
import com.uam.wmi.findmytutor.network.RetrofitClientInstance;
|
||||||
|
import com.uam.wmi.findmytutor.service.Coordinates;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Callback;
|
||||||
|
import retrofit2.Response;
|
||||||
|
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
@ -14,8 +26,27 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
SharedPreferences sharedPref = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
|
SharedPreferences sharedPref = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
|
||||||
String text = sharedPref.getString("test",null);
|
final String authToken = sharedPref.getString("authToken",null);
|
||||||
Log.e("Mainactivity", text);
|
|
||||||
|
Coordinates service = RetrofitClientInstance.createService(Coordinates.class,authToken);
|
||||||
|
|
||||||
|
Call<List<Coordinate>> call = service.getTopCoordinates(authToken);
|
||||||
|
|
||||||
|
|
||||||
|
call.enqueue(new Callback<List<Coordinate>>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<List<Coordinate>> call, Response<List<Coordinate>> response) {
|
||||||
|
Log.e("MainActivity", "rsp: " + response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<List<Coordinate>> call, Throwable t) {
|
||||||
|
Toast.makeText(MainActivity.this, "Something went wrong...Error message: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,71 +1,51 @@
|
|||||||
package com.uam.wmi.findmytutor.network;
|
package com.uam.wmi.findmytutor.network;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
import retrofit2.converter.gson.GsonConverterFactory;
|
import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
|
|
||||||
public class RetrofitClientInstance {
|
public class RetrofitClientInstance {
|
||||||
private static Retrofit retrofit;
|
|
||||||
private static final String BASE_URL = "https://s416084.projektstudencki.pl/develop/";
|
private static final String BASE_URL = "https://s416084.projektstudencki.pl/develop/";
|
||||||
|
|
||||||
|
private static Retrofit.Builder builder
|
||||||
|
= new Retrofit.Builder()
|
||||||
|
.baseUrl(BASE_URL)
|
||||||
|
.addConverterFactory(GsonConverterFactory.create());
|
||||||
|
|
||||||
public static Retrofit getRetrofitLoginInstance() {
|
private static Retrofit retrofit = builder.build();
|
||||||
if (retrofit == null) {
|
|
||||||
retrofit = new retrofit2.Retrofit.Builder()
|
private static OkHttpClient.Builder httpClient
|
||||||
.baseUrl(BASE_URL)
|
= new OkHttpClient.Builder();
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
|
||||||
.build();
|
private static HttpLoggingInterceptor logging
|
||||||
|
= new HttpLoggingInterceptor()
|
||||||
|
.setLevel(HttpLoggingInterceptor.Level.BASIC);
|
||||||
|
|
||||||
|
public static <S> S createService(Class<S> serviceClass) {
|
||||||
|
if (!httpClient.interceptors().contains(logging)) {
|
||||||
|
httpClient.addInterceptor(logging);
|
||||||
|
builder.client(httpClient.build());
|
||||||
|
retrofit = builder.build();
|
||||||
}
|
}
|
||||||
return retrofit;
|
return retrofit.create(serviceClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Retrofit getRetrofitInstance() {
|
public static <S> S createService(Class<S> serviceClass, final String token) {
|
||||||
if (retrofit == null) {
|
if (token != null) {
|
||||||
retrofit = new retrofit2.Retrofit.Builder()
|
httpClient.interceptors().clear();
|
||||||
.baseUrl(BASE_URL)
|
httpClient.addInterceptor(chain -> {
|
||||||
.client(provideOkHttpClient())
|
Request original = chain.request();
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
Request.Builder builder1 = original.newBuilder()
|
||||||
.build();
|
.header("Authorization", "Bearer " + token);
|
||||||
|
Request request = builder1.build();
|
||||||
|
return chain.proceed(request);
|
||||||
|
});
|
||||||
|
builder.client(httpClient.build());
|
||||||
|
retrofit = builder.build();
|
||||||
}
|
}
|
||||||
return retrofit;
|
return retrofit.create(serviceClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static OkHttpClient provideOkHttpClient() {
|
|
||||||
OkHttpClient.Builder okhttpClientBuilder = new OkHttpClient.Builder();
|
|
||||||
okhttpClientBuilder.connectTimeout(30, TimeUnit.SECONDS);
|
|
||||||
okhttpClientBuilder.readTimeout(30, TimeUnit.SECONDS);
|
|
||||||
okhttpClientBuilder.writeTimeout(30, TimeUnit.SECONDS);
|
|
||||||
return okhttpClientBuilder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// private OkHttpClient provideokHttpClient() {
|
|
||||||
// OkHttpClient.Builder okhttpClientBuilder = new OkHttpClient.Builder();
|
|
||||||
// okhttpClientBuilder.connectTimeout(30, TimeUnit.SECONDS);
|
|
||||||
// okhttpClientBuilder.readTimeout(30, TimeUnit.SECONDS);
|
|
||||||
// okhttpClientBuilder.writeTimeout(30, TimeUnit.SECONDS);
|
|
||||||
//
|
|
||||||
// okhttpClientBuilder.addInterceptor(new NetworkConnectionInterceptor() {
|
|
||||||
// @Override
|
|
||||||
// public boolean isInternetAvailable() {
|
|
||||||
// return this.isInternetAvailable();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void onInternetUnavailable() {
|
|
||||||
// if (mInternetConnectionListener != null) {
|
|
||||||
// mInternetConnectionListener.onInternetUnavailable();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void onCacheUnavailable() {
|
|
||||||
// if (mInternetConnectionListener != null) {
|
|
||||||
// mInternetConnectionListener.onCacheUnavailable();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ public interface Coordinates {
|
|||||||
Call<List<Coordinate>> getTopCoordinatesByUserId(@Path("userId") String userId , @Header("Authorization") JwtToken token);
|
Call<List<Coordinate>> getTopCoordinatesByUserId(@Path("userId") String userId , @Header("Authorization") JwtToken token);
|
||||||
|
|
||||||
@GET("api/coordinates/top")
|
@GET("api/coordinates/top")
|
||||||
Call<List<Coordinate>> getTopCoordinates(@Header("Authorization") JwtToken token);
|
Call<List<Coordinate>> getTopCoordinates(@Header("Authorization") String auth);
|
||||||
|
|
||||||
@GET("api/coordinates/top/online")
|
@GET("api/coordinates/top/online")
|
||||||
Call<List<Coordinate>> getOnlineCoordinates(@Header("Authorization") JwtToken token);
|
Call<List<Coordinate>> getOnlineCoordinates(@Header("Authorization") JwtToken token);
|
||||||
|
Loading…
Reference in New Issue
Block a user