philipsorst / angular-rest-springsecurity

An example AngularJS Application that uses a Spring Security protected Jersey REST backend based on Hibernate/JPA
Apache License 2.0
620 stars 367 forks source link

Content-type in user authentication #2

Closed 0x08 closed 10 years ago

0x08 commented 10 years ago

Hi,

I've been checking out your example and it has helped me understand the technology used a lot better.

I have found one issue however:

The LoginService definition is as follows:

services.factory('LoginService', function($resource) {
    return $resource('rest/user/:action', {},
            {
                authenticate: {
                    method: 'POST',
                    params: {'action' : 'authenticate'},
                    headers : {'Content-Type': 'application/x-www-form-urlencoded'}
                },
            }
        );
});

However, the Content-Type header here does not seem to be working. When I look at the actual headers sent over the wire, a different header is sent:

POST /angular/rest/user/authenticate HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 27
Accept: application/json, text/plain, */*
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:8080/angular/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

In your application this is not an issue as you use JAXRS annotated resources. However, we are using Spring MVC @Controllers, which require 'application/x-www-form-urlencoded' as Content-Type in order to bind the POST body to @RequestParams.

I think this is an issue in ngResource, I thought I would just let you know this is occurring.

0x08 commented 10 years ago

Just FYI, I solved this by sending the authentication data as JSON in the POST body and mapping it to an AuthenticationRequest object with a @ResponseBody annotation.

philipsorst commented 10 years ago

Couldn't reproduce it (and angular was updated meanwhile). Closing.