Java 类io.jsonwebtoken.impl.crypto.MacProvider 实例源码

项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test
public void testExpiredToken() {

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.SECOND, 1);

    Key key = MacProvider.generateKey();

    // new KeyPair("publickey","privatekey")
    String token = TokenUtil.getJWTString("Phil", new String[]{"user"}, 11, calendar.getTime(), key);
    assertNotNull(token);
    try {
        Thread.sleep(1000L * 3L);                 //1000 milliseconds is one second.
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }

    assertEquals("Token should have expired", false, TokenUtil.isValid(token, key));
}
项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test
public void testNonExpiredToken() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MINUTE, 15);

    Key key = MacProvider.generateKey();

    // new KeyPair("publickey","privatekey")
    String token = TokenUtil.getJWTString("Phil", new String[]{"user"}, 12, calendar.getTime(), key);
    assertNotNull(token);
    try {
        Thread.sleep(1000L * 2L);                 //1000 milliseconds is one second.
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }

    assertEquals("Token should have been valid", true, TokenUtil.isValid(token, key));
}
项目:kms-jwt    文件:JWTEncoder.java   
public static JwtBuilder builder(KeyEncrypt keyEncrypt) throws KeyOperationException {
    SecretKey secretKey = MacProvider.generateKey(SignatureAlgorithm.HS256);
    byte[] encryptedKey = keyEncrypt.encrypt(secretKey.getEncoded());

    return Jwts.builder()
            .setHeaderParam("kid", DatatypeConverter.printBase64Binary(encryptedKey))
            .signWith(SignatureAlgorithm.HS256, secretKey);
}
项目:sam-elle    文件:UserSigninTest.java   
@Test
    public void testSigninService() {

        UserSigninReq reqObj = new UserSigninReq();
        // reqObj.setAppName("samyh");
        // reqObj.setDeviceType("android");
        // reqObj.setVersion("0.0.1");
//      reqObj.setUserPhone("13900000014");
        reqObj.setPassword("123456789");
        reqObj.setDeviceInfo("XXXXXXXXXXX");

        String jsonReq = JSON.toJSONString(reqObj);

        logger.info("Reuqest json String:" + jsonReq);

        String url = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/user/signin.json").build().toUriString();

        logger.info("Request URL:" + url);

        Key key = MacProvider.generateKey();
        String s = createJWT(key, jsonReq);
        logger.info("jwt:" + s);

        String str = decodeJWT(key, s);
        logger.info("jwt:" + str);

        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("jsonReq", jsonReq);
        String resp = restTemplate.postForObject(url, params, String.class);

        logger.info("ResponseBody:" + resp);

        assertEquals(jsonReq, str);
    }
项目:sam-elle    文件:UserSigninTest.java   
@Test
    public void testSigninService() {

        UserSigninReq reqObj = new UserSigninReq();
        // reqObj.setAppName("samyh");
        // reqObj.setDeviceType("android");
        // reqObj.setVersion("0.0.1");
//      reqObj.setUserPhone("13900000014");
        reqObj.setPassword("123456789");
        reqObj.setDeviceInfo("XXXXXXXXXXX");

        String jsonReq = JSON.toJSONString(reqObj);

        logger.info("Reuqest json String:" + jsonReq);

        String url = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/user/signin.json").build().toUriString();

        logger.info("Request URL:" + url);

        Key key = MacProvider.generateKey();
        String s = createJWT(key, jsonReq);
        logger.info("jwt:" + s);

        String str = decodeJWT(key, s);
        logger.info("jwt:" + str);

        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("jsonReq", jsonReq);
        String resp = restTemplate.postForObject(url, params, String.class);

        logger.info("ResponseBody:" + resp);

        assertEquals(jsonReq, str);
    }
