简体   繁体   English

HttpUrlConnection-Android应用-带有Spring Web Service的oauth2授权

[英]HttpUrlConnection - android app - oauth2 authorization with spring web service

I try to connect with my spring rest service from android app. 我尝试从android应用程序连接我的spring rest服务。 My code doesn't work. 我的代码不起作用。 I learn from the following tutorial: http://websystique.com/spring-security/secure-spring-rest-api-using-oauth2/ 我从以下教程中学习: http : //websystique.com/spring-security/secure-spring-rest-api-using-oauth2/

and I try to do this point: 我尝试做到这一点:

"Ask for tokens[access+refresh] using HTTP POST on /oauth/token, with grant_type=password,and resource owners credentials as req-params. Additionally, send client credentials in Authorization header. POST http://localhost:8080/SpringSecurityOAuth2Example/oauth/token?grant_type=password&username=bill&password=abc123 " “使用/ oauth / token上的HTTP POST,使用grant_type = password,并使用资源所有者凭据作为req-params,请求令牌[access + refresh]。此外,在Authorization标头中发送客户端凭据。POST http:// localhost:8080 / SpringSecurityOAuth2Example / oauth / token?grant_type = password&username = bill&password = abc123

I tried to do it in the other way, not like in the tutorial but using HttpURLConnection. 我试图用另一种方式来做到这一点,而不是像本教程中那样,而是使用HttpURLConnection。 How should look parameters to make appropriate request? 应该如何看待参数以提出适当的要求? I simulate it on my phone and it has the connection with local network by wifi. 我在手机上模拟了它,并通过wifi与本地网络建立了连接。

try {
        String plainClientCredentials="my-trusted-client:secret";
        String base64ClientCredentials = new String(Base64.encodeBase64(plainClientCredentials.getBytes()));
        URL urlObj = new URL("http://192.168.1.150:8080/Web_Maxim_Manager/oauth/token");
        HttpURLConnection urlConn = (HttpURLConnection) urlObj.openConnection();
        urlConn.setDoOutput(true);
        urlConn.setDoInput(true);
        urlConn.setRequestProperty("Content-Type", "application/json");
        urlConn.setRequestProperty("Accept", "application/json");
        urlConn.setRequestMethod("POST");
        urlConn.setRequestProperty("grant_type", "password");
        urlConn.setRequestProperty("username", "bill");
        urlConn.setRequestProperty("password", "abc123");
        urlConn.setRequestProperty("Authorization", "Basic "+base64ClientCredentials);
        urlConn.connect();
        String response = urlConn.getResponseMessage();
        userName.setText(response);
    } catch (Exception e) {
        userName.setText("uuuu");
        e.printStackTrace();
    }


     W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:623)
W/System.err:     at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
W/System.err:     at java.net.Socket.checkOpenAndCreate(Socket.java:687)
W/System.err:     at java.net.Socket.setSoTimeout(Socket.java:541)
W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:1179)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:392)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:295)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
W/System.err:     at runwithme.android.maxim.com.myapplication.activities.MainActivity.onCreate(MainActivity.java:43)

I tested service with postman apps and it works. 我使用邮递员应用程序测试了服务,并且可以正常工作。 So, the fault is in my android app. 因此,故障出在我的Android应用程序中。

Solved. 解决了。 Problem was in the faulty set application permissions. 问题出在错误的设置应用程序权限上。 Additional thing is that: 另一件事是:

    urlConn.setRequestProperty("grant_type", "password");
    urlConn.setRequestProperty("username", "bill");
    urlConn.setRequestProperty("password", "abc123");

should not be set as a request parameters, but they should be part of url. 不应将其设置为请求参数,但应将其作为url的一部分。 Permissions should be set as: 权限应设置为:

<uses-permission android:name="android.permission.INTERNET"/>

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

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