android - 无法将android中的POST请求发送到Grails后端

标签 android spring security grails

我在使用Grails和SpringSecurity进行后端发送POST时遇到了一些问题。
我不知道为什么,但是当我发送ROLE_ADMIN方法时,它不起作用。我尝试对用户和密码进行编码,然后将其设置在请求 header 上,并尝试相同但不进行编码。
我真的开始对这个问题感到绝望。
这是我的POST代码:

public String POST2(String targetURL, String urlParameters, String user,
            String clave) {
        URL url;
        String h = "http://";
        String u = h+targetURL;

        HttpURLConnection connection = null;
        try { // Create connection //
            // targetURL = URLEncoder.encode(targetURL, "UTF-8");
            url = new URL(u);
            connection = (HttpURLConnection) url.openConnection(); // cambiarlo
                                                                    // luego al
                                                                    // usuario q
            String login = user + ":" + clave;
            String encoding = new String(org.apache.commons.codec.binary.Base64 //
                    .encodeBase64(org.apache.commons.codec.binary.StringUtils //
                            .getBytesUtf8(login)));

            connection.setRequestMethod("POST");
            connection.setRequestProperty("Authorization", "Basic " + login);
            connection.setRequestProperty("Content-Type", "plain/text");// hace
                                                                        // // q
            // sirva // con // el // string // de // json

            connection.setRequestProperty("Content-Length",
                    "" + Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setReadTimeout(120000); // Send
            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            // Get Response 
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            this.setResponseCode(connection.getResponseCode());
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            return response.toString();

        } catch (Exception e) {

            e.printStackTrace();
            return null;

        } finally {

            if (connection != null) {
                connection.disconnect();
            }
        }
    }

这就是我所说的:
private class EntrenadorExecutioner extends AsyncTask<String, Void, String> {
        private EntrenadorActivity activity = null;

        public EntrenadorExecutioner(EntrenadorActivity activity) {
            attach(activity);
        }

        private void attach(EntrenadorActivity activity) {
            this.activity = activity;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            activity.startLoader();
        }

        @Override
        protected String doInBackground(String... params) {
            String json = "";

            JSONObject jsonObject = new JSONObject();

            try {
                jsonObject.accumulate("cedula", params[0]);
                jsonObject.accumulate("nombre", params[1]);
                jsonObject.accumulate("primerApellido", params[2]);
                jsonObject.accumulate("segundoApellido", params[3]);

                json = jsonObject.toString();
            } catch (JSONException je) {
                je.printStackTrace();
            }

            return rh.POST2(Constantes.GUARDAR_ENTRENADOR, json,
                    Constantes.user.getUsername(),
                    Constantes.user.getPassword());
        }

        @Override
        protected void onPostExecute(String response) {
            activity.markAsDone();
            if (response.equals("")) {
                // Refresh the list view then show success
                Toast.makeText(getApplicationContext(),
                        getString(R.string.successEntre), Toast.LENGTH_SHORT)
                        .show();
            } else
                Toast.makeText(getApplicationContext(),
                        getString(R.string.err_unexp), Toast.LENGTH_LONG)
                        .show();
        }
    }

最佳答案

我找到了解决方案!
我需要做的就是使用Grails用户 token ,并将其添加到 header 中,如下所示:

connection.setRequestProperty("Authorization", "Bearer " + Constantes.user.getToken()); // where get token returns the auth token

关于android - 无法将android中的POST请求发送到Grails后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29261805/

相关文章:

spring - 如何在 Spring 4 STOMP over WebSocket 配置中回复未经身份验证的用户?

jquery - 阻止对 API 的外部调用(安全)

c# - 防止 SSL 密码

java - 这是 http ://www. springframework.org/schema/util 的 Spring JAR

java - 检查加载的 ADF 中的区域是否已本地化

java - 如何删除框架布局/视频 View 中的自动对焦

android - 如何检索 View 的背景颜色并将其与颜色资源进行比较

spring rest api 版本控制

android - 在Android中录制并保存音频,其他应用程序不应访问该音频?

java - Android中的时间粒度是多少?