2014-11-11 7 views
0

저는 안드로이드 개발에 상당히 익숙하며 이미지를 업로드하기 위해 HTTP 서버를 호출해야합니다. 그렇게하기 전에 사용자는 사용자 이름과 암호로 로그인하고 사용자 ID와 함께 JWT 토큰이 리턴됩니다. 이미지 업로드 기능에서는 서버가 세션을 확인한 후 이미지를 받아들이도록 토큰과 ID를 제공해야합니다. 그 위치에서 문제가 발생합니다.안드로이드 애플 리케이션의 JWT 인증

이미지 업로드 또는 세션 유효성 검사에 문제가 있는지는 잘 모르겠지만 오류 메시지에서 두 번째 것으로 보입니다. (마지막에서 봐 주시기 바랍니다) 주어진

public String sendPostMult(String myurl, String token, List<ValuePair> params, Bitmap bm) throws Exception { 

    try { 

     HttpClient client = new DefaultHttpClient(); 

     HttpPost post = new HttpPost(myurl); 
     post.addHeader("Authorization", "Token token=\"" + token + "\""); 
     MultipartEntityBuilder postEntity = MultipartEntityBuilder.create(); 

     ByteArrayOutputStream bao = new ByteArrayOutputStream(); 

     bm.compress(Bitmap.CompressFormat.PNG, 90, bao); 

     byte[] ba = bao.toByteArray(); 

     String ba1 = Base64.encodeToString(ba, Base64.DEFAULT); 

     // File file = new File("Your File path on SD card"); 
     // postEntity.addPart("fileupload", new FileBody(file, "image/jpeg")); 
     for (ValuePair param : params) { 
      postEntity.addTextBody(param.getValue(), param.getName()); 
      Log.d(param.getValue(), param.getName()); 
     } 

     postEntity.addTextBody("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"); 


     postEntity.addTextBody("client_assertion", token); 

     postEntity.addTextBody("picture", ba1); 
     // Log.d("YY", getBytesFromBitmap(bm).toString()); 
     post.setEntity(postEntity.build()); 

     HttpResponse response = client.execute(post); 

     int responseCode = response.getStatusLine().getStatusCode(); 


     BufferedReader reader = new BufferedReader(new InputStreamReader(
       response.getEntity().getContent(), "UTF-8")); 

     String sResponse = reader.readLine(); 

     return sResponse; 

    }catch(Exception e) 
    { 
     e.printStackTrace(); 
     return "ERROR"; 
    } 
} 

오류는 다음과 같습니다 :

기능은 다음은

11-11 10:51:09.780 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - NafHttpAuthStrategyDefault﹕ (thUse=686) NafHttpAuthStrategyDefault() 

11-11 10:51:09.780 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - KeeperManager﹕ (thUse=686) INITIALIZATION of shared resources 

11-11 10:51:09.780 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - AndroidContextProviderImpl﹕ (thUse=686) [email protected] 
11-11 10:51:09.830 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - GbaSupportIndicatorRequestUpdaterDefault﹕ (thUse=686) GbaSupportIndicatorRequestUpdaterAbstract() userHeaderPredefined=Apache-HttpClient/UNAVAILABLE (java 1.4) 

11-11 10:51:09.940 11762-11769/team13.foods I/dalvikvm﹕ Total arena pages for JIT: 11 
11-11 10:51:09.940 11762-11769/team13.foods I/dalvikvm﹕ Total arena pages for JIT: 12 
11-11 10:51:09.940 11762-11769/team13.foods I/dalvikvm﹕ Total arena pages for JIT: 13 
11-11 10:51:09.940 11762-11769/team13.foods I/dalvikvm﹕ Total arena pages for JIT: 14 
11-11 10:51:09.950 11762-11769/team13.foods I/dalvikvm﹕ Total arena pages for JIT: 15 

11-11 10:51:09.960 11762-12135/team13.foods D/dalvikvm﹕ DexOpt: couldn't find static field Lorg/apache/http/message/BasicHeaderValueParser;.INSTANCE 

11-11 10:51:09.960 11762-12135/team13.foods W/dalvikvm﹕ VFY: unable to resolve static field 1921 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueParser; 

11-11 10:51:09.960 11762-12135/team13.foods D/dalvikvm﹕ VFY: replacing opcode 0x62 at 0x001b 

11-11 10:51:09.960 11762-12135/team13.foods D/dalvikvm﹕ DexOpt: couldn't find static field Lorg/apache/http/message/BasicHeaderValueFormatter;.INSTANCE 

11-11 10:51:09.960 11762-12135/team13.foods W/dalvikvm﹕ VFY: unable to resolve static field 1915 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueFormatter; 

