0

I am attempting to consume a REST API with an Android app I am developing in Android Studio. I have developed the API using Spring Boot.

The issue:

Whenever I make a call to my API I am always returned a 401 Unauthorized response. "Pre-authenticated entry point called. Rejecting access."

I have configured my CORS as follows:
cors: allowed-origins: "*" allowed-methods: GET, PUT, POST, DELETE, OPTIONS allowed-headers: "*" exposed-headers: allow-credentials: true max-age: 1800

My api is running on localhost:8080, and I am making the requests from my device with my WLAN IPv4 address.

Such as: curl -v -X GET http://192.168.1.xx:8080/api/users
This returns a 200 OK response.
I am also able to call this in postman/DHC etc.. and receive a 200 OK response.

However, when I call this same address with HttpURLConnection through my android device, I receive a 401 response.

Noob developer here - any ideas as to what might be causing this would be greatly appreciated!

Edit to include my GET Request:

@Override
protected String doInBackground(String... params){
    String stringUrl = params[0];
    String result;
    String inputLine;
    try {
        //Create a URL object holding our url
        URL myUrl = new URL(stringUrl);
        //Create a connection
        HttpURLConnection connect =(HttpURLConnection)
                myUrl.openConnection();
        connect.setRequestMethod(REQUEST_METHOD); // GET
        connect.setRequestProperty("Host", HOST);
        connect.setRequestProperty("Connection", "keep-alive");
        connect.setRequestProperty("Origin", ORIGIN);
        connect.setRequestProperty("User-Agent", System.getProperty("http.agent"));
        connect.setRequestProperty("Content-Type", CONTENT_TYPE);
        connect.setRequestProperty("Accept", "*/*");
        connect.setRequestProperty("Accept-Encoding", ACCEPT_ENCODING);
        connect.setRequestProperty("Accept-Language", ACCEPT_LANGUAGE);

        connect.setDoOutput(true);
        connect.setDoInput(true);

        //Connect to our url
        connect.connect();
        String responseMessage = connect.getResponseMessage();  // Unathorized
        int responseCode = connect.getResponseCode();   //401
        //Create a new InputStreamReader
        InputStreamReader streamReader = new
                InputStreamReader(connect.getInputStream());
        //Create a new buffered reader and String Builder
        BufferedReader reader = new BufferedReader(streamReader);
        StringBuilder stringBuilder = new StringBuilder();
        //Check if the line we are reading is not null
        while((inputLine = reader.readLine()) != null){
            stringBuilder.append(inputLine);
        }
        //Close our InputStream and Buffered reader
        reader.close();
        streamReader.close();
        //Set our result equal to our stringBuilder
        result = stringBuilder.toString();
    }
    catch(IOException e){
        e.printStackTrace();
        result = null;
    }
    return result;
}
Alien
  • 444
  • 2
  • 9
  • 28
  • This might help you - http://stackoverflow.com/questions/6176609/connect-an-android-device-to-a-web-service-on-local-host – Rahul Dec 18 '16 at 08:12
  • Replace:- InputStreamReader streamReader = new InputStreamReader(connect.getInputStream()); With:- InputStreamReader streamReader = new InputStreamReader(connect.getErrorStram()); This will help you track why 401 then you can proceed solving that issue. 401-- is server side problem, Server is unable to process you request, check if you have to pass data in header or url as you are using get request – Mrinmoy Dec 19 '16 at 06:09

1 Answers1

1

Replace:-

InputStreamReader streamReader = new InputStreamReader(connect.getInputStream());

With:-

InputStreamReader streamReader = new InputStreamReader(connect.getErrorStram());

This will help you track why 401 then you can proceed solving that issue.

401-- is server side problem, Server is unable to process you request, check if you have to pass data in header or url as you are using get request

Mrinmoy
  • 1,370
  • 2
  • 18
  • 28
  • 1
    Hey great, thanks so much for that. I figured out that this was an issue with my CORS filter. In case anyone is wondering the spring security filter was of higher precedence than my CORS filter and therefore was rejecting my requests from my tablet. I fixed this by adding `.authorizeRequests() .antMatchers(HttpMethod.OPTIONS,"/**").permitAll()` in my SecurityConfiguration. – Alien Dec 19 '16 at 16:25
  • As well as setting the CORS Filter to `@Order(Ordered.HIGHEST_PRECEDENCE)` By default, the security chain is set to Order `0` this can be set via properties such as `security.filter-order= ` – Alien Dec 19 '16 at 16:32