diff --git a/build.gradle b/build.gradle index e8c0482..c46667c 100644 --- a/build.gradle +++ b/build.gradle @@ -43,6 +43,11 @@ dependencies { // gson implementation 'com.google.code.gson:gson:2.9.0' + //implementation 'io.awspring.cloud:spring-cloud-starter-aws:2.3.5' + //implementation 'org.springframework.cloud:spring-cloud-start-aws:2.0.1.RELEASE' + //implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' + + implementation 'org.json:json:20200518' diff --git a/src/main/java/com/example/neoul/controller/AuthController.java b/src/main/java/com/example/neoul/controller/AuthController.java index 723d7d1..4e88179 100644 --- a/src/main/java/com/example/neoul/controller/AuthController.java +++ b/src/main/java/com/example/neoul/controller/AuthController.java @@ -2,6 +2,7 @@ import com.example.neoul.dto.TokenRes; import com.example.neoul.dto.UserReq; +import com.example.neoul.global.entity.ApiResponse; import com.example.neoul.global.entity.BaseEntity; import com.example.neoul.global.exception.BadRequestException; import com.example.neoul.service.AuthService; @@ -29,12 +30,12 @@ public class AuthController { "카카오의 access_token을 반환 후" + "access_token을 /kakao/login의 요청에 넣어서 우리 사이트의 로그인하고 그 결과를 얻음") @GetMapping("/kakao") - public String getAccessTokenKakao(@RequestParam String code) { + public ApiResponse getAccessTokenKakao(@RequestParam String code) { String accessToken=authService.getKakaoAccessToken(code); System.out.println("code for kakaoServer : " + code); System.out.println("for /oauth/kakao : " + accessToken); - return accessToken; + return new ApiResponse<>(accessToken); } @ApiOperation(value = "카카오 로그인", notes = "카카오 로그인") diff --git a/src/main/java/com/example/neoul/controller/UserController.java b/src/main/java/com/example/neoul/controller/UserController.java index de5b7b0..605bd44 100644 --- a/src/main/java/com/example/neoul/controller/UserController.java +++ b/src/main/java/com/example/neoul/controller/UserController.java @@ -1,14 +1,17 @@ package com.example.neoul.controller; +import com.example.neoul.dto.TokenRes; +import com.example.neoul.dto.UserReq; +import com.example.neoul.dto.UserRes; +import com.example.neoul.entity.user.User; +import com.example.neoul.global.entity.ApiResponse; import com.example.neoul.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; -import java.io.UnsupportedEncodingException; - @RequiredArgsConstructor @RestController @@ -19,38 +22,33 @@ public class UserController { private final UserService userService; -// @ApiOperation(value = "현재 로그인 유저", notes = "현재 로그인 유저") -// @GetMapping("/now") -// public BaseResponse now(){ -// -// if(userService.findNowLoginUser() != null){ -// return BaseResponse.ok(SUCCESS, userService.findNowLoginUser()); -// } -// -// return BaseResponse.ok(USER_NEED_TO_LOGIN); -// } + @ApiOperation(value = "현재 로그인 유저(이거 지금 실행x)", notes = "현재 로그인 유저") + @GetMapping("/now") + public ApiResponse now(){ + return new ApiResponse<>(userService.findNowLoginUser()); + } -// @ApiOperation(value = "회원가입", notes = "회원가입") -// @PostMapping("/signup") -// public BaseResponse signup(@RequestBody UserReq.SignupUserReq signupUserReq) { -// -// if(!signupUserReq.isEmailVerified()) -// return BaseResponse.ok(USER_EMAIL_VERIFIED_FALSE); -// -// return BaseResponse.ok(SUCCESS, userService.signup(signupUserReq)); -// } -// -// -// -// -// @ApiOperation(value = "로그인", notes = "로그인") -// @PostMapping("/login") -// public BaseResponse signup(@RequestBody UserReq.LoginUserReq loginUserReq) { -// -// return BaseResponse.ok(SUCCESS, userService.login(loginUserReq)); -// } -// + @ApiOperation(value = "회원가입", notes = "회원가입") + @PostMapping("/signup") + public ApiResponse signup(@RequestBody UserReq.SignupUserDto signupUserDto) { + //이메일 형식 체크 + + //비밀번호 형식 체크 + + + return new ApiResponse<>(userService.signup(signupUserDto)); + } + + + + + @ApiOperation(value = "로그인", notes = "로그인") + @PostMapping("/login") + public ApiResponse signup(@RequestBody UserReq.LoginUserDto loginUserDto) { + return new ApiResponse<>(userService.login(loginUserDto)); + } + // // @ApiOperation(value = "회원 탈퇴", notes = "회원 탈퇴") // @GetMapping("/withdrawal") diff --git a/src/main/java/com/example/neoul/dto/TokenRes.java b/src/main/java/com/example/neoul/dto/TokenRes.java index 4cb061a..bf3b419 100644 --- a/src/main/java/com/example/neoul/dto/TokenRes.java +++ b/src/main/java/com/example/neoul/dto/TokenRes.java @@ -11,6 +11,7 @@ @Getter public class TokenRes { private String username; + private boolean firstLogin; private String accessToken; private String refreshToken; } diff --git a/src/main/java/com/example/neoul/dto/UserReq.java b/src/main/java/com/example/neoul/dto/UserReq.java index b25cc61..6064582 100644 --- a/src/main/java/com/example/neoul/dto/UserReq.java +++ b/src/main/java/com/example/neoul/dto/UserReq.java @@ -10,7 +10,7 @@ public class UserReq { @AllArgsConstructor @Setter @Getter - public static class LoginUserReq { + public static class LoginUserDto { @ApiModelProperty(example = "test1@naver.com") private String username; @ApiModelProperty(example = "test1234") @@ -22,7 +22,7 @@ public static class LoginUserReq { @AllArgsConstructor @Setter @Getter - public static class SocialLoginUserReq { + public static class SocialLoginUserDto { @ApiModelProperty(example = "test@gmail.com") private String username; @ApiModelProperty(example = "kakao") @@ -37,14 +37,11 @@ public static class SocialLoginUserReq { @AllArgsConstructor @Setter @Getter - public static class SignupUserReq { + public static class SignupUserDto { private String username; private String password; private String name; private String imageUrl; - - @ApiModelProperty(example = "true or false") - private boolean emailVerified; } @@ -53,7 +50,7 @@ public static class SignupUserReq { @AllArgsConstructor @Setter @Getter - public static class EmailAuthReq { + public static class EmailAuthDto { private String email; } diff --git a/src/main/java/com/example/neoul/dto/UserRes.java b/src/main/java/com/example/neoul/dto/UserRes.java index c44d965..6c62907 100644 --- a/src/main/java/com/example/neoul/dto/UserRes.java +++ b/src/main/java/com/example/neoul/dto/UserRes.java @@ -1,5 +1,6 @@ package com.example.neoul.dto; +import com.example.neoul.entity.user.User; import lombok.*; public class UserRes { @@ -16,6 +17,31 @@ public static class SignupUserRes { } + @Builder + @NoArgsConstructor + @AllArgsConstructor + @Setter + @Getter + public static class UserDetailDto { + private String username; + private String name; + private String phone; + private String imageUrl; + private String social; + private boolean firstLogin; + + public static UserDetailDto toDto(User user){ + return UserDetailDto.builder() + .username(user.getUsername()) + .name(user.getName()) + .phone(user.getPhone()) + .imageUrl(user.getImageUrl()) + .social(user.getSocial()) + .firstLogin(user.isFirstLogin()) + .build(); + } + } + @Builder diff --git a/src/main/java/com/example/neoul/global/exception/ServerErrorException.java b/src/main/java/com/example/neoul/global/exception/ServerErrorException.java index 30e0d7c..b5b8115 100644 --- a/src/main/java/com/example/neoul/global/exception/ServerErrorException.java +++ b/src/main/java/com/example/neoul/global/exception/ServerErrorException.java @@ -5,5 +5,8 @@ public class ServerErrorException extends RuntimeException { public ServerErrorException() { super("서버 내부 에러입니다."); } + + public ServerErrorException(String s) { + } } diff --git a/src/main/java/com/example/neoul/global/jwt/TokenProvider.java b/src/main/java/com/example/neoul/global/jwt/TokenProvider.java index 7d761f9..debbdac 100644 --- a/src/main/java/com/example/neoul/global/jwt/TokenProvider.java +++ b/src/main/java/com/example/neoul/global/jwt/TokenProvider.java @@ -84,14 +84,14 @@ public void afterPropertiesSet() { // .compact(); // } - public String createToken(Long userIdx) { + public String createToken(Long userId) { long now = (new Date()).getTime(); Date validity = new Date(now + this.accessTime); return Jwts.builder() //.setSubject(authentication.getName()) - .claim("userIdx",userIdx) + .claim("userId",userId) .setIssuedAt(new Date()) //.claim(AUTHORITIES_KEY, authorities) .signWith(SignatureAlgorithm.HS512, secret) @@ -99,14 +99,14 @@ public String createToken(Long userIdx) { .compact(); } - public String createRefreshToken(Long userIdx) { + public String createRefreshToken(Long userId) { long now = (new Date()).getTime(); Date validity = new Date(now + this.refreshTime); return Jwts.builder() //.setSubject(authentication.getName()) - .claim("userIdx",userIdx) + .claim("userId",userId) .setIssuedAt(new Date()) //.claim(AUTHORITIES_KEY, authorities) .signWith(SignatureAlgorithm.HS512, refreshSecret) @@ -115,12 +115,12 @@ public String createRefreshToken(Long userIdx) { } - public GenerateToken createAllToken(Long userIdx){ - String accessToken=createToken(userIdx); - String refreshToken=createRefreshToken(userIdx); + public GenerateToken createAllToken(Long userId){ + String accessToken = createToken(userId); + String refreshToken = createRefreshToken(userId); // //redis에 리프레시토큰 저장 -// redisService.saveToken(String.valueOf(userIdx),refreshToken, (System.currentTimeMillis()+ refreshTime*1000)); +// redisService.saveToken(String.valueOf(userId),refreshToken, (System.currentTimeMillis()+ refreshTime*1000)); return new GenerateToken(accessToken,refreshToken); } diff --git a/src/main/java/com/example/neoul/service/AuthService.java b/src/main/java/com/example/neoul/service/AuthService.java index e68da48..d1f3af1 100644 --- a/src/main/java/com/example/neoul/service/AuthService.java +++ b/src/main/java/com/example/neoul/service/AuthService.java @@ -136,19 +136,27 @@ public TokenRes createAndLoginKakaoUser(UserReq.SocialReq socialReq) { //카카오 계정의 이메일과 현재 일반 회원가입한 이메일 중에서 같은 것이 없다면 카카오 계정의 이메일로 회원가입 if(!userRepository.existsByUsernameAndSocial(String.valueOf(email),socialReq.getSocial())){ - User user= User.toSocialLoginUser(String.valueOf(email),socialReq.getSocial(),name); + User user = User.toSocialLoginUser(String.valueOf(email),socialReq.getSocial(),name); userRepository.save(user); } br.close(); //로그인 - User user=userRepository.findByUsernameAndSocial(String.valueOf(email),socialReq.getSocial()); - Long userIdx=user.getUserId(); + User user = userRepository.findByUsernameAndSocial(String.valueOf(email),socialReq.getSocial()); + Long userId = user.getUserId(); - GenerateToken generateToken=tokenProvider.createAllToken(userIdx); + GenerateToken generateToken = tokenProvider.createAllToken(userId); - return new TokenRes(name,generateToken.getAccessToken(),generateToken.getRefreshToken()); + boolean isFirstLogin = user.isFirstLogin(); + user.setFirstLogin(false); //첫 로그인 이후는 전부 0으로 바꾸기 + + return TokenRes.builder() + .username(user.getUsername()) + .firstLogin(isFirstLogin) + .accessToken(generateToken.getAccessToken()) + .refreshToken(generateToken.getRefreshToken()) + .build(); } catch (IOException e) { diff --git a/src/main/java/com/example/neoul/service/UserService.java b/src/main/java/com/example/neoul/service/UserService.java index 53a7269..d5565cc 100644 --- a/src/main/java/com/example/neoul/service/UserService.java +++ b/src/main/java/com/example/neoul/service/UserService.java @@ -4,8 +4,11 @@ import com.example.neoul.dto.GenerateToken; import com.example.neoul.dto.TokenRes; import com.example.neoul.dto.UserReq; +import com.example.neoul.dto.UserRes; import com.example.neoul.entity.user.Authority; import com.example.neoul.entity.user.User; +import com.example.neoul.global.exception.BadRequestException; +import com.example.neoul.global.exception.ServerErrorException; import com.example.neoul.global.jwt.JwtFilter; import com.example.neoul.global.jwt.TokenProvider; import com.example.neoul.repository.UserRepository; @@ -24,6 +27,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; @RequiredArgsConstructor @@ -44,11 +48,11 @@ public User findNowLoginUser(){ String username = SecurityUtil.getCurrentUsername().orElse(null); - if(username != null){ - return userRepository.findUserByUsername(username).get(); + if(username == null){ + throw new ServerErrorException("accessToken이 없습니다"); } - return null; + return userRepository.findUserByUsername(username).get(); } public List giveUSER(){ @@ -73,18 +77,11 @@ public List giveADMIN(){ } - public List giveWRITER(List authorityList){ - Authority roleWriter = new Authority("ROLE_WRITER"); - authorityList.add(roleWriter); - - return authorityList; - } - @Transactional - public User signup(UserReq.SignupUserReq signupUserReq) { - if (userRepository.findUserWithAuthoritiesByUsername(signupUserReq.getUsername()).orElse(null) != null) { - throw new RuntimeException("이미 가입되어 있는 유저입니다."); + public UserRes.UserDetailDto signup(UserReq.SignupUserDto signupUserDto) { + if (userRepository.findUserWithAuthoritiesByUsername(signupUserDto.getUsername()).orElse(null) != null) { + throw new BadRequestException("이미 가입되어 있는 유저입니다."); } Authority authority = Authority.builder() @@ -92,73 +89,89 @@ public User signup(UserReq.SignupUserReq signupUserReq) { .build(); User user = User.builder() - .username(signupUserReq.getUsername()) - .password(passwordEncoder.encode(signupUserReq.getPassword())) - .name(signupUserReq.getName()) - .imageUrl(signupUserReq.getImageUrl()) + .username(signupUserDto.getUsername()) + .password(passwordEncoder.encode(signupUserDto.getPassword())) + .name(signupUserDto.getName()) + .imageUrl(signupUserDto.getImageUrl()) + .authorities(Collections.singletonList(authority)) .build(); userRepository.save(user); - return user; + return UserRes.UserDetailDto.toDto(user); } - public User signupADMIN(UserReq.SignupUserReq signupUserReq) { - if (userRepository.findUserWithAuthoritiesByUsername(signupUserReq.getUsername()).orElse(null) != null) { - throw new RuntimeException("이미 가입되어 있는 관리자입니다."); - } - - Authority roleAdmin = new Authority("ROLE_ADMIN"); - Authority roleUser = new Authority("ROLE_USER"); - - - List authorityList = giveADMIN(); - - - User user = User.builder() - .username(signupUserReq.getUsername()) - .password(passwordEncoder.encode(signupUserReq.getPassword())) - .name(signupUserReq.getName()) - .imageUrl(signupUserReq.getImageUrl()) - //.authorities(Collections.singletonList(authority)) - .authorities(authorityList) - .build(); - - userRepository.save(user); - - return user; - - } +// public User signupADMIN(UserReq.SignupUserDto signupUserDto) { +// if (userRepository.findUserWithAuthoritiesByUsername(signupUserDto.getUsername()).orElse(null) != null) { +// throw new RuntimeException("이미 가입되어 있는 관리자입니다."); +// } +// +// Authority roleAdmin = new Authority("ROLE_ADMIN"); +// Authority roleUser = new Authority("ROLE_USER"); +// +// +// List authorityList = giveADMIN(); +// +// +// User user = User.builder() +// .username(signupUserDto.getUsername()) +// .password(passwordEncoder.encode(signupUserDto.getPassword())) +// .name(signupUserDto.getName()) +// .imageUrl(signupUserDto.getImageUrl()) +// //.authorities(Collections.singletonList(authority)) +// .authorities(authorityList) +// .build(); +// +// userRepository.save(user); +// +// return user; +// +// } @Transactional - public TokenRes login(UserReq.LoginUserReq loginUserReq){ + public TokenRes login(UserReq.LoginUserDto loginUserDto){ + Optional optionalUser = userRepository.findUserByUsername(loginUserDto.getUsername()); + if(optionalUser.isPresent()) { + User user = optionalUser.get(); + if(passwordEncoder.matches(loginUserDto.getPassword(), user.getPassword())) { - //이메일 존재 여부 체크 + //인증토큰 생성 + UsernamePasswordAuthenticationToken authenticationToken = + new UsernamePasswordAuthenticationToken(loginUserDto.getUsername(), loginUserDto.getPassword()); - //비밀번호 동일 여부 체크 + Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken); + SecurityContextHolder.getContext().setAuthentication(authentication); //SecurityContext에 저장 - //인증토큰 생성 - UsernamePasswordAuthenticationToken authenticationToken = - new UsernamePasswordAuthenticationToken(loginUserReq.getUsername(), loginUserReq.getPassword()); - Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken); - SecurityContextHolder.getContext().setAuthentication(authentication); //SecurityContext에 저장 + User loginUser = optionalUser.get(); + Long userId = loginUser.getUserId(); + GenerateToken generateToken = tokenProvider.createAllToken(userId); - //String jwt = tokenProvider.createToken(authentication); //인증토큰으로 jwt토큰 생성 + boolean isFirstLogin = user.isFirstLogin(); + user.setFirstLogin(false); //첫 로그인 이후는 전부 0으로 바꾸기 - User loginUser = userRepository.findUserByUsername(loginUserReq.getUsername()).get(); - Long userIdx = loginUser.getUserId(); - GenerateToken generateToken = tokenProvider.createAllToken(userIdx); +// HttpHeaders httpHeaders = new HttpHeaders(); +// httpHeaders.add(JwtFilter.AUTHORIZATION_HEADER, "Bearer " + generateToken.getAccessToken()); +// - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.add(JwtFilter.AUTHORIZATION_HEADER, "Bearer " + generateToken.getAccessToken()); - return new TokenRes(loginUser.getUsername(), generateToken.getAccessToken(), generateToken.getRefreshToken()); + return TokenRes.builder() + .username(loginUser.getUsername()) + .firstLogin(isFirstLogin) + .accessToken(generateToken.getAccessToken()) + .refreshToken(generateToken.getRefreshToken()) + .build(); + } else{ + throw new BadRequestException("비밀번호를 다시 입력해주세요"); + } + } else{ + throw new BadRequestException("존재하지 않는 회원입니다"); + } }