[프로젝트] 웹 TripRecorder - day07

_bean_·2023년 6월 12일
0
post-thumbnail

오류 및 문제점

1. GitHub protected branch hook declined

  • 문제점: 권한이 없어 Organizations의 branch에 push가 안된다.
  • 해결 방안: 멤버 권한을 Owner로 변경한다.

2. successfulAuthentication return object

  • 문제점: 로그인 성공 시 json 형식의 사용자 정보를 front로 보내고자 한다.
  • 해결 방안: response.getWriter()를 이용해 응답을 보낸다.
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonString);

3. Java object to Json String

  • 문제점: 로그인 성공 시 getWriter를 사용하기 위해 json 형태의 문자열을 만들어야 한다.
  • 해결 방안: ObjectMapper를 이용해 Java 객체를 json 문자열로 만든다.
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(user);

4. 아이디 잘못 입력 시 null

  • 문제점: 로그인 시 아이디를 잘못 입력하면 null이 되어 에러가 발생한다.
  • 해결 방안: user가 null이 아닐 때 getPassword(), getUsername()을 실행하도록 코드를 수정한다.
@Override
public String getPassword() {
	return user != null ? user.getUserPw() : null;
}

@Override
public String getUsername() {
	return user != null ? user.getUserId() : null;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
	Collection<GrantedAuthority> authorities = new ArrayList<>();
	if(user != null)
		authorities.add(new SimpleGrantedAuthority(user.getUserRole().toString()));
	return authorities;
}

5. ProfileRepository is null

  • 문제점: 로그인 성공 시 profile 이미지를 받아야 하는데 repository가 null이다.
  • 해결 방안: JwtAuthorizationFilter와 마찬가지로 SecurityConfig에서 생성한 repository를 매개변수로 전달받는다. (AWS에서 이미지를 가져오는 것은 추후 진행하기로 했다.)
// SecurityConfig.java
.addFilter(new JwtAuthenticationFilter(authenticationManager(), prepo))

// JwtAuthenticationFilter.java
public JwtAuthenticationFilter(AuthenticationManager authenticationManager, ProfileRepository prepo) {
	super(authenticationManager);
	this.prepo = prepo;
}

String profile = prepo.findById(user.getUserNo()).orElse(null).getProfilePhoto();

6. Cannot deserialize value of type java.sql.Timestamp from String

  • 문제점: request body 요청 시 Timestamp 형식이 맞지 않아 해당 타입으로 파싱할 수 없다.
  • 해결 방안: 요청 형태를 Timestamp 문자열 형식으로 바꾼다.
{
    "myDateTime": "2023-06-12T09:30:00.000Z"
}

진행 상황

1. 회원가입 시 닉네임, 이메일 중복 체크

public boolean postUsernickCheck(@RequestBody UserVO user) {
	UserVO findUser = urepo.findByUserNick(user.getUserNick());
	return findUser != null;
}

2. 로그인 시 로그인 정보 전달

  • userno를 이용해 프로필 이미지를 얻는다. (현재는 파일명만 가져온 상태)
String profile = prepo.findById(user.getUserNo()).orElse(null).getProfilePhoto();
user.setUserProfile(profile);

ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(user);

response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonString);

3. 여행 카테고리 등록 진행중

  • DB에 잘 들어가지만 사용자 정보를 리턴하면 같은 사용자가 엄청 많이 나온다.
@PostMapping("/registertrip") 
public TripVO postRegisterTrip(HttpServletRequest request, @RequestBody TripVO trip) {
	String jwt = request.getHeader("Authorization");
	String decodeStr = EncodingUtil.getDecodedStr(jwt.replace('.', '@').split("@")[1]);
	JSONObject jsonObj = JsonUtil.getStringToJsonObj(decodeStr);
	Long userNo = (Long) jsonObj.get("userno");
	System.out.println(userNo);
		
	UserVO user = urepo.findById(userNo).orElse(null);
	trip.setUser(user);
		
	trepo.save(trip);
	return trip;
}

참고 자료

profile
어쩌다 풀스택 :3

0개의 댓글