Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

.authorizeHttpRequests(auth -> auth
.requestMatchers(
"/","/api/auth/**", "/swagger-ui/**", "/v3/api-docs/**"
"/","/api/auth/**", "/api/profile/**", "/swagger-ui/**", "/v3/api-docs/**"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

λ³΄μ•ˆ μ„€μ • λ³€κ²½ ν•„μš”: ν”„λ‘œν•„ μ—”λ“œν¬μΈνŠΈ 인증 ν•„μˆ˜

ν”„λ‘œν•„ κ΄€λ ¨ μ—”λ“œν¬μΈνŠΈ(/api/profile/**)λ₯Ό 인증 없이 μ ‘κ·Ό κ°€λŠ₯ν•˜λ„λ‘ μ„€μ •ν•œ 것은 λ³΄μ•ˆμƒ μœ„ν—˜ν•©λ‹ˆλ‹€. ν”„λ‘œν•„ μˆ˜μ •μ€ 인증된 μ‚¬μš©μžλ§Œ κ°€λŠ₯ν•΄μ•Ό ν•©λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같이 μˆ˜μ •μ„ μ œμ•ˆν•©λ‹ˆλ‹€:

                .requestMatchers(
-                       "/","/api/auth/**", "/api/profile/**", "/swagger-ui/**", "/v3/api-docs/**"
+                       "/","/api/auth/**", "/swagger-ui/**", "/v3/api-docs/**"
                ).permitAll()

μΆ”κ°€λ‘œ νŠΉμ • ν”„λ‘œν•„ μ—”λ“œν¬μΈνŠΈμ— λŒ€ν•œ μ„ΈλΆ„ν™”λœ κΆŒν•œ 섀정이 ν•„μš”ν•©λ‹ˆλ‹€:

.requestMatchers(HttpMethod.GET, "/api/profile/**").authenticated()
.requestMatchers(HttpMethod.PUT, "/api/profile/**").authenticated()

).permitAll()
.anyRequest()
.authenticated());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
package com.mycom.socket.go_socket.controller;

import com.mycom.socket.auth.security.MemberDetails;
import com.mycom.socket.go_socket.dto.request.PasswordUpdateRequest;
import com.mycom.socket.go_socket.dto.request.ProfileUpdateRequest;
import com.mycom.socket.go_socket.dto.response.ProfileResponse;
import com.mycom.socket.go_socket.entity.Member;
import com.mycom.socket.go_socket.service.MemberService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/profile")
public class ProfileController {

private final MemberService memberService;

@GetMapping
public ProfileResponse getProfile(@AuthenticationPrincipal MemberDetails memberDetails) {
return ProfileResponse.of(memberDetails.getMember());
}

@PutMapping
public ProfileResponse updateProfile(
@AuthenticationPrincipal MemberDetails memberDetails,
@RequestBody @Valid ProfileUpdateRequest request
) {
Member updatedMember = memberService.updateProfile(
memberDetails.getMember().getEmail(),
request.nickname(),
request.intro()
);
return ProfileResponse.of(updatedMember);
}

@PutMapping("/password")
public void updatePassword(
@AuthenticationPrincipal MemberDetails memberDetails,
@RequestBody @Valid PasswordUpdateRequest request
) {
memberService.updatePassword(
memberDetails.getMember().getEmail(),
request.currentPassword(),
request.newPassword()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mycom.socket.go_socket.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record PasswordUpdateRequest(
@NotBlank(message = "ν˜„μž¬ λΉ„λ°€λ²ˆν˜ΈλŠ” ν•„μˆ˜μž…λ‹ˆλ‹€")
String currentPassword,
@NotBlank(message = "μƒˆ λΉ„λ°€λ²ˆν˜ΈλŠ” ν•„μˆ˜μž…λ‹ˆλ‹€")
@Size(min = 8, message = "λΉ„λ°€λ²ˆν˜ΈλŠ” 8자 이상이어야 ν•©λ‹ˆλ‹€")
String newPassword
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mycom.socket.go_socket.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record ProfileUpdateRequest(
@NotBlank(message = "λ‹‰λ„€μž„μ€ ν•„μˆ˜μž…λ‹ˆλ‹€")
@Size(min = 2, max = 20, message = "λ‹‰λ„€μž„μ€ 2자 이상 20자 μ΄ν•˜μ—¬μ•Ό ν•©λ‹ˆλ‹€")
String nickname,
@Size(max = 100, message = "μžκΈ°μ†Œκ°œλŠ” 100자λ₯Ό μ΄ˆκ³Όν•  수 μ—†μŠ΅λ‹ˆλ‹€")
String intro
) {}
9 changes: 9 additions & 0 deletions src/main/java/com/mycom/socket/go_socket/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ public Member(String email, String nickname, String password, String intro, Memb
public void updateRefreshToken(String updateRefreshToken) {
this.refreshToken = updateRefreshToken;
}

public void updateProfile(String nickname, String intro) {
this.nickname = nickname;
this.intro = intro;
}
Comment on lines +54 to +57
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

ν”„λ‘œν•„ 이미지 URL μ—…λ°μ΄νŠΈ λ©”μ†Œλ“œ λˆ„λ½

ν”„λ‘œν•„ 이미지 URL ν•„λ“œκ°€ μ‘΄μž¬ν•˜μ§€λ§Œ μ—…λ°μ΄νŠΈ λ©”μ†Œλ“œκ°€ λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

     public void updateProfile(String nickname, String intro) {
         this.nickname = nickname;
         this.intro = intro;
     }
+
+    public void updateProfileImage(String profileImgUrl) {
+        this.profileImgUrl = profileImgUrl;
+    }

Committable suggestion skipped: line range outside the PR's diff.


public void updatePassword(String encodedPassword) {
this.password = encodedPassword;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.mycom.socket.go_socket.service;

import com.mycom.socket.global.exception.NotFoundException;
import com.mycom.socket.go_socket.entity.Member;
import com.mycom.socket.go_socket.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -11,4 +14,26 @@
public class MemberService {

private final MemberRepository memberRepository;
private final PasswordEncoder passwordEncoder;

@Transactional
public Member updateProfile(String email, String nickname, String intro) {
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new NotFoundException("μ‚¬μš©μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."));

member.updateProfile(nickname, intro);
return member;
}

@Transactional
public void updatePassword(String email, String currentPassword, String newPassword) {
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new NotFoundException("ν˜„μž¬ λΉ„λ°€λ²ˆν˜Έκ°€ μΌμΉ˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."));

if (!passwordEncoder.matches(currentPassword, member.getPassword())) {
throw new NotFoundException("μƒˆ λΉ„λ°€λ²ˆν˜ΈλŠ” ν˜„μž¬ λΉ„λ°€λ²ˆν˜Έμ™€ 달라야 ν•©λ‹ˆλ‹€.");
}

member.updatePassword(passwordEncoder.encode(newPassword));
}
}
Loading