11-11 10:51:09.960 11762-12135/team13.foods D/dalvikvm﹕ VFY: replacing opcode 0x62 at 0x0015 
11-11 10:51:09.970 11762-12135/team13.foods D/name﹕ djt 
11-11 10:51:09.970 11762-12135/team13.foods D/description﹕ test 
11-11 10:51:09.970 11762-12135/team13.foods D/tags﹕ hft 
11-11 10:51:09.970 11762-12135/team13.foods D/id﹕ 545a7b69ed2f9c0200dfc995 
11-11 10:51:10.000 11762-12135/team13.foods D/dalvikvm﹕ GC_FOR_ALLOC freed 799K, 16% free 7878K/9336K, paused 27ms, total 27ms 

11-11 10:51:10.020 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - NafHttpAuthStrategyDefault﹕ (thUse=686) cached value : gbaSupportIsPossible=null 

11-11 10:51:10.020 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - NafHttpAuthStrategyDefault﹕ (thUse=686) The current context is NOT a context of GBA service. 

11-11 10:51:10.020 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - GbaSupportPermissionRequestCheckerImpl﹕ (thUse=686) isCurrentProcessRequestedGba()#finished result=false 

11-11 10:51:10.020 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - GbaSupportPermissionRequestCheckerImpl﹕ (thUse=686) isCurrentProcessAllowedToUseGba()#started result=false 

11-11 10:51:10.020 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - NafHttpAuthStrategyDefault﹕ (thUse=686) The GBA permission wasn't requested for this process. 

11-11 10:51:10.020 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - NafHttpAuthStrategyDefault﹕ (thUse=686) It is impossible to support GBA now (many possible reasons: no Android Context, current client is GBA service, etc.), then it will be just usual HTTP. 

11-11 10:51:10.020 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - NafRequestExecutorWrapperRedirectionHandler﹕ (thUse=686) It isn't GBA flow, redirection responses are not handled. 

11-11 10:51:11.280 11762-12135/team13.foods W/DefaultRequestDirector﹕ Authentication error: Unable to respond to any of these challenges: {} 

11-11 10:51:11.280 11762-12135/team13.foods I/APACHE HTTP (thCr=686) - NafHttpAuthStrategyDefault﹕ (thUse=686) cached value : gbaSupportIsPossible=false 

11-11 10:51:11.280 11762-11762/team13.foods I/System.out﹕ UnauthorizedError: jwt must be provided 

그래서 참으로 JWT 것 같다 오류. 나는

post.addHeader("Authorization", "Token token=\"" + token + "\""); 

라인을 제거하면 오류가 말한다 : 인증 헤더가 제공되어야합니다.

그래서 제 질문은 다음과 같습니다

1) 인증 헤더가 맞습니까? 아니면 다른 형식이어야합니까?

2) JWT를 어떻게 통과합니까? 나는 문자열로서 토큰을 받았고, 나는 그것을 전달할 필요가 있다고 생각했습니다. 그러나 내가 토큰을 받았을 때, 다른 문자열로 파싱 된 몇 개의 필드가 포함되어 있었고 토큰은 그 중 하나였습니다. 어쩌면 전체 내용을 저장하고 클라이언트 주장 필드에 모두 보내야합니다. 그런데도 인증 헤더에서만 토큰을 보내야합니까?

3) 약간의 관련이 없지만 잘못된 형식으로 이미지를 전송할 수 있습니까?

감사합니다. 정말 감사!

+0

당신은 JWT가 어떻게 생겼는지 궁금 같은 : { "토큰": "eyJ0eXAiOiJKV1QiLCJhbGiOiJIUzI1NiJ9.eyJlbWFpbCI6Im9saUBvbGkuY29tIiwicGFzc3dvcmQiOiIkMmEkMDUkZEVpYmhRUzlNLlMwRW9wTU5XWndrLi5rR1VIMWNFUk9HaW1mMDRYcFpnQWdtSTBBbTFPRk8iLCJfaWQiOiI1NDVhN2I2OWVkMmY5YzAyMDBkZmM5OTUiLCJfX3YiOjB9.voPftTnS4Wfg_S6_87N6DUpBxCcu9S_dyJpUj_rsxlk", "ID": "545a7b69ed2f9c0200dfc995"} –

답변

2

JWT를 보면 문제가 바로 있습니다. JWT 토큰이 아닙니다.

JWT는 세 부분으로 구성됩니다 : <header>.<claims/payload>.<signature>. 내 주소는 <payload>.<signature>입니다. JWS (JSON 웹 서명)가 아니라 JWT (JSON 웹 토큰)가있는 것 같습니다.

당신은 사양의 형식에 대해 읽을 수 있습니다 : http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#ExampleJWT

이이 유용한 도구입니다 토큰의 문제를 해결해야하는 경우도 http://kjur.github.io/jsjws/mobile/tool_jwt.html

, 당신처럼 주장에 암호를 보내는 것은 것 같아 거꾸로 할 일은 암호화되어 있어도 마찬가지입니다. JWT는 각 서비스에 암호를 보내는 대신입니다. 서비스는 서명의 유효성을 검사하여 콘텐츠를 신뢰합니다. 경우