From 69d4b79bbd824de443ea702ba373bf9d4aa6c81c Mon Sep 17 00:00:00 2001 From: "Yuan, Harry Hongbin" Date: Mon, 18 Mar 2024 22:58:06 +0100 Subject: [PATCH] add twitter-screen-name (#210) --- .../com/dl/officialsite/member/Member.java | 22 +++++++--- .../officialsite/member/MemberController.java | 3 ++ .../oauth2/config/OAuthSessionKey.java | 3 +- .../oauth2/controller/TwitterController.java | 41 ++++++++----------- 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/dl/officialsite/member/Member.java b/src/main/java/com/dl/officialsite/member/Member.java index 5a59de84..5b385c38 100644 --- a/src/main/java/com/dl/officialsite/member/Member.java +++ b/src/main/java/com/dl/officialsite/member/Member.java @@ -4,19 +4,28 @@ import com.dl.officialsite.common.privacy.PrivacyEncrypt; import com.dl.officialsite.common.privacy.PrivacyTypeEnum; import com.fasterxml.jackson.annotation.JsonIgnore; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; import org.hibernate.annotations.DynamicUpdate; - import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; -import javax.persistence.*; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityListeners; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; import javax.validation.constraints.NotNull; import java.io.Serializable; -import static com.dl.officialsite.common.privacy.PrivacyTypeEnum.CUSTOMER; - @Getter @Setter @AllArgsConstructor @@ -52,6 +61,9 @@ public class Member implements Serializable private String githubId; @Column(length = 20) private String tweetId; + + @Column(unique=true,length = 64) + private String tweetScreenName; @Column(length = 20) private String telegramId; @Column(length = 20) diff --git a/src/main/java/com/dl/officialsite/member/MemberController.java b/src/main/java/com/dl/officialsite/member/MemberController.java index 2cbc604e..34c0441b 100644 --- a/src/main/java/com/dl/officialsite/member/MemberController.java +++ b/src/main/java/com/dl/officialsite/member/MemberController.java @@ -131,6 +131,7 @@ public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, public BaseResponse createMember(@Valid @RequestBody Member member, @RequestParam String address, HttpServletRequest request) { member.setGithubId(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.GITHUB_USER_NAME)); member.setTweetId(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_USER_NAME)); + member.setTweetScreenName(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_SCREEN_NAME)); this.setOAuthUserName(request.getSession(), member); @@ -153,6 +154,8 @@ public BaseResponse updateMemberByAddress(@RequestParam String address, @Request .ifPresent(githubUserName -> _member.setGithubId(githubUserName)); Optional.ofNullable(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_USER_NAME)) .ifPresent(twitterUserName -> _member.setTweetId(twitterUserName)); + Optional.ofNullable(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_SCREEN_NAME)) + .ifPresent(twitterScreenName -> _member.setTweetScreenName(twitterScreenName)); if (member.getWechatId() != null) { _member.setWechatId(member.getWechatId()); diff --git a/src/main/java/com/dl/officialsite/oauth2/config/OAuthSessionKey.java b/src/main/java/com/dl/officialsite/oauth2/config/OAuthSessionKey.java index 706f17bd..4a5bef1d 100644 --- a/src/main/java/com/dl/officialsite/oauth2/config/OAuthSessionKey.java +++ b/src/main/java/com/dl/officialsite/oauth2/config/OAuthSessionKey.java @@ -14,7 +14,8 @@ @AllArgsConstructor public enum OAuthSessionKey { GITHUB_USER_NAME("GITHUB_USER_NAME"), - TWITTER_USER_NAME("TWITTER_USER_NAME") + TWITTER_USER_NAME("TWITTER_USER_NAME"), + TWITTER_SCREEN_NAME("TWITTER_SCREEN_NAME") ; private String sessionKey; diff --git a/src/main/java/com/dl/officialsite/oauth2/controller/TwitterController.java b/src/main/java/com/dl/officialsite/oauth2/controller/TwitterController.java index b382708f..1bc03265 100644 --- a/src/main/java/com/dl/officialsite/oauth2/controller/TwitterController.java +++ b/src/main/java/com/dl/officialsite/oauth2/controller/TwitterController.java @@ -7,6 +7,7 @@ import com.dl.officialsite.oauth2.config.OAuthConfig; import com.dl.officialsite.oauth2.config.OAuthSessionKey; import com.dl.officialsite.oauth2.config.RegistrationConfig; +import com.nimbusds.jose.util.Pair; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.social.oauth1.AuthorizedRequestToken; @@ -50,10 +51,8 @@ public void setUpTwitter() { } @GetMapping("/oauth2/authorize/normal/twitter") - public void twitterOauthLogin( - @RequestParam(name = "test", defaultValue = "false") boolean test, - HttpServletResponse response - ) throws IOException { + public void twitterOauthLogin(@RequestParam(name = "test", defaultValue = "false") boolean test, HttpServletResponse response) + throws IOException { String authorizeUrl = this.generateAuthorizeUrl(test); response.sendRedirect(authorizeUrl); } @@ -63,9 +62,9 @@ private String generateAuthorizeUrl(boolean test) { OAuthToken requestToken = oauthOperations.fetchRequestToken(twitterConfig.getCallbackUrl(), null); OAuth1Parameters oAuth1Parameters = new OAuth1Parameters(new HashMap<>()); - if (test){ + if (test) { oAuth1Parameters.add("secret", requestToken.getSecret()); - }else{ + } else { // Save user's oauth_token_secret for exchanging the profile UserSecurityUtils.getUserLogin().setTwitterOauthTokenSecret(requestToken.getSecret()); } @@ -74,34 +73,30 @@ private String generateAuthorizeUrl(boolean test) { } @GetMapping("/oauth2/callback/twitter") - public BaseResponse getTwitter( - @RequestParam("oauth_token") String oauthToken, - @RequestParam("oauth_verifier") String oauthVerifier, - @RequestParam(value = "secret", required = false) String secret, - HttpServletRequest request - ) { - String twitterUserName = fetchProfile(oauthToken, oauthVerifier, secret); - HttpSessionUtils.setOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_USER_NAME, twitterUserName); - return BaseResponse.successWithData(twitterUserName); + public BaseResponse getTwitter(@RequestParam("oauth_token") String oauthToken, @RequestParam("oauth_verifier") String oauthVerifier, + @RequestParam(value = "secret", required = false) String secret, HttpServletRequest request) { + Pair twitterUserNameAndScreenName = fetchProfile(oauthToken, oauthVerifier, secret); + HttpSessionUtils.setOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_USER_NAME, twitterUserNameAndScreenName.getLeft()); + HttpSessionUtils.setOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_SCREEN_NAME, + twitterUserNameAndScreenName.getRight()); + return BaseResponse.successWithData(twitterUserNameAndScreenName.getLeft()); } - private String fetchProfile(String oAuthToken, String verifier, String secret) { + private Pair fetchProfile(String oAuthToken, String verifier, String secret) { OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations(); String twitterOauthTokenSecret = Optional.ofNullable(secret).orElseGet(() -> UserSecurityUtils.getUserLogin().getTwitterOauthTokenSecret()); - OAuthToken accessToken = oauthOperations.exchangeForAccessToken(new AuthorizedRequestToken( - new OAuthToken(oAuthToken, twitterOauthTokenSecret), verifier), OAuth1Parameters.NONE); + OAuthToken accessToken = oauthOperations.exchangeForAccessToken( + new AuthorizedRequestToken(new OAuthToken(oAuthToken, twitterOauthTokenSecret), verifier), OAuth1Parameters.NONE); - Twitter twitter = new TwitterTemplate(twitterConfig.getClientId(), - twitterConfig.getClientSecret(), - accessToken.getValue(), + Twitter twitter = new TwitterTemplate(twitterConfig.getClientId(), twitterConfig.getClientSecret(), accessToken.getValue(), accessToken.getSecret()); TwitterProfile profile = twitter.userOperations().getUserProfile(); - log.info("User's name:[{}]", profile.getName()); - return profile.getName(); + log.info("User's name:[{} : {}]", profile.getName(), profile.getScreenName()); + return Pair.of(profile.getName(), profile.getScreenName()); } // public static void main(String[] args) {