简体   繁体   中英

How to make my JAX-RS webservice remember my login?

I created a very simple REST webservice in Java by using JAX-RS. And have a client make Ajax calls to that webservice to login and get the login information.

JAX-RS Code

@Path("/netsuite")
public class myRESTWebService{
   @GET
   @Path("/login/{userName}")
   public String login(@PathParam("userName") String userName){
      //here I have to save that userName in some session so that I can use it in below function
   }


   @GET
   @Path("/getUserName")
   public String getUserName(){
      //here I have to return the above username
   }  
}

I know REST webservices are stateless, how can I make it stateful. I tried Google search whole day, didn't helped me much.

How can I make it stateful ?

You can access the HttpSession and store the username like this:

@GET
@Path("/login/{userName}")
public String login(@PathParam("userName") String userName, @Context HttpServletRequest servletRequest) {
    HttpSession session = request.getSession();
    session.setAttribute("userName", userName);
}

But I strongly recommend to rethink why you need state in a stateless application and take a look at Java EE Security Concepts .

Unrelated: Classes in Java always start with a capital Letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM