토큰 저장
1. 시작하며
Swit API를 사용하기 위해서는 기본적으로 토큰이 필요 합니다. 자세한 사항은 Swit Developers의 OAuth flow 를 참조해 주세요
2. 토큰의 저장
- 본 예제에서는 Access token과 Refresh token을 데이터 베이스에 저장하도록 하겠습니다.
- 먼저 Token을 담을 객체를 작성하겠습니다.
3. 토큰 객체 생성
3.1. SwitTokenEntity
package io.swit.api.model.entity.eai;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity
@Data
@Table(name = "swit_token")
@NoArgsConstructor(access = AccessLevel.PUBLIC)
public class SwitTokenEntity {
@Id
@Column(name = "token_id", nullable = false, length = 30)
private String tokenId = "SWIT";
@Column(name = "access_token", length = 500, nullable = false)
private String accessToken;
@Column(name = "refresh_token", length = 500, nullable = false)
private String refreshToken;
@Column(name = "expire_in", nullable = true)
private Long expireIn;
@Column(name = "scope", length = 100, nullable = true)
private String scope;
@Column(name = "token_type", length = 50, nullable = false)
private String tokenType;
}
3.2. SwitTokenDto
package io.swit.api.model.dto.eai;
import io.swit.api.model.entity.eai.SwitTokenEntity;
import lombok.Data;
@Data
public class SwitTokenDto {
/** 토큰 아이디 */
private String tokenId;
/** 접근 토큰 */
private String accessToken;
/** 재인증 토큰 */
private String refreshToken;
/** 만료시간 */
private Long expireIn;
/** 인증 점위 */
private String scope;
/** 토큰 타입 */
private String tokenType;
/**
* 엔티티 변환
* @return 스윗 유저 토큰 엔티티
*/
public SwitTokenEntity toEntity() {
SwitTokenEntity entity = new SwitTokenEntity();
entity.setTokenId(tokenId);
entity.setAccessToken(this.accessToken);
entity.setRefreshToken(this.refreshToken);
entity.setExpireIn(this.expireIn);
entity.setScope(this.scope);
entity.setTokenType(this.tokenType);
return entity;
}
}
3.3. 인증용 컨트롤러 작성
- 토큰을 요청하였을때 결과값을 받는 컨트롤러를 작성합니다.
- 토큰 요청은 Swit Developers를 참조해 주시고, 소스 관련은 Customer Tech Support 팀의 Github를 참조해 주세요.
- 아래의 소스는 인증을 받아 토큰을 받는 예제 입니다.
@GetMapping(value = "/callback")
public ResponseEntity<?> getOauthCallback(
HttpServletRequest req,
HttpServletResponse res,
HttpSession session
) {
String code = "";
String state = "";
SwitTokenDto retDto = new SwitTokenDto();
WebClient webClient = WebClient.builder()
.baseUrl(var.API_URL)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.build();
try {
code = req.getParameter("code").trim();
state = req.getParameter("state").trim();
String redirectUri = req.getRequestURL().toString().replace("http://", "https://");
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("grant_type", "authorization_code");
body.add("code", code);
body.add("client_id", var.CLIENT_ID);
body.add("client_secret", var.CLIENT_SECRET);
body.add("redirect_uri", redirectUri);
String retVal = webClient.post()
.uri(var.TOKEN_URL)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.bodyValue(body)
.retrieve()
.bodyToMono(String.class)
.block();
JsonParser parser = new JsonParser();
Object obj = parser.parse(retVal);
JsonObject jsonObject = (JsonObject)obj;
retDto.setAccessToken(jsonObject.get("access_token").toString().replace("\"", ""));
retDto.setRefreshToken(jsonObject.get("refresh_token").toString().replace("\"", ""));
retDto.setExpireIn(Long.parseLong(jsonObject.get("expires_in").toString()));
retDto.setScope(jsonObject.get("scope").toString().replace("\"", ""));
retDto.setTokenType(jsonObject.get("token_type").toString().replace("\"", ""));
// 받아온 토큰을 데이터베이스에 저장한다.
retDto.setTokenId("cts-api");
this.authService.saveSwitToken(retDto);
//res.sendRedirect("/swagger-ui.html");
} catch (Exception e) {
log.error(e.getMessage());
return ResponseEntity.internalServerError().body(e.getMessage());
}
return ResponseEntity.ok(retDto);
}
