在Java中使用Cookie维护会话

时间:2019-04-22 08:54:28

标签: java cookies oauth

我有以下代码,该代码获取访问令牌,然后验证是否将请求传递给服务器。

但是我想要实现的是使用cookie维护会话。 我尝试了几件事,但它弄乱了服务器的响应。

在这种情况下,您如何使用Cookie,

public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
        if(request.header("Authorization") == null) {
            logger.info("YOU ARE NOT AUTHORIZED");
            RestResponse response = new BytesRestResponse(RestStatus.UNAUTHORIZED, "Access denied");
            channel.sendResponse(response);
        }
       else {
        try {
            String access_token=request.header("Authorization");
            access_token=access_token.substring(7);
            logger.info("ACCESS TOKEN = "+ access_token);
            URL obj=new URL("validation url");
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Connection", "Keep-Alive");
            int responseCode = con.getResponseCode();
            logger.info("THE RESPONSE CODE IS" + responseCode);
            if(responseCode==200) {   //here i am validating
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                write_logs(in);
                dispatchRequest_assist(request,channel,threadContext);
            }
            else {
                logger.info("YOU ARE NOT AUTHORIZED");
                RestResponse response = new BytesRestResponse(RestStatus.UNAUTHORIZED, "Access denied");
                channel.sendResponse(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }

0 个答案:

没有答案