1

I am trying to use a webService with Spring Boot I have my RestControllerPage :

@RequestMapping(
            value="/api/user/connection",
            method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces=MediaType.APPLICATION_JSON_VALUE
    )
    public ResponseEntity<User> connect(@RequestBody Authentification authentification){
        try {
            if (authentification.getLogins() == "" || authentification.getLogins() == null) {
                return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
            String val = Md5.md5(authentification.getPwd());
            int nbuser = userDao.countUser(authentification.getLogins(), val);
            // we get the user for update the token
            LinkedList<User> listUser = userDao.findByLoginsAndPwd(authentification.getLogins(), val);
            if (listUser.get(0).getToken() == "" || listUser.get(0).getToken() == null) {
                listUser.get(0).setToken(Md5.md5(listUser.get(0).getId() + listUser.get(0).getLogins()));
            }
            if (userDao.save(listUser.get(0)) == null) {
                return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
            return new ResponseEntity<User>(listUser.get(0),HttpStatus.OK);
        }catch(Exception e){
            return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

And for contact This web service I am Using Jquery in other page out of the Spring boot Server :

<html>

<head>

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('#BtnValidate').click(function(){
                $.ajax({
                    type: "POST",
                    contentType: 'application/json',
                    dataType:'json',
                    url:'http://localhost:9000/api/user/connection',
                    data:JSON.stringify({
                        logins:$('#Txtlogin').val(),
                        pwd:$('#TxtPwd').val()
                    }),
                    success: function(data){
                        $('.notification').html("<p> response : "+data.d+"</p>");
                    },
                    error:function(){
                        alert('Error in the webService');
                    }

                });
                return false;
            });


        });

    </script>
</head>

<body>

        <label>Login : </label>
        <input type="text" id="Txtlogin">
        <br/>
        <label>Pwd : </label>
        <input type="pwd" id="TxtPwd">
        <br/>
        <input type="button" id="BtnValidate" value="validate"/>

    <div class="notification">

    </div>
</body>
</html>

And i get this error :

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.

I am really lock.

Thank you

zaafrani
  • 65
  • 3
  • 9

3 Answers3

0

So you are trying to make an API call from JavaScript to a different host or port? This is by default not permitted due to Same-Origin-Policy. You can however enable Cross Origin Requests in Spring Boot Enable CORS in Spring Boot

JuHarm89
  • 847
  • 3
  • 12
  • 26
  • I am testing this Thank you – zaafrani Apr 02 '16 at 13:26
  • But I must put my html page on my spring server ? – zaafrani Apr 02 '16 at 13:30
  • No, the point of this article is to activate CORS globally or for specific Controller so that you dont need to have your HTML + JavaScript in the same Spring Boot application. They use Controller but essentially it is the same as your RestController because they also serve JSON. – JuHarm89 Apr 02 '16 at 15:31
0

the The CORS header must be added to target of your request. So if the Spring Boot application invokes a service outside its context, you'll need to add CORS support to that service. The way you do this depends on the platform that runs your service.

On AWS for example, you can set CORS support for an S3 website in the settings.

If you have control over the other service and it's a Spring Boot application, then you can implement what's suggested in the link only for the other service.

Marco Tedone
  • 592
  • 4
  • 13
0

You can use @crossorigin annotation to accept all request headers.