Yes, it's legal to do so. You can even flip them off. Just as long as you don't make any physical contact with one, whatsoever. If you do, they're going to arrest you. Not exactly sure what you'll be charged with. But ANY type of touch or physical contact is illegal. And yes, spitting on an officer counts as touching.
Also, if an officer tells you to move on after you've cussed him out. DO IT! And, do it immediately. Otherwise he can definitely arrest you for that.
Several years back, had a run-in with a young NYPD officer near a barricade. Was having a conversation with a middle-aged Black man who asked me which streets were open. This was on December 31st, late in the morning. Was telling him to avoid a certain street, even though it was open; due to a particularly obnoxious NYPD detective with a chip on his shoulder. As I described the detective, the young, uniformed officer angerly butted in. He was upset that I had described the detective as a Black man, instead of as African-American. The young officer, happened to be White. Keep in mind, the middle-aged Black man I had been speaking to, didn't get offended one bit.
I cussed that particular young officer out. He deserved it. Absolutely. Basically told him he was a miserable disgrace to that uniform, and how he shouldn't treat it like a late-year Halloween costume. How he should carry himself in a professional manner, with some dignity and pride. Yeah, I was born with a serpent's tongue. I choose not to use it 99.9% of the time. But I can cut someone down on the spot if they deserve it. He deserved it. After awhile, he told me to just move along. I told him that was the first intelligent thing he had said during our conversation, and I did so. Just disengaged. He was at the barricade with another young, White, officer. His buddy next to him didn't say a word that whole time. Didn't have his back. Knew his "partner" was being disgustingly unprofessional, and just stayed out of it.