项目:Jersey2-Security-JWT    文件:BookResourceTest.java   
protected javax.ws.rs.core.Application configure() {
    enable(TestProperties.LOG_TRAFFIC);
    enable(TestProperties.DUMP_ENTITY);
    final BookDao bookDao = new BookDao();
    final UserDao userDao = new UserDao();
    userDao.addUser(AdminUser);
    userDao.addUser(NormalUser);
    book1Id = bookDao.addBook(book1).getId();
    book2Id = bookDao.addBook(book2).getId();
    final Key key = MacProvider.generateKey();
    return new com.sample.Application(bookDao, userDao, key);
}
项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test(expected = NullPointerException.class)
public void testGetJWTString_invalidUserName() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MINUTE, 15);
    String username = null;
    String[] roles = new String[]{"user"};
    int version = 0;
    Date expires = calendar.getTime();
    Key key = MacProvider.generateKey();
    TokenUtil.getJWTString(username, roles, version, expires, key);
}
项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test(expected = NullPointerException.class)
public void testGetJWTString_invalidRoles() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MINUTE, 15);
    String username = "Bob";
    String[] roles = null;
    int version = 0;
    Date expires = calendar.getTime();
    Key key = MacProvider.generateKey();
    TokenUtil.getJWTString(username, roles, version, expires, key);
}
项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test(expected = NullPointerException.class)
public void testGetJWTString_invalidExpires() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MINUTE, 15);
    String username = "Bob";
    String[] roles = new String[]{"user"};
    int version = 0;
    Date expires = null;
    Key key = MacProvider.generateKey();
    TokenUtil.getJWTString(username, roles, version, expires, key);
}
项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test
public void testGetVersionGood() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MINUTE, 15);
    String username = "Bob";
    String[] roles = new String[]{"user"};
    int version = 3;
    Date expires = calendar.getTime();
    Key key = MacProvider.generateKey();
    String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key);
    int version1 = TokenUtil.getVersion(jwtString, key);
    assertEquals(version, version1);
}
项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test
public void testGetVersionBad() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MINUTE, 15);
    String username = "Bob";
    String[] roles = new String[]{"user"};
    int version = 3;
    Date expires = calendar.getTime();
    Key key = MacProvider.generateKey();
    String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key);
    int version1 = TokenUtil.getVersion(jwtString, MacProvider.generateKey());
    assertEquals(-1, version1);
}
项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test
public void testGetRolesGood() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MINUTE, 15);
    String username = "Bob";
    String[] roles = new String[]{"user"};
    int version = 3;
    Date expires = calendar.getTime();
    Key key = MacProvider.generateKey();
    String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key);
    String[] roles1 = TokenUtil.getRoles(jwtString, key);
    assertEquals(Arrays.toString(roles), Arrays.toString(roles1));
}
项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test
public void testGetRolesBad() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MINUTE, 15);
    String username = "Bob";
    String[] roles = new String[]{"user"};
    int version = 3;
    Date expires = calendar.getTime();
    Key key = MacProvider.generateKey();
    String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key);
    String[] roles1 = TokenUtil.getRoles(jwtString, MacProvider.generateKey());
    assertEquals(Arrays.toString(new String[]{}), Arrays.toString(roles1));
}
项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test
public void testGetNamesGood() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MINUTE, 15);
    String username = "Bob";
    String[] roles = new String[]{"user"};
    int version = 3;
    Date expires = calendar.getTime();
    Key key = MacProvider.generateKey();
    String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key);
    String username1 = TokenUtil.getName(jwtString, key);
    assertEquals(username, username1);
}
项目:Jersey2-Security-JWT    文件:TokenUtilTest.java   
@Test
public void testGetNamesBad() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MINUTE, 15);
    String username = "Bob";
    String[] roles = new String[]{"user"};
    int version = 3;
    Date expires = calendar.getTime();
    Key key = MacProvider.generateKey();
    String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key);
    String username1 = TokenUtil.getName(jwtString, MacProvider.generateKey());
    assertNull(username1);
}
项目:WebAPI    文件:TokenManager.java   
public static String createJsonWebToken(String subject, Date expiration, Collection<String> permissions) {
  Key key = MacProvider.generateKey();
  userToKeyMap.put(subject, key);

  String permissionsString = StringUtils.join(permissions, "|");
  Map<String, Object> claims = new HashMap<>();
  claims.put("permissions", permissionsString);
  return Jwts.builder()
          .setClaims(claims)
          .setSubject(subject)
          .setExpiration(expiration)
          .signWith(SignatureAlgorithm.HS512, key)
          .compact();
}
项目:json-api-java    文件:JwtTokenService.java   
public JwtTokenService() {
    key = MacProvider.generateKey();
}
项目:spark-jwt-auth    文件:IKeyGeneratorImpl.java   
@Override
public Key getKey() {

    return MacProvider.generateKey();
}
项目:Jersey2-Security-JWT    文件:Main.java   
public static HttpServer startServer() {

        Key key = MacProvider.generateKey();

        final ResourceConfig rc = new Application(new BookDao(), new UserDao(), key);
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    }