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

[3주차 과제] 이동현 #42

Open
wants to merge 12 commits into
base: dongsuu
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Spring-JPA-study
18 changes: 16 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.1'
id 'org.springframework.boot' version '2.7.6'
id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.dku'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
sourceCompatibility = '11'

configurations {
compileOnly {
Expand All @@ -22,6 +22,20 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
// jwt
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'

implementation 'org.springframework.boot:spring-boot-starter-data-redis:2.3.1.RELEASE' // redis

// s3 storage
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.281'

// swagger
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/dku/springstudy/SpringConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dku.springstudy;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import static com.dku.springstudy.SpringConfig.BASE_PACKAGE;

@Configuration
@EnableJpaRepositories(basePackages = {BASE_PACKAGE})
public class SpringConfig {
static final String BASE_PACKAGE = "com.dku.springstudy.repository.jpa";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableJpaAuditing
@SpringBootApplication
public class SpringStudyApplication {

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/dku/springstudy/config/AwsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.dku.springstudy.config;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AwsConfig {
@Value("${cloud.aws.credentials.accessKey}")
private String accessKey;

@Value("${cloud.aws.credentials.secretKey}")
private String secretKey;

@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3 amazonS3() {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
return AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/dku/springstudy/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.dku.springstudy.config;

import com.dku.springstudy.security.JwtAuthenticationFilter;
import com.dku.springstudy.security.JwtTokenProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.SecurityConfigurer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@EnableWebSecurity
@Configuration
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtTokenProvider jwtTokenProvider;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.csrf().disable()
.httpBasic().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/member/login", "/member/join", "/member/reissue", "/swagger-ui/**", "/swagger-ui.html", "/api/usage").permitAll()
.antMatchers("/swagger-resources/**", "/swagger-ui.html", "/webjars/**", "/v2/**").permitAll()
// .antMatchers("/item/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class)
.build();
}



@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

}
58 changes: 58 additions & 0 deletions src/main/java/com/dku/springstudy/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.dku.springstudy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.HashSet;
import java.util.Set;

@Configuration
@EnableWebMvc
public class SwaggerConfig {
private ApiInfo swaggerInfo() {
return new ApiInfoBuilder()
.title("Karrot API")
.description("Karrot API Docs")
.build();
}

@Bean
public Docket swaggerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.consumes(getConsumeContentTypes())
.produces(getProduceContentTypes())
.apiInfo(swaggerInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.dku.springstudy.controller"))
.paths(PathSelectors.any())
.build()
.useDefaultResponseMessages(false);
}

@Bean
public InternalResourceViewResolver defaultViewResolver() {
return new InternalResourceViewResolver();
}

private Set<String> getConsumeContentTypes() {
Set<String> consumes = new HashSet<>();
consumes.add("application/json;charset=UTF-8");
consumes.add("application/x-www-form-urlencoded");
return consumes;
}

private Set<String> getProduceContentTypes() {
Set<String> produces = new HashSet<>();
produces.add("application/json;charset=UTF-8");
return produces;
}

}
30 changes: 30 additions & 0 deletions src/main/java/com/dku/springstudy/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.dku.springstudy.config;

import com.dku.springstudy.security.ResponseInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

private final ResponseInterceptor responseInterceptor;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowedOrigins("*");
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry
.addInterceptor(responseInterceptor)
.addPathPatterns("/**");
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.dku.springstudy.controller;

import com.dku.springstudy.domain.ImageFile;
import com.dku.springstudy.domain.Item;
import com.dku.springstudy.dto.ItemDto;
import com.dku.springstudy.service.ItemService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
public class HomeController {

private final ItemService itemService;

@ApiOperation(value = "홈", notes = "등록된 전체 상품 리스트 반환")
@ApiResponses({
@ApiResponse(code = 200, message = "API 정상 작동")
}
)
@GetMapping("/home")
public List<ItemDto> home(){
List<Item> allItems = itemService.findAll();
return allItems.stream()
.map(i -> new ItemDto(
i.getId(),
i.getImages().stream().map(ImageFile::getImageUrl).collect(Collectors.toList()),
i.getTitle(),
i.getContent(),
i.getPrice(),
i.getLikes().size(),
i.getStatus())
)
.collect(Collectors.toList());
}

}
Loading