-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4671afc
Showing
61 changed files
with
1,706 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
name: feature issue about: "μ΄μ κ° κ²¨ ~~~~~ \U0001F3C4ββοΈ" | ||
title: '' | ||
labels: '' | ||
assignees: '' | ||
--- | ||
|
||
## π Feature Issue | ||
<!-- ꡬνν κΈ°λ₯μ λν λ΄μ©μ μ€λͺ ν΄μ£ΌμΈμ. --> | ||
|
||
## π To-do | ||
<!-- ν΄μΌ ν μΌλ€μ μ μ΄μ£ΌμΈμ. --> | ||
|
||
- [ ] | ||
- [ ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## βοΈ κ΄λ ¨ μ΄μλ²νΈ | ||
|
||
- Closes #536 | ||
|
||
## π Key Changes | ||
|
||
1. λ΄μ© | ||
- μ€λͺ | ||
|
||
## π’ To Reviewers | ||
- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
HELP.md | ||
.gradle | ||
build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
bin/ | ||
!**/src/main/**/bin/ | ||
!**/src/test/**/bin/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
out/ | ||
!**/src/main/**/out/ | ||
!**/src/test/**/out/ | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
application.yml | ||
application-local.yml | ||
application-dev.yml | ||
application-prod.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# muyaho-api | ||
|
||
> μ ν리μΌμ΄μ λͺ¨λ κ³μΈ΅ (API) | ||
## νμ λͺ¨λ | ||
|
||
- blossom-common | ||
- blossom-domain | ||
- blossom-external |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
dependencies { | ||
implementation project(':blossom-domain') | ||
implementation project(':blossom-external') | ||
|
||
// spring mvc | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
|
||
// redis | ||
implementation "org.springframework.boot:spring-boot-starter-data-redis" | ||
implementation "org.springframework.session:spring-session-data-redis" | ||
|
||
// swagger | ||
implementation 'org.springdoc:springdoc-openapi-ui:1.5.4' | ||
} |
19 changes: 19 additions & 0 deletions
19
blossom-api/src/main/java/com/seoultech/blossom/api/ApiApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.seoultech.blossom.api; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
import com.seoultech.blossom.domain.BlossomDomainRoot; | ||
import com.seoultech.blossom.external.BlossomExternalRoot; | ||
|
||
@SpringBootApplication(scanBasePackageClasses = { | ||
BlossomDomainRoot.class, | ||
BlossomExternalRoot.class | ||
}) | ||
public class ApiApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ApiApplication.class, args); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
blossom-api/src/main/java/com/seoultech/blossom/api/config/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.seoultech.blossom.api.config; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.context.MessageSource; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.support.ReloadableResourceBundleMessageSource; | ||
import org.springframework.validation.Validator; | ||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import com.seoultech.blossom.api.config.interceptor.auth.AuthInterceptor; | ||
import com.seoultech.blossom.api.config.resolver.UserIdResolver; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
private final AuthInterceptor authInterceptor; | ||
private final UserIdResolver userIdResolver; | ||
|
||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedOrigins("*") | ||
.allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS"); | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(authInterceptor); | ||
} | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(userIdResolver); | ||
} | ||
|
||
@Bean | ||
public MessageSource validationMessageSource() { | ||
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); | ||
messageSource.setBasename("classpath:/messages/validation"); | ||
messageSource.setDefaultEncoding("UTF-8"); | ||
return messageSource; | ||
} | ||
|
||
@Override | ||
public Validator getValidator() { | ||
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean(); | ||
bean.setValidationMessageSource(validationMessageSource()); | ||
return bean; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
blossom-api/src/main/java/com/seoultech/blossom/api/config/filter/FilterConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.seoultech.blossom.api.config.filter; | ||
|
||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@Configuration | ||
public class FilterConfig { | ||
|
||
@Profile({"dev", "prod"}) | ||
@Bean | ||
public FilterRegistrationBean<RequestLoggingFilter> requestLoggingFilter() { | ||
FilterRegistrationBean<RequestLoggingFilter> filter = new FilterRegistrationBean<>(new RequestLoggingFilter()); | ||
filter.addUrlPatterns("/api/*"); | ||
filter.setOrder(1); | ||
return filter; | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
blossom-api/src/main/java/com/seoultech/blossom/api/config/filter/RequestLoggingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.seoultech.blossom.api.config.filter; | ||
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.web.util.ContentCachingRequestWrapper; | ||
import org.springframework.web.util.ContentCachingResponseWrapper; | ||
import org.springframework.web.util.WebUtils; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class RequestLoggingFilter implements Filter { | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws | ||
IOException, | ||
ServletException { | ||
ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper((HttpServletRequest)request); | ||
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper( | ||
(HttpServletResponse)response); | ||
|
||
long start = System.currentTimeMillis(); | ||
chain.doFilter(requestWrapper, responseWrapper); | ||
long end = System.currentTimeMillis(); | ||
|
||
log.info("\n" + | ||
"[REQUEST] {} - {} {} - {}s\n" + | ||
"Headers : {}\n" + | ||
"Request : {}\n" + | ||
"Response : {}\n", | ||
((HttpServletRequest)request).getMethod(), ((HttpServletRequest)request).getRequestURI(), | ||
responseWrapper.getStatus(), (end - start) / 1000.0, | ||
getHeaders((HttpServletRequest)request), | ||
getRequestBody(requestWrapper), | ||
getResponseBody(responseWrapper)); | ||
} | ||
|
||
private Map<String, String> getHeaders(HttpServletRequest request) { | ||
Map<String, String> headerMap = new HashMap<>(); | ||
|
||
Enumeration<String> headerArray = request.getHeaderNames(); | ||
while (headerArray.hasMoreElements()) { | ||
String headerName = headerArray.nextElement(); | ||
headerMap.put(headerName, request.getHeader(headerName)); | ||
} | ||
return headerMap; | ||
} | ||
|
||
private String getRequestBody(ContentCachingRequestWrapper request) { | ||
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); | ||
if (wrapper != null) { | ||
byte[] buf = wrapper.getContentAsByteArray(); | ||
if (buf.length > 0) { | ||
try { | ||
return new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); | ||
} catch (UnsupportedEncodingException e) { | ||
return " - "; | ||
} | ||
} | ||
} | ||
return " - "; | ||
} | ||
|
||
private String getResponseBody(final HttpServletResponse response) throws IOException { | ||
String payload = null; | ||
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, | ||
ContentCachingResponseWrapper.class); | ||
if (wrapper != null) { | ||
byte[] buf = wrapper.getContentAsByteArray(); | ||
if (buf.length > 0) { | ||
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); | ||
wrapper.copyBodyToResponse(); | ||
} | ||
} | ||
return payload == null ? " - " : payload; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
blossom-api/src/main/java/com/seoultech/blossom/api/config/interceptor/auth/Auth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.seoultech.blossom.api.config.interceptor.auth; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Auth { | ||
} |
34 changes: 34 additions & 0 deletions
34
...-api/src/main/java/com/seoultech/blossom/api/config/interceptor/auth/AuthInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.seoultech.blossom.api.config.interceptor.auth; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import com.seoultech.blossom.common.constant.JwtKey; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class AuthInterceptor implements HandlerInterceptor { | ||
|
||
private final LoginCheckHandler loginCheckHandler; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
if (!(handler instanceof HandlerMethod)) { | ||
return true; | ||
} | ||
HandlerMethod handlerMethod = (HandlerMethod)handler; | ||
Auth auth = handlerMethod.getMethodAnnotation(Auth.class); | ||
if (auth == null) { | ||
return true; | ||
} | ||
Long userId = loginCheckHandler.getUserId(request); | ||
request.setAttribute(JwtKey.USER_ID, userId); | ||
return true; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...pi/src/main/java/com/seoultech/blossom/api/config/interceptor/auth/LoginCheckHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.seoultech.blossom.api.config.interceptor.auth; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
|
||
import com.seoultech.blossom.common.exception.UnAuthorizedException; | ||
import com.seoultech.blossom.common.util.JwtUtils; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class LoginCheckHandler { | ||
|
||
private final JwtUtils jwtUtils; | ||
|
||
public Long getUserId(HttpServletRequest request) { | ||
String bearerToken = request.getHeader("Authorization"); | ||
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) { | ||
String accessToken = bearerToken.substring("Bearer ".length()); | ||
if (jwtUtils.validateToken(accessToken)) { | ||
Long userId = jwtUtils.getUserIdFromJwt(accessToken); | ||
if (userId != null) { | ||
return userId; | ||
} | ||
} | ||
} | ||
throw new UnAuthorizedException(String.format("μλͺ»λ JWT (%s) μ λλ€.", bearerToken)); | ||
} | ||
} |
Oops, something went wrong.