-
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.
Merge pull request #8 from DO-SOPT-SERVER/feature/7
[6주차] 기본 과제 & 심화 과제
- Loading branch information
Showing
60 changed files
with
843 additions
and
191 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 was deleted.
Oops, something went wrong.
54 changes: 0 additions & 54 deletions
54
api/src/main/java/org/sopt/api/member/api/MemberApiController.java
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
api/src/main/java/org/sopt/api/member/dto/response/MemberSaveResponse.java
This file was deleted.
Oops, something went wrong.
66 changes: 0 additions & 66 deletions
66
api/src/main/java/org/sopt/api/member/service/MemberService.java
This file was deleted.
Oops, something went wrong.
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,23 @@ | ||
dependencies { | ||
// spring boot web | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
// spring data jpa | ||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' | ||
// spring security | ||
implementation 'org.springframework.boot:spring-boot-starter-security' | ||
implementation project(path: ':seminar-infra') | ||
// h2 | ||
runtimeOnly 'com.h2database:h2' | ||
// jwt | ||
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5' | ||
implementation group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5' | ||
implementation group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5' | ||
// domain dependency | ||
implementation project(path: ':seminar-domain') | ||
// common dependency | ||
implementation project(path: ':seminar-common') | ||
} | ||
|
||
jar { | ||
enabled = false | ||
} |
File renamed without changes.
16 changes: 16 additions & 0 deletions
16
seminar-api/src/main/java/org/sopt/api/auth/BCryptPasswordConfig.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,16 @@ | ||
package org.sopt.api.auth; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
@Configuration | ||
public class BCryptPasswordConfig { | ||
private static final int STRENGTH = 10; | ||
|
||
@Bean | ||
public PasswordEncoder bCryptPasswordEncoder() { | ||
return new BCryptPasswordEncoder(STRENGTH); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
seminar-api/src/main/java/org/sopt/api/auth/ExceptionHandlerFilter.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,50 @@ | ||
package org.sopt.api.auth; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.sopt.api.common.ApiResponse; | ||
import org.sopt.api.common.Constants; | ||
import org.sopt.common.error.ErrorStatus; | ||
import org.sopt.common.error.UnauthorizedException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
public class ExceptionHandlerFilter extends OncePerRequestFilter { | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException { | ||
try { | ||
filterChain.doFilter(request, response); | ||
} catch (UnauthorizedException e) { | ||
handleUnauthorizedException(response, e); | ||
} catch (Exception ee) { | ||
handleException(response); | ||
} | ||
} | ||
|
||
private void handleUnauthorizedException(HttpServletResponse response, Exception e) throws IOException { | ||
UnauthorizedException ue = (UnauthorizedException) e; | ||
ErrorStatus errorStatus = ue.getErrorStatus(); | ||
HttpStatus httpStatus = errorStatus.getHttpStatus(); | ||
setResponse(response, httpStatus, errorStatus); | ||
} | ||
|
||
private void handleException(HttpServletResponse response) throws IOException { | ||
setResponse(response, HttpStatus.INTERNAL_SERVER_ERROR, ErrorStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
private void setResponse(HttpServletResponse response, HttpStatus httpStatus, ErrorStatus errorStatus) throws IOException { | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.setCharacterEncoding(Constants.CHARACTER_TYPE); | ||
response.setStatus(httpStatus.value()); | ||
PrintWriter writer = response.getWriter(); | ||
writer.write(objectMapper.writeValueAsString(ApiResponse.of(errorStatus))); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
seminar-api/src/main/java/org/sopt/api/auth/JwtAuthenticationEntryPoint.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,38 @@ | ||
package org.sopt.api.auth; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.sopt.api.common.ApiResponse; | ||
import org.sopt.api.common.Constants; | ||
import org.sopt.common.error.ErrorStatus; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
@Component | ||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { | ||
handleException(response); | ||
} | ||
|
||
private void handleException(HttpServletResponse response) throws IOException { | ||
setResponse(response, HttpStatus.UNAUTHORIZED, ErrorStatus.UNAUTHORIZED); | ||
} | ||
|
||
private void setResponse(HttpServletResponse response, HttpStatus httpStatus, ErrorStatus errorStatus) throws IOException { | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.setCharacterEncoding(Constants.CHARACTER_TYPE); | ||
response.setStatus(httpStatus.value()); | ||
PrintWriter writer = response.getWriter(); | ||
writer.write(objectMapper.writeValueAsString(ApiResponse.of(errorStatus))); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
seminar-api/src/main/java/org/sopt/api/auth/JwtAuthenticationFilter.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,57 @@ | ||
package org.sopt.api.auth; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.sopt.api.auth.jwt.JwtProvider; | ||
import org.sopt.api.auth.jwt.JwtValidator; | ||
import org.sopt.api.common.Constants; | ||
import org.sopt.common.error.ErrorStatus; | ||
import org.sopt.common.error.UnauthorizedException; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetails; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.sopt.api.auth.UserAuthentication.createDefaultUserAuthentication; | ||
|
||
@RequiredArgsConstructor | ||
public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
private final JwtValidator jwtValidator; | ||
private final JwtProvider jwtProvider; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
final String accessToken = getAccessToken(request); | ||
jwtValidator.validateAccessToken(accessToken); | ||
setAuthentication(request, jwtProvider.getSubject(accessToken)); | ||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private String getAccessToken(HttpServletRequest request) { | ||
String accessToken = request.getHeader(Constants.AUTHORIZATION); | ||
if (StringUtils.hasText(accessToken) && accessToken.startsWith(Constants.BEARER)) { | ||
return accessToken.substring(Constants.BEARER.length()); | ||
} | ||
throw new UnauthorizedException(ErrorStatus.INVALID_ACCESS_TOKEN); | ||
} | ||
|
||
private void setAuthentication(HttpServletRequest request, Long memberId) { | ||
UserAuthentication authentication = createDefaultUserAuthentication(memberId); | ||
createWebAuthenticationDetailsAndSet(request, authentication); | ||
SecurityContext securityContext = SecurityContextHolder.getContext(); | ||
securityContext.setAuthentication(authentication); | ||
} | ||
|
||
private void createWebAuthenticationDetailsAndSet(HttpServletRequest request, UserAuthentication authentication) { | ||
WebAuthenticationDetailsSource webAuthenticationDetailsSource = new WebAuthenticationDetailsSource(); | ||
WebAuthenticationDetails webAuthenticationDetails = webAuthenticationDetailsSource.buildDetails(request); | ||
authentication.setDetails(webAuthenticationDetails); | ||
} | ||
} |
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 org.sopt.api.auth; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface MemberId { | ||
} |
Oops, something went wrong.