在android中设置Access jwt标记

时间:2018-02-19 05:40:13

标签: android jwt token

我有一个登录表单,我已经分配了一个jwt令牌,但是当我尝试登录时,它请求我一个令牌。它不会给我一个令牌,它会向我提供一条要求分配令牌的消息。

我的会话课程:

    public class Session {
    Context context;
    private SharedPreferences prefs;
    private ApiInterface apiInterface;

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        this.context = cntx;
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public void setAccessToken(String token) {

        prefs.edit().putString("JwtToken", token).apply();
    }

    public String getAccessToken() {
        String token = prefs.getString("JwtToken", "");
        if (token == null || token.isEmpty()) {
            token = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjIxNzc0NTI3OTksImlhdCI6MTUxNjAyMjk5OSwiaXNzIjoiQmFzb2JhYXMgTmVwYWwiLCJuYmYiOjE1MTYwMjI5OTksImp0aSI6Ikd1ZXN0VG9rZW4iLCJzdWIiOjB9.QikmNgBYmqch5HREGFEpUs4Xk3x-zFfDg5mhYJO7jM8";
        }
        return token;
    }

    public class LoginUser extends AppCompatActivity {
    private Button userLogin;
    ApiInterface apiInterface;
    private EditText loginEmail, loginPassword;
    private User user;

    TextView register_user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_user);

        loginEmail = findViewById(R.id.textInputEmailAddress);

        loginPassword = findViewById(R.id.textInputPassword);

        userLogin = findViewById(R.id.button_userLogin);


        userLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                userLogin(view);

            }
        });

        register_user = findViewById(R.id.Register_user);
        register_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginUser.this, SignupUserActivity.class));
                Toast.makeText(LoginUser.this, "asdasdsadas", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void userLogin(View view) {
        final User user = new User();
      final Session session = new Session(this);
        user.setEmail(loginEmail.getText().toString().trim());
        user.setPassword(loginPassword.getText().toString().trim());
        apiInterface = ApiClient.getClient().create(ApiInterface.class);

        Call<ResponseBody> call = apiInterface.logMeIn(session.getAccessToken(),user);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.code() == 200) {
                    try {
                        JSONObject jsonObject = new JSONObject(response.body().string());
                        JSONObject dataObject = jsonObject.getJSONObject("data");
                        String token = dataObject.getString("access_token");
                        session.setAccessToken("Bearer " + token);
                        String name=user.getName();
                        Toast.makeText(LoginUser.this, "WELCOME", Toast.LENGTH_SHORT).show();
                        Log.e("USER", jsonObject.toString());
                        startActivity(new Intent(LoginUser.this, MainActivity.class));


                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else
                    Log.e("TestActivity", response.raw().toString());

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });

    }
}
  

{&#34; message&#34;:&#34;需要令牌&#34;,&#34;代码&#34;:400}

每当我尝试登录时,都会向我发送上述请求令牌的消息,但请提供帮助。

0 个答案:

没有答案