forked from codestates-seb/seb44_main_026
-
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 #24 from LinaKK/ref/be/product
Ref/be/product
- Loading branch information
Showing
6 changed files
with
130 additions
and
7 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
94 changes: 94 additions & 0 deletions
94
server/src/main/java/greenNare/auth/handler/OAuth2MemberSuccessHandler.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,94 @@ | ||
package greenNare.auth.handler; | ||
|
||
import greenNare.auth.jwt.JwtTokenizer; | ||
import greenNare.auth.utils.CustomAuthorityUtils; | ||
import greenNare.cache.CacheService; | ||
import greenNare.member.entity.Member; | ||
import greenNare.member.service.MemberService; | ||
import org.springframework.cache.Cache; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.*; | ||
|
||
public class OAuth2MemberSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { | ||
private final JwtTokenizer jwtTokenizer; | ||
private final CustomAuthorityUtils customAuthorityUtils; | ||
private final MemberService memberService; | ||
|
||
private final CacheService cacheService; | ||
|
||
public OAuth2MemberSuccessHandler(JwtTokenizer jwtTokenizer, | ||
CustomAuthorityUtils customAuthorityUtils, | ||
MemberService memberService, | ||
CacheService cacheService){ | ||
this.jwtTokenizer = jwtTokenizer; | ||
this.customAuthorityUtils = customAuthorityUtils; | ||
this.memberService = memberService; | ||
this.cacheService = cacheService; | ||
} | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { | ||
var oAuth2User = (OAuth2User)authentication.getPrincipal(); | ||
String email = String.valueOf(oAuth2User.getAttributes().get("email")); | ||
|
||
//가입된 회원인지 확인 | ||
try { | ||
memberService.findMemberByEmail(email); | ||
} catch (Exception e){ | ||
//가입안된 이메일이면 DB 저장 | ||
String name = String.valueOf(oAuth2User.getAttributes().get("name")); | ||
Member newMember = new Member(); | ||
newMember.setEmail(email); | ||
newMember.setName(name); | ||
newMember.setPoint(0); | ||
memberService.createMember(newMember); | ||
} | ||
|
||
//토큰 생성해서 반환 | ||
Member member = memberService.findMemberByEmail(email); | ||
String accessToken = delegateAccessToken(member); | ||
String refreshToken = delegateRefreshToken(member); | ||
|
||
response.setHeader("Authorization", "Bearer " + accessToken); | ||
response.setHeader("Refresh", refreshToken); | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
|
||
return; | ||
} | ||
|
||
private String delegateAccessToken(Member member) { | ||
Map<String, Object> claims = new HashMap<>(); | ||
claims.put("username", member.getEmail()); | ||
claims.put("roles", member.getRoles()); | ||
claims.put("memberId", member.getMemberId()); | ||
|
||
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); | ||
cacheService.putCache("RefreshToken", Integer.toString(member.getMemberId()) ,refreshToken); | ||
|
||
return refreshToken; | ||
} | ||
|
||
} |
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
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