Skip to content

Commit

Permalink
update impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Donghak Kim authored and Donghak Kim committed Oct 21, 2023
1 parent 7202858 commit 6a4b09d
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 165 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {
testImplementation("org.springframework.security:spring-security-test")
annotationProcessor("org.projectlombok:lombok:1.18.20")
testAnnotationProcessor("org.projectlombok:lombok:1.18.20")
runtimeOnly("org.postgresql:postgresql")
runtimeOnly("com.mysql:mysql-connector-j")
testCompileOnly("org.projectlombok:lombok:1.18.20")
compileOnly("org.springframework.boot:spring-boot-starter-security")
compileOnly("org.projectlombok:lombok:1.18.20")
Expand Down
25 changes: 0 additions & 25 deletions src/main/java/com/gapple/weeingback/WeeingBackApplication.java
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));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gapple.weeingback.domain.check.entity;
package com.gapple.weeingback.domain.okay.entity;

import com.gapple.weeingback.domain.user.entity.User;
import jakarta.persistence.*;
Expand All @@ -11,20 +11,20 @@
@Table
@Entity
@Getter @Setter
public class Check {
public class Okay {
@Id @GeneratedValue
private Long checkId;
private Long id;
private Long issuedAt; // 상담 신청 일자
private Long startAt; // 언제 가야하는지
private boolean isAccess = false; // 선생님이 승인했는지
private boolean isTrue = false; // 활성화된 Check 인지

@OneToOne(mappedBy = "check")
@OneToOne(mappedBy = "okay")
private User user;

public Check(boolean isTrue){
public Okay(boolean isTrue){
this.isTrue = isTrue;
}

public Check(){}
public Okay(){}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.gapple.weeingback.domain.okay.service;

public class OkayService {

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gapple.weeingback.domain.user.entity;

import com.gapple.weeingback.domain.check.entity.Check;
import com.gapple.weeingback.domain.okay.entity.Okay;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -18,6 +19,6 @@ public class User {

@OneToOne
@JoinColumn(name = "check_id")
private Check check;
private Okay okay;
// private UserRole role;
}
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);
}
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 src/main/java/com/gapple/weeingback/global/filter/JwtFilter.java
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 src/main/java/com/gapple/weeingback/global/jwt/JwtProvider.java
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){

}
}
// }
// }
7 changes: 4 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
spring.datasource.url=jdbc:postgresql://0.0.0.0:5432/postgres
spring.datasource.username=postgres
spring.datasource.url=jdbc:mysql://0.0.0.0:3306/mysql
spring.datasource.username=root
spring.datasource.password=1234
spring.jpa.open-in-view=false
spring.jpa.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
Expand Down
10 changes: 0 additions & 10 deletions src/main/resources/templates/index.html

This file was deleted.

Loading

0 comments on commit 6a4b09d

Please sign in to comment.