Skip to content

Commit

Permalink
verify api 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
asn6878 committed Jun 3, 2024
1 parent 926ac79 commit 45c796c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import com.example.demo.domain.auth.dto.SignInReq;
import com.example.demo.domain.auth.dto.SignUpReq;
import com.example.demo.domain.auth.service.AuthService;
import com.example.demo.global.security.userdetails.CustomUserDetails;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("api/v1/auth")
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -29,4 +30,11 @@ public ResponseEntity<?> signUp(@RequestBody SignUpReq request) {

return ResponseEntity.ok(authService.signUp(request));
}

@GetMapping("/verify")
public ResponseEntity<?> verify(@AuthenticationPrincipal CustomUserDetails userDetails){
log.warn("userDetails : {}", userDetails.getUsername().toString());

return ResponseEntity.ok("반갑습니다! " + userDetails.getUsername() + "님!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ public User(Long id, String username, String password, String nickName, String e
this.password = password;
this.nickName = nickName;
this.email = email;
this.authority = Authority.ROLE_ADMIN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public Claims getClaimsFromRefreshToken(String refreshToken){
}

public Authentication getAuthentication(Claims claims) {
UserDetails userDetails = userDetailsService.loadUserById(Long.parseLong(claims.getId()));
UserDetails userDetails = userDetailsService.loadUserByUsername(claims.get("sub", String.class));
log.info("{}", userDetails);
return new UsernamePasswordAuthenticationToken(userDetails, "",
userDetails.getAuthorities());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ protected void doFilterInternal(HttpServletRequest request,
String token = jwtTokenProvider.parseJwtFromRequest(request);
if (token != "") {
try {
log.info("JwtAuthenticationFilter 도착");
Claims claims = jwtTokenProvider.getClaimsFromAccessToken(token);
Authentication authentication = jwtTokenProvider.getAuthentication(claims);
SecurityContextHolder.getContext().setAuthentication(authentication);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public String getPassword() {

@Override
public String getUsername() {
return null;
return this.user.getUsername();
}

/*
Expand Down

0 comments on commit 45c796c

Please sign in to comment.