简体   繁体   English

在REST服务中启用身份验证

[英]Keep Authentication enable in REST services

So the problem is: I've developed REST services in Jersey, that run on Glassfish. 所以问题是:我在Jersey上开发了在Glassfish上运行的REST服务。 For authentication, I implemented Basic-Authentication . 对于身份验证,我实现了Basic-Authentication On the client side, I implemented Authentication through ApacheHTTPClient . 在客户端,我通过ApacheHTTPClient实现了Authentication。

My idea is just to ask for authentication when a registered user enters - like a Login. 我的想法是在注册用户进入时(例如登录名)要求进行身份验证。 Is that configured in the Client Application (to keep the authentication valid until the user Logout), or in the REST services, where I configured the Basic-Authentication? 是在客户端应用程序中配置的(在用户注销之前保持身份验证有效),还是在配置了基本身份验证的REST服务中进行了配置?

Thanks! 谢谢!


This is how I'm doing the Login on the Client App: 这是我在客户端应用程序上进行登录的方式:

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

public class UserLogin {

    private static final String BASE_URI = "http://localhost:8080/LULServices/webresources";

    public static void main(String[] args) throws Exception {

    final DefaultHttpClient httpclient = new DefaultHttpClient();

    try {
            httpclient.getCredentialsProvider().setCredentials(
                new AuthScope("localhost", 8080),
                new UsernamePasswordCredentials("zzzzz", "xxxxx"));

    HttpPut httpPut = new HttpPut(BASE_URI + "/services.users/login");
    HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000);

    httpPut.addHeader("Content-type", "multipart/form-data");

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("login", "zzzzz"));
    nameValuePairs.add(new BasicNameValuePair("password","xxxxx"));

    httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httpPut);

    try {
            System.out.println("Executing request " + httpPut.getRequestLine());
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println("HTTP Status: " + response.getStatusLine());

            String putResponse = EntityUtils.toString(entity);
            System.out.println(putResponse);
            EntityUtils.consume(entity);

        } finally
            httpPut.releaseConnection();

        } finally
            httpclient.getConnectionManager().shutdown();
    }
}

It returns to the user his secret_id. 它将用户的secret_id返回给用户。

As mentioned by Darrel Miller , REST based services should be stateless. Darrel Miller所述 ,基于REST的服务应该是无状态的。 But to help you solve your current issue, I would suggest to use an auth token and a refresh policy. 但是为了帮助您解决当前的问题,我建议您使用身份验证令牌和刷新策略。

Description: After every successful authentication your server can return a unique 27[any length you want] digit string. 描述:每次成功认证后,您的服务器都可以返回一个唯一的 27 [任意长度]数字字符串。 This token may or may not have a expiry policy[depends on what you want]. 该令牌可能有也可能没有到期政策[取决于您想要的内容]。 So for subsequent authentications [when the client application has an auth token]you can actually provide a new auth token and invalidate the previous one. 因此,对于后续身份验证(当客户端应用程序具有auth令牌时),您实际上可以提供新的auth令牌并使前一个令牌无效。

Additionally for every other API call you can send this auth token to validate whether the request is from an authenticated source or not. 此外,对于其他所有API调用,您都可以发送此身份验证令牌,以验证请求是否来自经过身份验证的来源。 Now when the user logs out of the application you can simply remove the auth token from the client side. 现在,当用户注销应用程序时,您只需从客户端删除身份验证令牌即可。

Next time when the user returns to the application, the app will not have a auth token and can be redirected to the login screen. 下次用户返回到应用程序时,该应用程序将没有身份验证令牌,并且可以重定向到登录屏幕。

REST based services should be stateless. 基于REST的服务应该是无状态的。 Ideally, there should be no notion of login on the server. 理想情况下,服务器上应该没有登录概念。 You can simulate login/logoff on the client by deciding whether to send the authn header or not. 您可以通过决定是否发送authn标头来在客户端上模拟登录/注销。

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

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