-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-feature
- Loading branch information
Showing
36 changed files
with
382 additions
and
38 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
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
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
74 changes: 74 additions & 0 deletions
74
src/main/java/com/workduo/configuration/jwt/JwtExceptionHandlerFilter.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,74 @@ | ||
package com.workduo.configuration.jwt; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import com.workduo.error.global.exception.UsernameFromTokenException; | ||
import com.workduo.error.global.type.GlobalExceptionType; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.time.LocalDateTime; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.workduo.error.global.type.GlobalExceptionType.responseJsonString; | ||
|
||
@Slf4j | ||
@Component | ||
public class JwtExceptionHandlerFilter extends OncePerRequestFilter { | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
try{ | ||
filterChain.doFilter(request,response); | ||
} catch (UsernameFromTokenException ex){ | ||
log.error("exception exception handler filter"); | ||
setErrorResponse(HttpStatus.BAD_REQUEST,request,response,ex); | ||
}catch (RuntimeException ex){ | ||
log.error("runtime exception exception handler filter"); | ||
setErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR,request,response,ex); | ||
} | ||
} | ||
|
||
public void setErrorResponse(HttpStatus status,HttpServletRequest request, HttpServletResponse response,Throwable ex){ | ||
response.setStatus(status.value()); | ||
response.setContentType("application/json"); | ||
JwtErrorResponse generate = generate(status, request, ex); | ||
ObjectMapper obj = new ObjectMapper(); | ||
obj.registerModule(new JavaTimeModule()); | ||
try{ | ||
response.getWriter().write(obj.writeValueAsString(generate)); | ||
}catch (IOException e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public JwtErrorResponse generate(HttpStatus status,HttpServletRequest request,Throwable ex){ | ||
return new JwtErrorResponse(request.getRequestURL().toString(), | ||
ex.getMessage(),status.value()); | ||
} | ||
@Getter | ||
@Setter | ||
class JwtErrorResponse { | ||
private String path; | ||
private String error; | ||
private String timestamp; | ||
private int status; | ||
|
||
public JwtErrorResponse(String path, String error, int status) { | ||
this.path = path; | ||
this.error = error; | ||
this.timestamp = LocalDateTime.now().toString(); | ||
this.status = status; | ||
} | ||
} | ||
} |
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
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
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/workduo/configuration/swagger/SwaggerConfig.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,33 @@ | ||
package com.workduo.configuration.swagger; | ||
|
||
import io.swagger.v3.oas.models.Components; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.info.Info; | ||
import io.swagger.v3.oas.models.security.SecurityRequirement; | ||
import io.swagger.v3.oas.models.security.SecurityScheme; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.Arrays; | ||
|
||
@Configuration | ||
public class SwaggerConfig { | ||
@Bean | ||
public OpenAPI openAPI(){ | ||
Info information = new Info() | ||
.title("WorkDuo API 문서 ") | ||
.version("1.0") | ||
.description("WorkDuo 서비스 는 운동 을 같이하고 싶은 사람들 의 모임을 지원하는 API 서비스 입니다."); | ||
|
||
SecurityScheme securityScheme = new SecurityScheme() | ||
.type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT") | ||
.in(SecurityScheme.In.HEADER).name("Authorization"); | ||
|
||
var securitySchemeReq = new SecurityRequirement().addList("bearerAuth"); | ||
|
||
return new OpenAPI() | ||
.components(new Components().addSecuritySchemes("bearerAuth",securityScheme)) | ||
.security(Arrays.asList(securitySchemeReq)) | ||
.info(information); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/workduo/error/global/exception/UsernameFromTokenException.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,7 @@ | ||
package com.workduo.error.global.exception; | ||
|
||
public class UsernameFromTokenException extends RuntimeException{ | ||
public UsernameFromTokenException(String message){ | ||
super(message); | ||
} | ||
} |
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
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
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
Oops, something went wrong.