Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Code Review] 환이 code review #17

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Binary file added UMC-0-WORKBOOK.zip
Binary file not shown.
7 changes: 7 additions & 0 deletions UMC-0-WORKBOOK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ dependencies {
implementation 'org.springdoc:springdoc-openapi-ui:1.6.9'
implementation 'org.springdoc:springdoc-openapi-data-rest:1.6.9'

// 스프링 시큐리티
implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.security:spring-security-test'

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.1.RELEASE'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class QMember extends EntityPathBase<Member> {

public final StringPath name = createString("name");

public final StringPath password = createString("password");

public final StringPath phoneNumber = createString("phoneNumber");

public final NumberPath<Integer> point = createNumber("point", Integer.class);
Expand All @@ -55,6 +57,8 @@ public class QMember extends EntityPathBase<Member> {

public final ListPath<ReviewReply, QReviewReply> reviewReplyList = this.<ReviewReply, QReviewReply>createList("reviewReplyList", ReviewReply.class, QReviewReply.class, PathInits.DIRECT2);

public final EnumPath<umc.study.domain.enums.Role> role = createEnum("role", umc.study.domain.enums.Role.class);

public final EnumPath<umc.study.domain.enums.UserStatus> status = createEnum("status", umc.study.domain.enums.UserStatus.class);

public final EnumPath<umc.study.domain.enums.UserType> type = createEnum("type", umc.study.domain.enums.UserType.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public class QReview extends EntityPathBase<Review> {

public final NumberPath<Float> rating = createNumber("rating", Float.class);

public final ListPath<ReviewReply, QReviewReply> reviewReplyList = this.<ReviewReply, QReviewReply>createList("reviewReplyList", ReviewReply.class, QReviewReply.class, PathInits.DIRECT2);

public final QStore store;

//inherited
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class QMemberMission extends EntityPathBase<MemberMission> {

public final umc.study.domain.QMission mission;

public final umc.study.domain.QStore store;

//inherited
public final DateTimePath<java.time.LocalDateTime> updatedAt = _super.updatedAt;

Expand All @@ -56,6 +58,7 @@ public QMemberMission(Class<? extends MemberMission> type, PathMetadata metadata
super(type, metadata, inits);
this.member = inits.isInitialized("member") ? new umc.study.domain.QMember(forProperty("member")) : null;
this.mission = inits.isInitialized("mission") ? new umc.study.domain.QMission(forProperty("mission"), inits.get("mission")) : null;
this.store = inits.isInitialized("store") ? new umc.study.domain.QStore(forProperty("store"), inits.get("store")) : null;
}

}
Expand Down
15 changes: 12 additions & 3 deletions UMC-0-WORKBOOK/src/main/java/umc/study/StudyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import umc.study.domain.Store;
import umc.study.repository.StoreRepository.StoreRepository;
import umc.study.service.StoreService.StoreQueryService;

@SpringBootApplication
Expand All @@ -19,20 +21,27 @@ public static void main(String[] args) {
@Bean
public CommandLineRunner run(ApplicationContext context) {
Copy link
Member

Choose a reason for hiding this comment

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

이건 어떤 의도로 사용한 걸까요 ??

return args -> {
StoreRepository storeRepository = context.getBean(StoreRepository.class);
StoreQueryService storeService = context.getBean(StoreQueryService.class);

// 파라미터 값 설정
// 더미 데이터 저장
Store dummyStore = Store.builder()
.name("요아정")
.address("서울특별시")
.rating(4.5f)
.build();
storeRepository.save(dummyStore);

// 쿼리 실행
String name = "요아정";
Float rating = 4.0f;

// 쿼리 메서드 호출 및 쿼리 문자열과 파라미터 출력
System.out.println("Executing findStoresByNameAndScore with parameters:");
System.out.println("Name: " + name);
System.out.println("Rating: " + rating);

storeService.findStoresByNameAndScore(name, rating)
.forEach(System.out::println);
};

}
}
19 changes: 19 additions & 0 deletions UMC-0-WORKBOOK/src/main/java/umc/study/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package umc.study.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 모든 경로에 대해 CORS 허용
.allowedOrigins("http://localhost:8080", "http://localhost:3000") // 허용할 도메인
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 허용할 HTTP 메서드
.allowedHeaders("*") // 모든 헤더 허용
.allowCredentials(true); // 인증 정보 허용
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package umc.study.config.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

@EnableWebSecurity
@Configuration
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.cors() // CORS 허용 설정 추가
.and()
.authorizeHttpRequests((requests) -> requests
.requestMatchers("/", "/home", "/signup", "/css/**").permitAll()
.requestMatchers(
"/swagger-ui/**", // Swagger UI
"/v3/api-docs/**", // OpenAPI docs
"/swagger-ui.html", // Legacy Swagger UI path
"/webjars/**",
"/memberMissions/**",
"/members/**",
"/stores/**"
).permitAll()
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
)
.formLogin((form) -> form
.loginPage("/login")
.defaultSuccessUrl("/home", true)
.permitAll()
)
.logout((logout) -> logout
.logoutUrl("/logout")
.logoutSuccessUrl("/login?logout")
Copy link
Member

Choose a reason for hiding this comment

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

url을 이렇게 작성하신 의도가 궁금하네요

.permitAll()
)
.csrf().disable(); // Swagger 테스트 시 CSRF 비활성화

return http.build();
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000", "http://localhost:8080")); // 허용할 Origin 추가
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS")); // 허용할 HTTP 메서드
configuration.setAllowedHeaders(Arrays.asList("*")); // 모든 헤더 허용
configuration.setAllowCredentials(true); // 인증 정보 허용

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration); // 모든 경로에 대해 CORS 설정 적용
return source;
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public static Member toMember(MemberRequestDTO.JoinDto request){

return Member.builder()
.address(request.getAddress())
.email(request.getEmail())
.password(request.getPassword())
.gender(gender)
.name(request.getName())
.age(request.getAge())
.role(request.getRole())
.build();
}

Expand Down
13 changes: 12 additions & 1 deletion UMC-0-WORKBOOK/src/main/java/umc/study/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.hibernate.annotations.DynamicUpdate;
import umc.study.domain.common.BaseEntity;
import umc.study.domain.enums.Gender;
import umc.study.domain.enums.Role;
import umc.study.domain.enums.UserStatus;
import umc.study.domain.enums.UserType;
import umc.study.domain.mapping.MemberAgree;
Expand Down Expand Up @@ -39,9 +40,15 @@ public class Member extends BaseEntity {
@Column(nullable = false)
private Integer age;

// @Column(nullable = false, length = 40)
@Column(nullable = false, length = 40)
private String email;

@Column(nullable = false)
private String password;

@Enumerated(EnumType.STRING)
private Role role;

@ColumnDefault("0")
private Integer point;

Expand Down Expand Up @@ -80,4 +87,8 @@ public class Member extends BaseEntity {
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL)
private final List<ReviewReply> reviewReplyList = new ArrayList<>();

public void encodePassword(String password){
this.password = password;
}

}
24 changes: 12 additions & 12 deletions UMC-0-WORKBOOK/src/main/java/umc/study/domain/Mission.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Mission extends BaseEntity {

public class Mission extends BaseEntity{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Integer reward;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "store_id", nullable = false)
private Store store;

@Column(columnDefinition = "TEXT")
@Column(nullable = false, columnDefinition = "TEXT")
private String content;

@Column(nullable = false)
private Integer reward;

@Enumerated(EnumType.STRING)
@Column(columnDefinition = "VARCHAR(15) DEFAULT 'CHALLENGING'")
private MissionStatus missionStatus;


private LocalDateTime deadline;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "store_id")
private Store store;

@OneToMany(mappedBy = "mission", cascade = CascadeType.ALL)
private List<MemberMission> memberMissionList = new ArrayList<>();


private final List<MemberMission> memberMissionList = new ArrayList<>();

}
// getters, setters, constructors
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Question extends BaseEntity {
@Column(columnDefinition = "VARCHAR(15) DEFAULT 'NOT_DELETED'")
private DelYN delYN;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
Copy link
Member

Choose a reason for hiding this comment

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

Eager로 설정한 이유가 있나요?

@JoinColumn(name = "member_id")
private Member member;

Expand Down
34 changes: 14 additions & 20 deletions UMC-0-WORKBOOK/src/main/java/umc/study/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,33 @@
import java.util.ArrayList;
import java.util.List;

@Entity

@Getter
@DynamicInsert
@DynamicUpdate
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Review extends BaseEntity {
@Entity
public class Review extends BaseEntity{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(columnDefinition = "TEXT")
private String content;

private float rating;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "store_id")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "store_id", nullable = false)
private Store store;

@OneToMany(mappedBy = "review", cascade = CascadeType.ALL)
private List<ReviewReply> reviewReplyList = new ArrayList<>();
@Column(nullable = false)
private Float rating;

@Column(nullable = false, columnDefinition = "TEXT")
private String content;

public void setStore(Store store){
if(this.store != null)
store.getReviewList().remove(this);
this.store = store;
store.getReviewList().add(this);
}
}
// getters, setters, constructors
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class ReviewReply extends BaseEntity {
@Column(nullable = false, length = 20)
private String title;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "review_id")
private Review review;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "member_id")
private Member member;
}
4 changes: 2 additions & 2 deletions UMC-0-WORKBOOK/src/main/java/umc/study/domain/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public class Store extends BaseEntity {
@Column(nullable = false, length = 20)
private String name;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "region_id")
private Region region;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "food_id")
private FoodCategory foodCategory;

Expand Down
5 changes: 5 additions & 0 deletions UMC-0-WORKBOOK/src/main/java/umc/study/domain/enums/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package umc.study.domain.enums;

public enum Role {
ADMIN, USER
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public class MemberAgree extends BaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "agree_id")
private Agreement agreement;
}
Loading