如何在 Web 服务上下文中存储信息?

发布于 2024-10-27 17:44:33 字数 727 浏览 5 评论 0原文

我使用负责用户登录的网络服务。如果登录成功,应该生成一个令牌。

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/login")
public String login(@QueryParam("userName") String name,
        @QueryParam("password") String password) {
            //Spring Securtity Check
    HttpResponse r =loginResponse(name,password);
    String s = r.getFirstHeader("Location").toString();
    boolean isError = s.contains("login_error");
    if(!isError){
        //TODO store Token in the application context 

        MD5 token = new MD5(name+System.currentTimeMillis());
        return "token:"+token.getMD5();
    }
    return "fail";
}

我想将令牌存储在应用程序上下文中,但我不知道如何存储。只要服务器应用程序正在运行,令牌就应该存在。 Web 服务是否有自己的应用程序上下文?我应该使用某种 HTTP servlet 来存储信息吗?

I use a web service which is responsible for user logins. If a login is successful, a token should be generated.

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/login")
public String login(@QueryParam("userName") String name,
        @QueryParam("password") String password) {
            //Spring Securtity Check
    HttpResponse r =loginResponse(name,password);
    String s = r.getFirstHeader("Location").toString();
    boolean isError = s.contains("login_error");
    if(!isError){
        //TODO store Token in the application context 

        MD5 token = new MD5(name+System.currentTimeMillis());
        return "token:"+token.getMD5();
    }
    return "fail";
}

I would like to store the token in the application context, but I don't know how. The token should exist as long as the server application is running. Does the web service have its own application context? Should I use some kind of HTTP servlet to store the information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗失的美好 2024-11-03 17:44:33

将其存储在 memcached 中,使用它您可以应用一些过期策略,并且当您有超过一台服务器,存储在本地内存中会有问题,存储在全局缓存中,比如memcached更合适。

store it in memcached, using it you can apply some expiration policy, and also when you have more than one server, it will be an problem to store it in the local memory, store it in global cache like memcached is more apropariate.

东风软 2024-11-03 17:44:33

我不太明白你所说的应用程序上下文。

是ServletContext吗?您可以在 Jersey 中使用 @Context 注释来获取它:@Context ServletContext。您可以将 is 作为资源中的字段或作为方法的参数。
ServletContext 已启动,而 servlet 已启动。根据其配置,它可能会被 servlet 容器删除。

顺便提一句。你的设计真的很糟糕而且不安全。您使用 GET 进行登录操作,并在 url 上传递用户名和密码。这意味着几件事:

  1. GET 请求可以被中介缓存。你希望它发生吗?
  2. 每个人都会在网址中看到密码。即使您使用 SSL,密码仍然会在 url 中,每个人都可以看到。
  3. URL 经常被客户端、服务器和中介记录。您真的、真的、真的不希望密码被记录。

我对你的问题进行了投票,因为这是登录设计不良的一个很好的例子。

I don't quite understand what you call the application context.

Is it ServletContext? You can get it in Jersey using the @Context annotation: @Context ServletContext. You can get is either as a field in your resource, or as a parameter to your method.
The ServletContext is up, while servlet is up. It may be removed by the servlet container depending on its configuration.

Btw. your design is really really bad and insecure. You use GET for login operation and pass both username and password on the url. This means few things:

  1. GET requests can be cached by intermediaries. Do you want it to happen?
  2. Everybody will see password in url. Even if you use SSL, password will be still in url, seen to everyone.
  3. URL is often logged, both by client, servers and intermediaries. You really, really, really don't want the password to be logged.

I'm voting your question up, since it's a great example of a bad design for login.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文