admin管理员组

文章数量:1414892

i have a huge problem with CORS on my server. My server is running on Tomcat on localhost. The server i implemented has a couple of RestFul resources. For example:

@Path("user")
public class UserService {
@GET
@Path("/{username}/{password}")
@Produces("text/plain")
public String checkLoginParameter(@PathParam("username") String username, @PathParam("password") String password) {
        String tempUserName = "";
        String tempPassword = "";
        String bool = "";

        EntityManager em = createEntityManager();
        try{
            User user = (User)em.createQuery("SELECT u FROM User u WHERE u.username='"+username+"' AND u.password='"+password+"'").getSingleResult();
            tempUserName = user.getUsername();
            tempPassword = user.getPassword();
        }
        catch(Exception e){}

        if(username.equals(tempUserName) && !username.isEmpty() && password.equals(tempPassword) && !password.isEmpty()){
            bool = "true";
        }
        else{
            bool = "false";
        }
        return bool;
   }      
}

The Ressource is working. Now i want to access to the Ressource via JavaScript from a different Website. When i try to do this, i get a Error message:

XMLHttpRequest cannot load http://localhost:8080/RSAppStore/user/poc/poc. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I know i have add to my Server the Permission for CORS. I do it this way:

web.xml:

<filter>
<filter-name>CORS</filter-name>
<filter-class>.thetransactionpany.cors.CORSFilter</filter-class>
<init-param>
 <param-name>cors.allowOrigin</param-name>
    <param-value>*</param-value>
</init-param>
<init-param>
 <param-name>cors.supportedMethods</param-name>
    <param-value>GET, POST, HEAD, PUT, DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
    <param-name>cors.exposedHeaders</param-name>
    <param-value>Set-Cookie</param-value>
</init-param>
<init-param>
    <param-name>cors.supportsCredentials</param-name>
    <param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

To access with JavaScript:

<script type="text/javascript">
$(function(){
  $.ajax({
    type: "GET",
    crossDomain: true,
    url: "http://robosmart-appstore:robosmart@localhost:8080/RSAppStore/user/poc/poc",

    contentType: "text/plain",
    success: function( string ) {

      console.log(string);  


    },
    error:function () {
      console.log("err");
    }
  });
});
</script>

In addition i have added these 2 jars.

cors-filter-1.9.2.jar
java-property-utils-1.9.jar

I read so many examples and explanations on the Internet, but nothing seems to work and i don't know what i am doing wrong.

i have a huge problem with CORS on my server. My server is running on Tomcat on localhost. The server i implemented has a couple of RestFul resources. For example:

@Path("user")
public class UserService {
@GET
@Path("/{username}/{password}")
@Produces("text/plain")
public String checkLoginParameter(@PathParam("username") String username, @PathParam("password") String password) {
        String tempUserName = "";
        String tempPassword = "";
        String bool = "";

        EntityManager em = createEntityManager();
        try{
            User user = (User)em.createQuery("SELECT u FROM User u WHERE u.username='"+username+"' AND u.password='"+password+"'").getSingleResult();
            tempUserName = user.getUsername();
            tempPassword = user.getPassword();
        }
        catch(Exception e){}

        if(username.equals(tempUserName) && !username.isEmpty() && password.equals(tempPassword) && !password.isEmpty()){
            bool = "true";
        }
        else{
            bool = "false";
        }
        return bool;
   }      
}

The Ressource is working. Now i want to access to the Ressource via JavaScript from a different Website. When i try to do this, i get a Error message:

XMLHttpRequest cannot load http://localhost:8080/RSAppStore/user/poc/poc. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I know i have add to my Server the Permission for CORS. I do it this way:

web.xml:

<filter>
<filter-name>CORS</filter-name>
<filter-class>.thetransactionpany.cors.CORSFilter</filter-class>
<init-param>
 <param-name>cors.allowOrigin</param-name>
    <param-value>*</param-value>
</init-param>
<init-param>
 <param-name>cors.supportedMethods</param-name>
    <param-value>GET, POST, HEAD, PUT, DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
    <param-name>cors.exposedHeaders</param-name>
    <param-value>Set-Cookie</param-value>
</init-param>
<init-param>
    <param-name>cors.supportsCredentials</param-name>
    <param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

To access with JavaScript:

<script type="text/javascript">
$(function(){
  $.ajax({
    type: "GET",
    crossDomain: true,
    url: "http://robosmart-appstore:robosmart@localhost:8080/RSAppStore/user/poc/poc",

    contentType: "text/plain",
    success: function( string ) {

      console.log(string);  


    },
    error:function () {
      console.log("err");
    }
  });
});
</script>

In addition i have added these 2 jars.

cors-filter-1.9.2.jar
java-property-utils-1.9.jar

I read so many examples and explanations on the Internet, but nothing seems to work and i don't know what i am doing wrong.

Share Improve this question edited Jun 28, 2014 at 15:26 Sam 7,40816 gold badges48 silver badges68 bronze badges asked Apr 28, 2014 at 13:51 A. JanikA. Janik 491 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I just succeed in passing CORS with tomcat and angular.js.

There's something you may need to configure:

  1. Change cors.supportedHeaders to this:

    <init-param>
        <param-name>cors.supportedHeaders</param-name>
        <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>
    </init-param>
    
  2. Make sure your filter is before any other filters.

  3. Try to change contentType to this:

    contentType: "application/x-www-form-urlencoded; charset=utf-8"
    

Try it man.

本文标签: httpTomcat quotAccessControlAllowOriginquot and JavaScriptStack Overflow