-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Donghak Kim
authored and
Donghak Kim
committed
Oct 21, 2023
1 parent
7202858
commit 6a4b09d
Showing
12 changed files
with
139 additions
and
165 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
25 changes: 0 additions & 25 deletions
25
src/main/java/com/gapple/weeingback/WeeingBackApplication.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 |
---|---|---|
@@ -1,37 +1,12 @@ | ||
package com.gapple.weeingback; | ||
|
||
import com.gapple.weeingback.domain.user.entity.User; | ||
import com.gapple.weeingback.domain.user.service.UserService; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
|
||
import java.awt.*; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
@SpringBootApplication | ||
public class WeeingBackApplication { | ||
public static void main(String[] args) throws IOException { | ||
SpringApplication.run(WeeingBackApplication.class, args); | ||
|
||
Scanner scanner = new Scanner(System.in); | ||
BigInteger a = scanner.nextBigInteger(); | ||
BigInteger b = scanner.nextBigInteger(); | ||
System.out.println(a.add(b)); | ||
System.out.println(a.subtract(b)); | ||
System.out.println(a.multiply(b)); | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/java/com/gapple/weeingback/domain/okay/service/OkayService.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,5 @@ | ||
package com.gapple.weeingback.domain.okay.service; | ||
|
||
public class OkayService { | ||
|
||
} |
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
50 changes: 4 additions & 46 deletions
50
src/main/java/com/gapple/weeingback/domain/user/service/UserService.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 |
---|---|---|
@@ -1,51 +1,9 @@ | ||
package com.gapple.weeingback.domain.user.service; | ||
|
||
import com.gapple.weeingback.domain.check.entity.Check; | ||
import com.gapple.weeingback.domain.user.entity.User; | ||
|
||
import com.gapple.weeingback.domain.user.entity.dto.UserJoinRequest; | ||
import com.gapple.weeingback.domain.user.entity.dto.UserLoginRequest; | ||
import com.gapple.weeingback.domain.user.repository.UserRepository; | ||
import com.gapple.weeingback.global.jwt.JwtProvider; | ||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
@RequiredArgsConstructor | ||
public class UserService { | ||
private final UserRepository userRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
private JwtProvider jwtProvider = new JwtProvider(); | ||
|
||
public void join(UserJoinRequest request) throws Exception{ | ||
User user = new User(); | ||
user.setName(request.getName()); | ||
user.setEmail(request.getEmail()); | ||
user.setPassword(request.getPassword()); | ||
user.setCheck(new Check()); | ||
|
||
if(!userRepository.existsUserByEmail(request.getEmail())) { | ||
user.setPassword(passwordEncoder.encode(user.getPassword())); | ||
userRepository.save(user); | ||
} | ||
else throw new Exception(); | ||
} | ||
|
||
public String login(UserLoginRequest request){ | ||
Authentication authentication = new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword()); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
return authentication.getName(); | ||
} | ||
|
||
public String token(UserLoginRequest request){ | ||
User user = userRepository.findUserByEmail(request.getEmail()); | ||
if(passwordEncoder.matches(request.getPassword(), user.getPassword())){ | ||
// jwtProvider.generateToken(); | ||
} | ||
return null; | ||
} | ||
} | ||
public interface UserService { | ||
void join(UserJoinRequest request) throws Exception; | ||
String login(UserLoginRequest request); | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/gapple/weeingback/domain/user/service/UserServiceImpl.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,49 @@ | ||
package com.gapple.weeingback.domain.user.service; | ||
|
||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
import com.gapple.weeingback.domain.okay.entity.Okay; | ||
import com.gapple.weeingback.domain.user.entity.User; | ||
import com.gapple.weeingback.domain.user.entity.dto.UserJoinRequest; | ||
import com.gapple.weeingback.domain.user.entity.dto.UserLoginRequest; | ||
import com.gapple.weeingback.domain.user.repository.UserRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class UserServiceImpl implements UserService{ | ||
private final UserRepository userRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
// private JwtProvider jwtProvider = new JwtProvider(); | ||
|
||
public void join(UserJoinRequest request) throws Exception{ | ||
User user = new User(); | ||
user.setName(request.getName()); | ||
user.setEmail(request.getEmail()); | ||
user.setPassword(request.getPassword()); | ||
user.setOkay(new Okay()); | ||
|
||
if(!userRepository.existsUserByEmail(request.getEmail())) { | ||
user.setPassword(passwordEncoder.encode(user.getPassword())); | ||
userRepository.save(user); | ||
} | ||
else throw new Exception(); | ||
} | ||
|
||
public String login(UserLoginRequest request){ | ||
Authentication authentication = new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword()); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
return authentication.getName(); | ||
} | ||
|
||
public String token(UserLoginRequest request){ | ||
User user = userRepository.findUserByEmail(request.getEmail()); | ||
if(passwordEncoder.matches(request.getPassword(), user.getPassword())){ | ||
// jwtProvider.generateToken(); | ||
} | ||
return null; | ||
} | ||
} |
84 changes: 42 additions & 42 deletions
84
src/main/java/com/gapple/weeingback/global/filter/JwtFilter.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 |
---|---|---|
@@ -1,56 +1,56 @@ | ||
package com.gapple.weeingback.global.filter; | ||
// package com.gapple.weeingback.global.filter; | ||
|
||
import com.gapple.weeingback.global.jwt.JwtProvider; | ||
import jakarta.servlet.*; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.util.StringUtils; | ||
// import com.gapple.weeingback.global.jwt.JwtProvider; | ||
// import jakarta.servlet.*; | ||
// import jakarta.servlet.http.HttpServletRequest; | ||
// import lombok.extern.slf4j.Slf4j; | ||
// import org.springframework.security.core.Authentication; | ||
// import org.springframework.security.core.context.SecurityContextHolder; | ||
// import org.springframework.util.StringUtils; | ||
|
||
import java.io.IOException; | ||
// import java.io.IOException; | ||
|
||
@Slf4j | ||
public class JwtFilter extends GenericFilter { | ||
public static final String AUTHORIZATION_HEADER = "Authorization"; | ||
// @Slf4j | ||
// public class JwtFilter extends GenericFilter { | ||
// public static final String AUTHORIZATION_HEADER = "Authorization"; | ||
|
||
private final JwtProvider provider; | ||
// private final JwtProvider provider; | ||
|
||
public JwtFilter(JwtProvider provider){ | ||
this.provider = provider; | ||
} | ||
// public JwtFilter(JwtProvider provider){ | ||
// this.provider = provider; | ||
// } | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
// @Override | ||
// public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
|
||
System.out.println("doFilter"); | ||
// System.out.println("doFilter"); | ||
|
||
HttpServletRequest httpServletRequest = (HttpServletRequest) request; | ||
String jwt = resolveToken(httpServletRequest); | ||
String requestURI = httpServletRequest.getRequestURI(); | ||
// HttpServletRequest httpServletRequest = (HttpServletRequest) request; | ||
// String jwt = resolveToken(httpServletRequest); | ||
// String requestURI = httpServletRequest.getRequestURI(); | ||
|
||
// 토큰 유효성 검증 후 정상이면 SecurityContext에 저장 | ||
if(StringUtils.hasText(jwt) && provider.validateToken(jwt)){ // TODO 토큰 검증 만들기 | ||
Authentication authentication = provider.getAuthentication(jwt); // TODO 토큰 인증 주기 | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
log.debug("Security Context에 '{}' 인증 정보를 저장했습니다, uri: {}",authentication.getName(),requestURI); | ||
} | ||
// // 토큰 유효성 검증 후 정상이면 SecurityContext에 저장 | ||
// if(StringUtils.hasText(jwt) && provider.validateToken(jwt)){ // TODO 토큰 검증 만들기 | ||
// Authentication authentication = provider.getAuthentication(jwt); // TODO 토큰 인증 주기 | ||
// SecurityContextHolder.getContext().setAuthentication(authentication); | ||
// log.debug("Security Context에 '{}' 인증 정보를 저장했습니다, uri: {}",authentication.getName(),requestURI); | ||
// } | ||
|
||
else log.debug("유효한 JWT 토큰이 없습니다, uri: {}",requestURI); | ||
// else log.debug("유효한 JWT 토큰이 없습니다, uri: {}",requestURI); | ||
|
||
// 생성한 필터 실행 | ||
chain.doFilter(httpServletRequest,response); | ||
} | ||
// // 생성한 필터 실행 | ||
// chain.doFilter(httpServletRequest,response); | ||
// } | ||
|
||
// Request Header에서 토큰 정보를 꺼내오기 | ||
private String resolveToken(HttpServletRequest request){ | ||
String bearerToken = request.getHeader(AUTHORIZATION_HEADER); | ||
if(StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")){ | ||
// // Request Header에서 토큰 정보를 꺼내오기 | ||
// private String resolveToken(HttpServletRequest request){ | ||
// String bearerToken = request.getHeader(AUTHORIZATION_HEADER); | ||
// if(StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")){ | ||
|
||
System.out.println("token : " + bearerToken); | ||
// System.out.println("token : " + bearerToken); | ||
|
||
return bearerToken.substring(7); | ||
} | ||
return null; | ||
} | ||
} | ||
// return bearerToken.substring(7); | ||
// } | ||
// return null; | ||
// } | ||
// } |
50 changes: 25 additions & 25 deletions
50
src/main/java/com/gapple/weeingback/global/jwt/JwtProvider.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 |
---|---|---|
@@ -1,33 +1,33 @@ | ||
package com.gapple.weeingback.global.jwt; | ||
// package com.gapple.weeingback.global.jwt; | ||
|
||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import org.springframework.security.core.Authentication; | ||
// import io.jsonwebtoken.Jwts; | ||
// import io.jsonwebtoken.SignatureAlgorithm; | ||
// import org.springframework.security.core.Authentication; | ||
|
||
import java.security.Key; | ||
import java.util.Date; | ||
// import java.security.Key; | ||
// import java.util.Date; | ||
|
||
public class JwtProvider { | ||
private String secret = "1234lbvyv1giuyr1fvbh3uy1jrgbv"; | ||
private Key key; | ||
// public class JwtProvider { | ||
// private String secret = "1234lbvyv1giuyr1fvbh3uy1jrgbv"; | ||
// private Key key; | ||
|
||
public JwtProvider(){ | ||
// public JwtProvider(){ | ||
|
||
|
||
// } | ||
|
||
} | ||
// public String generateToken(String email, String type, Long expired){ | ||
// String token = Jwts.builder() | ||
// .setSubject(type) | ||
// .setAudience(email) | ||
// .setExpiration(new Date(System.currentTimeMillis() + 1000L)) | ||
// .setIssuedAt(new Date(System.currentTimeMillis())) | ||
// // .signWith(new Key, SignatureAlgorithm.HS256) | ||
// .compact(); | ||
// return token; | ||
// } | ||
|
||
public String generateToken(String email, String type, Long expired){ | ||
String token = Jwts.builder() | ||
.setSubject(type) | ||
.setAudience(email) | ||
.setExpiration(new Date(System.currentTimeMillis() + 1000L)) | ||
.setIssuedAt(new Date(System.currentTimeMillis())) | ||
// .signWith(new Key, SignatureAlgorithm.HS256) | ||
.compact(); | ||
return token; | ||
} | ||
// public Authentication getAuthentication(String token){ | ||
|
||
public Authentication getAuthentication(String token){ | ||
|
||
} | ||
} | ||
// } | ||
// } |
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.
Oops, something went wrong.