简体   繁体   English

如何使我的JAX-RS Web服务记住我的登录信息?

[英]How to make my JAX-RS webservice remember my login?

I created a very simple REST webservice in Java by using JAX-RS. 我使用JAX-RS在Java中创建了一个非常简单的REST Web服务。 And have a client make Ajax calls to that webservice to login and get the login information. 并让客户端向该Web服务发出Ajax调用以登录并获取登录信息。

JAX-RS Code JAX-RS代码

@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. 我知道REST Web服务是无状态的,我如何使其具有状态。 I tried Google search whole day, didn't helped me much. 我整天尝试使用Google搜索,但对我没有太大帮助。

How can I make it stateful ? 我怎样才能使它有状态?

You can access the HttpSession and store the username like this: 您可以访问HttpSession并存储用户名,如下所示:

@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 . 但是我强烈建议重新考虑为什么您需要在无状态应用程序中使用状态,并了解一下Java EE安全概念

Unrelated: Classes in Java always start with a capital Letter. 无关:Java中的类始终以大写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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