JWTを使用して私のサーバで認証するシンプルなUnityスクリプトを作成しようとしています。残念ながら、JWTの.NETプラグインを使用することはできません。Unityのものよりも新しいバージョンの.NETが必要です(Monoのこと)。だから私はそれを自分で書くようにしました。私はログインを実行することができますが、後でidTokenを使用する方法を考えることができません。UnityスクリプトからのJWT認証
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
public class JsonLoaderTest : MonoBehaviour
{
public static string BASE_HTTP_URL = "http://localhost:8080/";
public static string BASE_HTTPS_URL = "https://localhost:8080/";
private string _idToken = "";
// Use this for initialization
[UsedImplicitly]
IEnumerator Start() {
yield return StartCoroutine(GetBeers());
yield return StartCoroutine(Login());
yield return StartCoroutine(GetBeers());
}
private IEnumerator GetBeers()
{
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Authorization", "Bearer " + _idToken);
WWW www = new WWW(BASE_HTTP_URL + "api/beers", null, headers);
while (!www.isDone) yield return null;
Debug.Log(www.text);
}
public class LoginPackage
{
public string username;
public string password;
public bool rememberMe;
}
public class IdTokenPackage
{
public string idToken;
}
private IEnumerator Login()
{
LoginPackage loginPackage = new LoginPackage();
loginPackage.username = "admin";
loginPackage.password = "admin";
loginPackage.rememberMe = true;
Dictionary<string, string> postHeaders = new Dictionary<string, string>();
postHeaders.Add("Content-Type", "application/json");
string json = JsonUtility.ToJson(loginPackage);
byte[] postData = System.Text.Encoding.UTF8.GetBytes(json);
WWW www = new WWW(BASE_HTTP_URL + "api/authenticate", postData, postHeaders);
while (!www.isDone) yield return null;
Debug.Log(www.text);
_idToken = JsonUtility.FromJson<IdTokenPackage>(www.text).idToken;
}
}
あなたは私がIDトークンを持っていないとして、最初の「GetBeers」要求は、401で失敗期待通り:これは私が持っているものです。ログイン作品やidTokenを返しますが、私は2番目「GetBeers」要求を作るしようとすると、今空でないIDと、それはまだ401で失敗しています。これは、サーバー上のログです:
2017-09-25 21:32:39.904 DEBUG 9968 --- [ XNIO-2 task-16] c.s.beerapp.aop.logging.LoggingAspect : Enter: org.springframework.boot.actuate.audit.AuditEventRepository.add() with argument[s] = [AuditEvent [timestamp=Mon Sep 25 21:32:39 CEST 2017, principal=anonymousUser, type=AUTHORIZATION_FAILURE, data={details=org.sprin[email protected]b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null, type=org.springframework.security.access.AccessDeniedException, message=Access is denied}]]
2017-09-25 21:32:39.907 DEBUG 9968 --- [ XNIO-2 task-16] c.s.beerapp.aop.logging.LoggingAspect : Exit: org.springframework.boot.actuate.audit.AuditEventRepository.add() with result = null
2017-09-25 21:32:39.909 DEBUG 9968 --- [ XNIO-2 task-16] i.g.j.s.Http401UnauthorizedEntryPoint : Pre-authenticated entry point called. Rejecting access
2017-09-25 21:32:40.145 DEBUG 9968 --- [ XNIO-2 task-25] c.s.beerapp.aop.logging.LoggingAspect : Enter: com.svendhhh.beerapp.web.rest.UserJWTController.authorize() with argument[s] = [LoginVM{username='admin', rememberMe=true}, com.codahale.metrics.s[email protected]7f7712]
2017-09-25 21:32:40.146 DEBUG 9968 --- [ XNIO-2 task-25] c.s.b.security.DomainUserDetailsService : Authenticating admin
Hibernate: select user0_.id as id1_7_0_, authority2_.name as name1_4_1_, user0_.created_by as created_2_7_0_, user0_.created_date as created_3_7_0_, user0_.last_modified_by as last_mod4_7_0_, user0_.last_modified_date as last_mod5_7_0_, user0_.activated as activate6_7_0_, user0_.activation_key as activati7_7_0_, user0_.email as email8_7_0_, user0_.first_name as first_na9_7_0_, user0_.image_url as image_u10_7_0_, user0_.lang_key as lang_ke11_7_0_, user0_.last_name as last_na12_7_0_, user0_.login as login13_7_0_, user0_.password_hash as passwor14_7_0_, user0_.reset_date as reset_d15_7_0_, user0_.reset_key as reset_k16_7_0_, authoritie1_.user_id as user_id1_8_0__, authoritie1_.authority_name as authorit2_8_0__ from jhi_user user0_ left outer join jhi_user_authority authoritie1_ on user0_.id=authoritie1_.user_id left outer join jhi_authority authority2_ on authoritie1_.authority_name=authority2_.name where user0_.login=?
2017-09-25 21:32:40.237 DEBUG 9968 --- [ XNIO-2 task-25] c.s.beerapp.aop.logging.LoggingAspect : Enter: org.springframework.boot.actuate.audit.AuditEventRepository.add() with argument[s] = [AuditEvent [timestamp=Mon Sep 25 21:32:40 CEST 2017, principal=admin, type=AUTHENTICATION_SUCCESS, data={}]]
Hibernate: insert into jhi_persistent_audit_event (event_id, event_date, event_type, principal) values (null, ?, ?, ?)
2017-09-25 21:32:40.240 DEBUG 9968 --- [ XNIO-2 task-25] c.s.beerapp.aop.logging.LoggingAspect : Exit: org.springframework.boot.actuate.audit.AuditEventRepository.add() with result = null
2017-09-25 21:32:40.242 DEBUG 9968 --- [ XNIO-2 task-25] c.s.beerapp.aop.logging.LoggingAspect : Exit: com.svendhhh.beerapp.web.rest.UserJWTController.authorize() with result = <200 OK,[email protected]5,{}>
2017-09-25 21:32:40.257 DEBUG 9968 --- [ XNIO-2 task-24] c.s.beerapp.aop.logging.LoggingAspect : Enter: org.springframework.boot.actuate.audit.AuditEventRepository.add() with argument[s] = [AuditEvent [timestamp=Mon Sep 25 21:32:40 CEST 2017, principal=anonymousUser, type=AUTHORIZATION_FAILURE, data={details=org.sprin[email protected]b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null, type=org.springframework.security.access.AccessDeniedException, message=Access is denied}]]
2017-09-25 21:32:40.258 DEBUG 9968 --- [ XNIO-2 task-24] c.s.beerapp.aop.logging.LoggingAspect : Exit: org.springframework.boot.actuate.audit.AuditEventRepository.add() with result = null
2017-09-25 21:32:40.260 DEBUG 9968 --- [ XNIO-2 task-24] i.g.j.s.Http401UnauthorizedEntryPoint : Pre-authenticated entry point called. Rejecting access
誰かが私が間違っていることを伝えることはできますか?間違った方法/フォーマットで認証ヘッダーを含めていますか?
ああ、私は問題が返されたJSONの私の非直列化にあることがわかりました。私の 'IdTokenPackage'クラスの' idToken'フィールドに 'id-token'値を出力していません... –
...しかし、私はなぜうまくいかないのですか?私は '[Serializable]'タグが見つからなかったのかもしれないと思ったかもしれませんが、それは助けになりませんでした... –