기본 콘텐츠로 건너뛰기

jwt 토큰생성

 jwt는  header, claims, signature 영역으로 이루어진다.  그러나 jwtbuilder의 멤버변수는 다음과 같다.  header/claims/signature의 3개의 변수는 아니다.  public class DefaultJwtBuilder implements JwtBuilder { private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper() ; private Header header ; private Claims claims ; private String payload ; private SignatureAlgorithm algorithm ; private Key key ; private byte [] keyBytes ; private CompressionCodec compressionCodec ; jwtbuilder 사용예제를 보면 다음과 같다.  String jws = Jwts.builder() .setIssuer( "Stormpath" ) .setSubject( "msilverman" ) .claim( "name" , "Micah Silverman" ) .claim( "scope" , "admins" ) // Fri Jun 24 2016 15:33:42 GMT-0400 (EDT) .setIssuedAt(Date.from(Instant.ofEpochSecond( 1466796822L ))) // Sat Jun 24 2116 15:33:42 GMT-0400 (EDT) .setExpiration(Date.from(Instant.ofEpochSecond( 4622470422L ))) .signWith( SignatureAlgor
최근 글

jwt 파싱2 (signature)

  http://changpd.blogspot.com/2021/04/jwt-1headerpayload.html 에이어서 두번째  signature 파싱처리 signature부분은 토큰의 변조 방지를 담당한다.  토큰내용이야 base64로 인코딩되어있어 얼마든지 디코딩이 가능하나 변조는 불가능해야 한다.  jwt파싱 (step1-1, SignatureAlgorithm) 첫번째 파싱한 header를 다시 JwsHeader로 타입캐스팅한다.  그리고 header에 저장된 alg값을 읽어온다.  이값이 없으면 더이상 진행이 되지 않는다.  // =============== Signature ================= if (base64UrlEncodedDigest != null ) { //it is signed - validate the signature JwsHeader jwsHeader = (JwsHeader) header ; SignatureAlgorithm algorithm = null; if (header != null ) { String alg = jwsHeader.getAlgorithm() ; if (Strings. hasText (alg)) { algorithm = SignatureAlgorithm. forName (alg) ; } } if (algorithm == null || algorithm == SignatureAlgorithm. NONE ) { //it is plaintext, but it has a signature. This is invalid: String msg = "JWT string has a digest/signature, but the header does not reference a valid signature " + "algori

jwt 파싱1(header,payload)

jwt 소스코드를 뜯어보자 jwt 소스를 보니 header, body, signature 로 구성되어있는거 확인 signature 영역은 string, body는 제네릭, jwsheader는 또 뭐가있긴한데, keyId, algorithm이 있음  public class DefaultJws< B > implements Jws< B > { private final JwsHeader header ; private final B body ; private final String signature ; public DefaultJws (JwsHeader header , B body , String signature) { this . header = header ; this . body = body ; this . signature = signature ; } @Override public JwsHeader getHeader () { return this . header ; } @Override public B getBody () { return this . body ; } @Override public String getSignature () { return this . signature ; } @Override public String toString () { return "header=" + header + ",body=" + body + ",signature=" + signature ; } } jwt 파싱 (step1, 함수선언) public Jwt parse (String jwt) throws ExpiredJwtException , MalformedJwtExcepti

(java) 크롬에서 쿠키 삭제 안되는 경우

  java에서 쿠키 삭제하는 방법은 똑같은 이름의 쿠키를 만들고 업데이트 하는 방식이다.  이때 maxAge를 '0' 으로 설정한다.  그래서 아래처럼 코딩한결과  크롬을 제외한 나머지 브라우저에서는 전부 쿠키가 삭제가 되었으나 크롬만 삭제가 되지 않았다.  문제의코드 Cookie cookie = new Cookie(name , null ) ; cookie.setHttpOnly( false ) ; cookie.setMaxAge( 0 ) ; cookie.setPath( "/" ) ; cookie.setDomain( "test.co.kr" ) ; 결론만 얘기하면  Secure 설정을 ture로 추가해서 해결했다.  https, ssl 기반에서만 주고받을 수 있게된다.  public void setSecure ( boolean flag) { secure = flag ; } /** * Returns <code> true </code> if the browser is sending cookies only over a * secure protocol, or <code> false </code> if the browser can send cookies * using any protocol. * * @return <code> true </code> if the browser uses a secure protocol; * otherwise, <code> true </code> * @see #setSecure */ 원인은 이사이트의 쿠키설정중 samesite가 'none' 으로 설정되어있었다.  samesite가 none인 경우에는 반드시 secure 옵션을 같이 넣어줘야 크롬에서 정상동작한다. (chrome 80 버전 업데이트 이슈) 웹서버에서 변경하고 싶으면 nginx.conf에  pr

(java) try-with-resources

https://www.baeldung.com/java-try-with-resources try 구문절에 resources 사용구문을 입력할 수 있다. 1 2 3 try (PrintWriter writer = new PrintWriter( new File( "test.txt" ))) {      writer.println( "Hello World" ); } 구문절에 기존에 try-catch-finally를 사용할때는 보통 아래처럼 한다. try에서 열어주고, finally에서 닫아주고  1 2 3 4 5 6 7 8 9 10 11 12 13 Scanner scanner = null ; try {      scanner = new Scanner( new File( "test.txt" ));      while (scanner.hasNext()) {          System.out.println(scanner.nextLine());      } } catch (FileNotFoundException e) {      e.printStackTrace(); } finally {      if (scanner != null ) {          scanner.close();      } } 이걸 다시 try-with-resources 방식으로 하면 1 2 3 4 5 6 7 try (Scanner scanner = new Scanner( new File( "test.txt" ))) {      while (scanner.hasNext()) {          System.out.println(scanner.nextLine());      } } catch