Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7762947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:32:27+00:00 2026-06-01T14:32:27+00:00

I have built a REST API backend whith Spring MVC and secured with basic

  • 0

I have built a REST API backend whith Spring MVC and secured with basic Auth with Spring Security.

I would like to do cross domain ajax call to the REST API from Javascript clients. I don’t want to use JSONP because i don’t want to be limited to GET calls. I use CORS and i have put the right headers on server side.

Suppose my REST API is on domain localhost:8087 and my client on localhost:8086, which is cross domain call.

In my Javascript client, i make ajax call with jQuery :

<script>
        $.ajax ({
            url: "http://localhost:8087/SpringMVC/users/user1",
            beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", "Basic xxxxxxxxxxxx"); },
            success: function(val) { console.log(val); alert("success" + val); },
            error: function(val) { console.log(val); alert("error" + val); }
        });
</script>

My problem is that jQuery does not send the Authorization header in the HTTP request and i don’t know why. I don’t understand because i do it in the beforeSend method, so it should be inside the HTTP request. Result : i have a 401 error.

When i try the script from the same domain localhost:8087, which is not cross domain anymore, i have no problem.

How is it possible ?

My script is just a test. I don’t intend to put my username/password on client side. But i want to test how to do ajax calls to a basic auth protected REST API. I imagine i have to send on server side to be secure my username/password, the REST API sends me back a cookie and i don’t need to pass the username/password anymore for my next ajax calls to the REST API. Am i right ?

I have tested my REST API with Chrome Advanced REST client and it is working like that. For the first request i need to pass the authorization header. Then it is not needed. Should it work also like that with my javascript web client ? I intend to use Node.JS with Backbone to build it.

Thanks a lot.

EDIT2 : Seems really to be a CORS Browser problem. I have added the header Access-Control-Allow-Methods for OPTIONS method on server side and it works on Chrome. I have access to the JSON response with no error anymore. But i still need to use the authorization header for the next requests. How to tell jQuery to use the cookie sent ?

And when i try with Firefox 11, i have no access to the json response and i have the error :

"NetworkError: 401 Non-Autorisé - http://localhost:8087/SpringMVC/users/user1"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:32:29+00:00Added an answer on June 1, 2026 at 2:32 pm

    Apparently, Chrome and Firefox treat Cross Domain requests a bit differently.
    Before doing the cross domain request, they do what is called a ‘preflight’ request with HTTP OPTIONS method. The difference between Chrome and Firefox is that Chrome sends also the Authorization header with the credentials whereas Firefox do not.

    Then, it remains a Spring Security config problem. My url /users/* is secured for all HTTP methods, including OPTIONS. In the case of Firefox, as the Authorization header is not sent, my request is not authorized. If i restrict my secure url /users/* to the GET method only, then it perfectly works for Firefox. So i had to add only that in my Spring security config :

    <intercept-url pattern="/users/*" access="isAuthenticated()" method="GET"/>
    

    Afterwards, i have the choice : i can add other methods to be secured in the intercept-url, except OPTIONS, or i can restrict the HTTP method call to GET in my Spring MVC controller, which will even treat my OPTIONS calls according to the Javadoc. I chose the second solution. But if somebody finds a solution to force Firefox to send the credentials like Chrome, it would be great and i would chose this one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built an MSI that I would like to deploy, and update frequently.
I have built an application in C# that I would like to be optimized
I have an NSArray built from json data from twitter REST API. NSArray *dict
I am working on a PHP REST API. I would like require a user
I would like to use Backbone.js with a REST api I control. I was
I have built a pretty simple REST service in Sinatra, on Rack. It's backed
I have created a small game in Java and I would like to add
I'm using django-piston for my REST json api, and I have it all set
I've built a REST API with the WCF Web API Preview and I wanna
I've read about oAuth, Amazon REST API, HTTP Basic/Digest and so on but can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.