-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
PR: 자체 회원 가입 및 이메일 인증 기능 구현
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.stockholm.main_project.auth.erroesponse; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@Setter | ||
public class ErrorResponse { | ||
private int status; | ||
private String error; | ||
private String message; | ||
|
||
public static ErrorResponse of(HttpStatus httpStatus) { | ||
ErrorResponse errorResponse = new ErrorResponse(); | ||
errorResponse.setStatus(httpStatus.value()); | ||
errorResponse.setError(httpStatus.getReasonPhrase()); | ||
errorResponse.setMessage("Authentication failed"); | ||
return errorResponse; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.stockholm.main_project.auth.filter; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.stockholm.main_project.auth.jwt.JwtTokenizer; | ||
import com.stockholm.main_project.auth.logindto.LoginDto; | ||
import com.stockholm.main_project.member.entity.Member; | ||
import lombok.SneakyThrows; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class JwtAuthenticationFilter extends UsernamePasswordAuthenticationFilter { | ||
private final AuthenticationManager authenticationManager; | ||
private final JwtTokenizer jwtTokenizer; | ||
|
||
public JwtAuthenticationFilter(AuthenticationManager authenticationManager, JwtTokenizer jwtTokenizer) { | ||
this.authenticationManager = authenticationManager; | ||
this.jwtTokenizer = jwtTokenizer; | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
LoginDto loginDto = objectMapper.readValue(request.getInputStream(), LoginDto.class); | ||
UsernamePasswordAuthenticationToken authenticationToken = | ||
new UsernamePasswordAuthenticationToken(loginDto.getEmail(), loginDto.getPassword()); | ||
|
||
return authenticationManager.authenticate(authenticationToken); | ||
} | ||
|
||
@Override | ||
protected void successfulAuthentication(HttpServletRequest request, | ||
HttpServletResponse response, | ||
FilterChain chain, | ||
Authentication authResult) throws ServletException, IOException { | ||
Member member = (Member) authResult.getPrincipal(); | ||
|
||
String accessToken = delegateAccessToken(member); | ||
String refreshToken = delegateRefreshToken(member); | ||
|
||
response.setHeader("Authorization", "Bearer " + accessToken); | ||
response.setHeader("Refresh", refreshToken); | ||
//response.setHeader("Access-Control-Expose-Headers", "Authorization"); | ||
//response.addHeader("Access-Control-Expose-Headers", "MemberId"); | ||
|
||
this.getSuccessHandler().onAuthenticationSuccess(request, response, authResult); | ||
} | ||
|
||
|
||
private String delegateAccessToken(Member member) { | ||
Map<String, Object> claims = new HashMap<>(); | ||
claims.put("memberEmail", member.getEmail()); | ||
claims.put("roles", member.getRoles()); | ||
|
||
String subject = member.getEmail(); | ||
Date expiration = jwtTokenizer.getTokenExpiration(jwtTokenizer.getAccessTokenExpirationMinutes()); | ||
|
||
String base64EncodedSecretKey = jwtTokenizer.encodeBase64SecretKey(jwtTokenizer.getSecretKey()); | ||
|
||
String accessToken = jwtTokenizer.generateAccessToken(claims, subject, expiration, base64EncodedSecretKey); | ||
|
||
return accessToken; | ||
} | ||
|
||
private String delegateRefreshToken(Member member) { | ||
String subject = member.getEmail(); | ||
Date expiration = jwtTokenizer.getTokenExpiration(jwtTokenizer.getRefreshTokenExpirationMinutes()); | ||
String base64EncodedSecretKey = jwtTokenizer.encodeBase64SecretKey(jwtTokenizer.getSecretKey()); | ||
|
||
String refreshToken = jwtTokenizer.generateRefreshToken(subject, expiration, base64EncodedSecretKey); | ||
|
||
return refreshToken; | ||
} | ||
} |