0

我想用来jjwt创建登录活动。所以我不知道如何使用它。我在https://github.com/jwtk/jjwt中阅读了它,但我听不懂,因为我的英语不流利。我想让你给我一个例子来理解它。我应该送任何东西去服务吗?或者我应该将密钥保存在我DatabaseHelper的 with 中sqlite openhelper。我OkHttp用于将用户名和密码发送到服务并使用json. 这是我的活动:

主要活动 :

public class MainActivity extends AppCompatActivity {

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

    }
    public void btnSignin_Clicked(View view){
        String username=((EditText)findViewById(R.id.txtUsername)).getText().toString();
        String password=((EditText)findViewById(R.id.txtPassword)).getText().toString();
        new SigninTask().execute(username,password);
    }
    public void btnSignup_Clicked(View view){
        ((EditText)findViewById(R.id.txtUsername)).setText("");
        ((EditText)findViewById(R.id.txtPassword)).setText("");
        String username=((EditText)findViewById(R.id.txtUsername)).getText().toString();
        String password=((EditText)findViewById(R.id.txtPassword)).getText().toString();
        new SignupTask().execute(username,password);
    }

    private class SignupTask extends AsyncTask<String,Object,String>{
        @Override
        protected String doInBackground(String... strings) {

            try {
                return
                new OkHttpClient()
                        .newCall(
                                new Request.Builder()
                                .url("http://localhost:8080/Service/signup?username="+strings[0]+"&password="+strings[1])
                                .build()
                        )
                        .execute()
                        .body()
                        .string();
            }catch (Exception e){
                return null;
            }

        }

        @Override
        protected void onPostExecute(String s) {
            SimpleDialog dialog=new SimpleDialog();
            dialog.setContext(MainActivity.this);
            dialog.setTitle("Error");
          try {
              JSONObject jsonObject=new JSONObject(s);
              Boolean result=jsonObject.getBoolean("value");
              if (result){
                  String username= ((EditText)findViewById(R.id.txtUsername)).getText().toString();
                  dialog.setMessage("Signup completed)");
                  ((EditText)findViewById(R.id.txtUsername)).setText("");
                  ((EditText)findViewById(R.id.txtPassword)).setText("");

              }else {
                  ((EditText)findViewById(R.id.txtUsername)).setText("");
                  ((EditText)findViewById(R.id.txtPassword)).setText("");
                  dialog.setMessage("Sign up failed !");
              }
          }catch (Exception e){
              dialog.setMessage("Error occured !");
          }
          dialog.show(getSupportFragmentManager(),"dialog");
        }
    }

    private class SigninTask extends AsyncTask<String,Object,String>{
        @Override
        protected String doInBackground(String... strings) {
            try {
              return   new OkHttpClient()
                        .newCall(
                                new Request.Builder()
                                        .url("http://localhost:8080/Service/authorize?username="+strings[0]+"&password="+strings[1])
                                        .build()
                        )
                        .execute()
                        .body()
                        .string();
            }catch (Exception e){
                return null;
            }
        }

        @Override
        protected void onPostExecute(String s) {
            SimpleDialog dialog=new SimpleDialog();
            dialog.setContext(MainActivity.this);
            dialog.setTitle("Error");
            try {
                JSONObject jsonObject=new JSONObject(s);
                Boolean result=jsonObject.getBoolean("value");
                if (result){
                    String username=((EditText)findViewById(R.id.txtUsername)).getText().toString();
                    String password=((EditText)findViewById(R.id.txtPassword)).getText().toString();
                    Intent intent=new Intent(MainActivity.this,CategoryActivity.class);
                    User user=new User();
                    user.setUsername(username);
                    user.setPassword(password);

                    intent.putExtra("user",user);
                    startActivity(intent);
                    finish();
                }else {
                    dialog.setMessage("Invalid username or password !");
                }
            }catch (Exception e){
                dialog.setMessage("Error occured !");
            }
            dialog.show(getSupportFragmentManager(),"dialog");
        }
    }
}
4

1 回答 1

0

通常,您将登录数据(用户名、密码)发送到对您进行身份验证的服务器,然后该服务器发回 JWT。您可以将此令牌存储在您的应用程序中。然后在每个请求中将此令牌发送到服务器以对您进行身份验证。

于 2020-04-10T12:00:33.677 回答