Android : Can't authenticate to host server through retrofit

on Tuesday, October 28, 2014


I am running retrofit through Springboot via Gradle. I am trying to connect to my localhost server which is set to port 8080. I am unable to authenticate through my client application(an emulated android application). When I try to connect through my browser a login screen pops up



> A username and password are being requested by http://localhost:8080. The site says: "Spring"


When I send the request I get an error. retrofit.Retrofit unauthorized 401 error, apparently I need to authenticate. On the console log I also get this



2014-10-29 00:50:51.708 WARN 7352 --- [nio-8080-exec-1] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:50:51.713 INFO 7352 --- [nio-8080-exec-1] o.s.b.a.audit.listener.AuditListener : AuditEvent [timestamp=Wed Oct 29 00:50:51 JST 2014, principal=<unknown>, type=AUTHENTICATION_FAILURE, data={message=An Authentication object was not found in the SecurityContext, type=org.springframework.security.authentication.AuthenticationCredentialsNotFoundException}]
2014-10-29 00:51:37.750 WARN 7352 --- [nio-8080-exec-2] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:51:37.751 INFO 7352 --- [nio-8080-exec-2] o.s.b.a.audit.listener.AuditListener : AuditEvent [timestamp=Wed Oct 29 00:51:37 JST 2014, principal=<unknown>, type=AUTHENTICATION_FAILURE, data={message=An Authentication object was not found in the SecurityContext, type=org.springframework.security.authentication.AuthenticationCredentialsNotFoundException}]
2014-10-29 00:51:44.596 WARN 7352 --- [nio-8080-exec-3] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:51:44.602 WARN 7352 --- [nio-8080-exec-3] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:51:44.621 INFO 7352 --- [nio-8080-exec-3] o.s.b.a.audit.listener.AuditListener : AuditEvent [timestamp=Wed Oct 29 00:51:44 JST 2014, principal=Spring, type=AUTHENTICATION_FAILURE, data={message=Bad credentials, type=org.springframework.security.authentication.BadCredentialsException}]
2014-10-29 00:51:51.451 WARN 7352 --- [nio-8080-exec-4] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:51:51.453 WARN 7352 --- [nio-8080-exec-4] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:51:51.457 INFO 7352 --- [nio-8080-exec-4] o.s.b.a.audit.listener.AuditListener : AuditEvent [timestamp=Wed Oct 29 00:51:51 JST 2014, principal=Authorization, type=AUTHENTICATION_FAILURE, data={message=Bad credentials, type=org.springframework.security.authentication.BadCredentialsException}]
2014-10-29 00:51:57.468 WARN 7352 --- [nio-8080-exec-5] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:51:57.469 WARN 7352 --- [nio-8080-exec-5] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:51:57.470 INFO 7352 --- [nio-8080-exec-5] o.s.b.a.audit.listener.AuditListener : AuditEvent [timestamp=Wed Oct 29 00:51:57 JST 2014, principal=admin, type=AUTHENTICATION_FAILURE, data={message=Bad credentials, type=org.springframework.security.authentication.BadCredentialsException}]
2014-10-29 00:52:01.451 WARN 7352 --- [nio-8080-exec-6] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:52:01.453 WARN 7352 --- [nio-8080-exec-6] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:52:01.455 INFO 7352 --- [nio-8080-exec-6] o.s.b.a.audit.listener.AuditListener : AuditEvent [timestamp=Wed Oct 29 00:52:01 JST 2014, principal=manager, type=AUTHENTICATION_FAILURE, data={message=Bad credentials, type=org.springframework.security.authentication.BadCredentialsException}]
2014-10-29 00:52:04.399 WARN 7352 --- [nio-8080-exec-7] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:52:04.400 WARN 7352 --- [nio-8080-exec-7] o.s.c.s.ResourceBundleMessageSource : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-29 00:52:04.408 INFO 7352 --- [nio-8080-exec-7] o.s.b.a.audit.listener.AuditListener : AuditEvent [timestamp=Wed Oct 29 00:52:04 JST 2014, principal=admin, type=AUTHENTICATION_FAILURE, data={message=Bad credentials, type=org.springframework.security.authentication.BadCredentialsException}]
2014-10-29 00:52:22.371 WARN 7352 --- [nio-8080-exec-8] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'dispatcherServlet'


I create my RestAdapter APi as follow from my client



PhotoSvcApi photoService= new RestAdapter.Builder() .setEndpoint(PhotoSvcApi.TEST_URL).setLogLevel(LogLevel.FULL).build()
.create(PhotoSvcApi.class);


my PhotoSvcApi



public interface PhotoSvcApi {


public static final String TITLE_PARAMETER = "title";

public final String TEST_URL = "http://10.0.2.2:8080";

//public static final String TOKEN_PATH = "/oauth/token";

// The path where we expect the VideoSvc to live
public static final String PHOTO_SVC_PATH = "/video";

// The path to search videos by title
public static final String PHOTO_TITLE_SEARCH_PATH = PHOTO_SVC_PATH + "/search/findByName";


@GET(PHOTO_SVC_PATH)
public Collection<Photo> getPhotoList();

@POST(PHOTO_SVC_PATH)
public Void addPhoto(@Body Photo p);

@GET(PHOTO_TITLE_SEARCH_PATH)
public Collection<Photo> findByTitle(@Query(TITLE_PARAMETER) String title);


}


My gradle build on my server side is as follows.



buildscript {
ext {
springBootVersion = '1.0.2.RELEASE'
}
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.2.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'

sourceCompatibility = 1.7
targetCompatibility = 1.7

war {
baseName = 'gs-convert-jar-to-war'
version = '0.1.0'
}

repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
maven { url "http://maven.springframework.org/milestone" }

flatDir {
dirs 'lib'
}
}

dependencies {

compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")

compile("org.springframework.boot:spring-boot-starter-tomcat:${springBootVersion}")

compile("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-aop:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")

compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")
compile("org.springframework.data:spring-data-rest-webmvc")

compile("org.springframework.boot:spring-boot-starter-security:${springBootVersion}")
// compile("org.springframework.security.oauth:spring-security-oauth2:2.0.0.RC2")
// compile("org.springframework.security.oauth:spring-security-oauth2-javaconfig:1.0.0.M1")

compile("org.hsqldb:hsqldb")

compile("com.google.guava:guava:17.0")
compile("org.apache.commons:commons-lang3:3.3.2")
compile("org.apache.httpcomponents:httpclient:4.3.4")
compile("com.squareup.retrofit:retrofit:1.7.1")
compile("commons-io:commons-io:2.4")

compile("com.github.davidmarquis:fluent-interface-proxy:1.3.0")
compile(":mobilecloud.handin:1.0.0")


testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}


Now I am uncertain how to go about and set my authentication for the Tomcat server. I have commented out my oauth2 dependency as I am try to simply test the client can access the server. I am running my program through Eclipse.


0 comments:

Post a Comment