无法在服务器端(Jersey)创建的角度js获取cookie

时间:2015-10-06 07:00:35

标签: javascript angularjs cookies jersey jax-rs

我有一个在服务器端创建的cookie(Rest Service)。 下面是创建cookie的休息服务:

        @Path("token")
        public class AuthService {

        @POST
        @Produces(MediaType.APPLICATION_JSON)
        public Response generateToken(@Context HttpServletRequest httpServletRequest) {

        //some code to get serializedJwt and xsrftoken. path is "/" and domain is "http://localhost"

        Cookie jwtCookie = new Cookie("jwt", serializedJwt, path, domain);
        Cookie xsrfCookie = new Cookie("X-XSRF-TOKEN", xsrfToken, path, domain);

        NewCookie newJwtCookie = new NewCookie(jwtCookie, null, maxAge, false);
        NewCookie newXsrfCookie = new NewCookie(xsrfCookie, null, maxAge, false);

        return Response.status(SUCCESSFUL_REQUEST)
        .header(ERROR_HEADER_NAME, SUCCESS)
        .header("SET-COOKIE", newJwtCookie.toString()+" ; HttpOnly")
        .header("SET-COOKIE", newXsrfCookie.toString())
        .entity(MAPPER.writeValueAsString(responseBody)).build();
        }
        }

现在我试图从角度js(v1.4.6)应用程序中恢复此cookie。

        console.log($cookies.get("X-XSRF-TOKEN")); //prints undefined
        $cookies.put('abc',"kishore"); //just for testing purpose
        console.log($cookies.get("abc")); //this prints kishore
        console.log(document.cookie); //this prints "abc=kishore"

注意: 对于X-XSRF-TOKEN,httpOnly为false。

1 个答案:

答案 0 :(得分:0)

我删除了domain = http://localhost

后问题得到解决
   Hope this wil help u
  In your web view layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#c7bbac" >

   <ImageView
    android:id="@+id/txtmain"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/topbar50" />
<ImageView
    android:id="@+id/backbutn"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:adjustViewBounds="true"
    android:paddingTop="2dp"
    android:src="@drawable/backbtn" />
  </RelativeLayout>

 <WebView  

 android:id="@+id/webView1"
 android:layout_below="@+id/relay"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 />
 </RelativeLayout>

  Webview Button Onclick:
   webbutton=(ImageView)findViewById(R.id.web);
    webbutton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            Intent intent = new Intent(getApplicationContext(),       WebViewActivity.class);
            startActivity(intent);
          }

        });

   Webview Activity:

    public class WebViewActivity extends Activity {

   private WebView webViewurl;
   ImageView back;


  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    back=(ImageView)findViewById(R.id.backbutn);
    webViewurl = (WebView) findViewById(R.id.webView1);
    webViewurl.getSettings().setJavaScriptEnabled(true);
    webViewurl.getSettings().setBuiltInZoomControls(true);
    final Activity activity = this;
    webViewurl.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String   description, String failingUrl) {
               Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });
    webViewurl.loadUrl("http://Your url");
    back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
         }
      });

     }
   }