Using Guid/uuid to generate token and verify the correctness of token

Source: Internet
Author: User
Tags uuid

There would have been a powerful tool for generating token validation JWT, but sometimes taking into account the actual requirements of the project, Guid/uuid will be used to generate token, of course, Guid/uuid can generate a lot of unique validation results, so here is the main record of the use of Guid/uuid

public string createtkn (string account,string pin,string imei) {
         Savetodb st = new Savetodb ();
         Create GUID object
          uuid uuid = Uuid.randomuuid ();
          Get the object generated ID
          String token = uuid.tostring ();
          Convert to uppercase
          token = Token.touppercase ();
          Replace "-" into space
          token = Token.replaceall ("-", "");
          SYSTEM.OUT.PRINTLN (token);
          Save token and user information to the database
          map<string, object> params = new hashmap<> ();
          Params.put ("St_sid", token);
          Params.put ("St_account", account);
          String tablename = "table name";
          Here is a mybaits method to save data to the database, specifically http://blog.csdn.net/suresand/article/details/52540684
          st.save (tablename, params);
          Save the next time you can use the user's token in the database to do a validation return
          "token";
    
/** * Verify that the user's token value is correct * @param account * @param tokenvalue * @return correct--1, error
          --0 */public string checktkn (string account, String tokenvalue) {Savetodb st = new Savetodb ();
          Map<string, object> params=new hashmap<> ();
          Query the database to see if the user's token exists params.put ("St_sid", Tokenvalue);
          String tablename = "T_hy_logintkn_i";
          Use a list set to accept the map collection, so you need to convert the list rs = new arraylist<> () later;
          The same mybaits Query method rs = st.select (tablename, params);
          Map<string, object> map=new hashmap<> ();
          Map = (map<string, object>) rs.get (0);
          Check if the token belongs to the user if (Account.equals (Map.get ("St_account"). ToString ()) {return "1";
          }else{return "0"; }
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.