-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Spring MVC] 한성재 미션 제출합니다. #358
base: bingle625
Are you sure you want to change the base?
Changes from all commits
d5890c7
92c31db
f60ca1f
89c9355
bc14e15
aa53458
e9f1555
dfc3f82
156e220
8963ac0
df35ea5
13e16ce
aa59507
2e8d057
09f939f
4aec219
d7133d8
d41fef9
8b7ffcd
251ca70
e311d68
2662b29
cfc3c26
610e556
2318082
2c735cc
32c827a
d08d455
a7af10e
14bc007
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package Hello; | ||
|
||
public class Hello { | ||
String name; | ||
Printer printer; | ||
|
||
public Hello() { | ||
} | ||
|
||
public Hello(String name, Printer printer) { | ||
this.name = name; | ||
this.printer = printer; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setPrinter(Printer printer) { | ||
this.printer = printer; | ||
} | ||
|
||
public String sayHello() { | ||
return "Hello " + this.name; | ||
} | ||
|
||
public void print() { | ||
this.printer.print(sayHello()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package Hello; | ||
|
||
public class Printer { | ||
|
||
public void print(String data) { | ||
System.out.println(data); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package Hello; | ||
|
||
public class StringPrinter extends Printer{ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package jdbctest; | ||
|
||
public class Customer { | ||
private long id; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자바엔 long과 Long이 있습니다. 둘은 무엇이 다르고 각각 어떤 장단점이 있나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrapper 클래스의 차이는 null값을 가질 수 있냐, 없냐 로 알고 있는데, 좀더 자세히 알아보고 정리해보겠습니다. long 과 Long 타입의 차이1. long은 원시(Primitive) 타입, Long 타입은 참조(Reference) 타입
추가 조사: heap과 스택자바 프로그램 실행시, JVM(자바가상머신)은 하드웨어로부터 전달받은 메모리를 특정 부분들로 나눈다. 메모리 공간(Runtime Data Area)
자바 변수의 종류
각 변수의 생성 시기 Method(static) 영역
Stack 영역
스택 프레임(stack frame) Heap 영역
Heap과 Stack의 차이점
출처: |
||
private String firstName; | ||
private String lastName; | ||
|
||
public Customer(long id, String firstName, String lastName) { | ||
this.id = id; | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
||
public String getLastName() { | ||
return lastName; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package jdbctest; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
public class CustomerController { | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
public CustomerController(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
@PostMapping("/customers") | ||
public ResponseEntity<Void> save(@RequestBody Customer customer) { | ||
String sql = "INSERT INTO customers(first_name, last_name) VALUES (?,?)"; | ||
jdbcTemplate.update(sql, customer.getFirstName(), customer.getLastName()); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@GetMapping("/customers") | ||
public ResponseEntity<List<Customer>> list() { | ||
String sql = "select id, first_name, last_name from customers"; | ||
List<Customer> customers = jdbcTemplate.query( | ||
sql, (resultSet, rowNum) -> { | ||
Customer customer = new Customer( | ||
resultSet.getLong("id"), | ||
resultSet.getString("first_name"), | ||
resultSet.getString("last_name") | ||
); | ||
return customer; | ||
}); | ||
return ResponseEntity.ok().body(customers); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package jdbctest; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
@SpringBootApplication | ||
public class DemoApplication implements CommandLineRunner { | ||
|
||
public static void main(String args[]) { | ||
SpringApplication.run(DemoApplication.class, args); | ||
} | ||
|
||
@Autowired | ||
JdbcTemplate jdbcTemplate; | ||
|
||
@Override | ||
public void run(String... strings) throws Exception { | ||
|
||
jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); | ||
jdbcTemplate.execute("CREATE TABLE customers(id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))"); | ||
|
||
List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream() | ||
.map(name -> name.split(" ")) | ||
.collect(Collectors.toList()); | ||
|
||
jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package roomescape.Domains.Auth; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class LoginController { | ||
|
||
final private String secretKey = "Yn2kjibddFAWtnPJ2AFlL8WXmohJMCvigQggaEypa5E="; | ||
private final UserRepository userRepository; | ||
|
||
public LoginController(final UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@PostMapping("/login") | ||
public final ResponseEntity<String> login(@RequestBody Map<String, String> credentials) { | ||
String password = credentials.get("password"); | ||
String email = credentials.get("email"); | ||
String name = "어드민"; | ||
|
||
this.userRepository.save(new User(name, email, password)); | ||
|
||
Claims claims = Jwts.claims().setSubject(name); | ||
|
||
Date now = new Date(); | ||
Date expiryDate = new Date(now.getTime() + 1000L * 60 * 60); // 토큰 만료 시간: 1시간 후 | ||
|
||
String accessToken = Jwts.builder().setClaims(claims).setIssuedAt(now).setExpiration(expiryDate) | ||
.signWith(SignatureAlgorithm.HS256, secretKey).compact(); | ||
|
||
return ResponseEntity.ok().header("Set-Cookie", "token=" + accessToken + ";").build(); | ||
} | ||
|
||
@GetMapping("/login/check") | ||
public final ResponseEntity<Map<String, String>> loginCheck(HttpServletRequest request) { | ||
Cookie[] cookies = request.getCookies(); | ||
String token = getToken(cookies); | ||
String memberName = String.valueOf(Jwts.parser() | ||
.setSigningKey(secretKey) | ||
.parseClaimsJws(token) | ||
.getBody().getSubject()); | ||
return ResponseEntity.ok().body(Map.of("name", memberName)); | ||
} | ||
|
||
private static String getToken(final Cookie[] cookies) { | ||
for (Cookie cookie : cookies) { | ||
if (cookie.getName().equals("token")) { | ||
return cookie.getValue(); | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package roomescape.Domains.Auth; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import roomescape.Domains.Time.Time; | ||
|
||
@Entity | ||
@Table(name = "customer") | ||
public class User { | ||
|
||
protected User() { | ||
} | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
|
||
private String name; | ||
private String email; | ||
private String password; | ||
|
||
public User(final String name,final String email, final String password) { | ||
this.name = name; | ||
this.email = email; | ||
this.password = password; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package roomescape.Domains.Auth; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package roomescape.Domains.Reservation; | ||
|
||
import jakarta.persistence.Embedded; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToOne; | ||
import roomescape.Domains.Time.Time; | ||
|
||
@Entity | ||
public class Reservation { | ||
|
||
protected Reservation() { | ||
} | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
|
||
private String name; | ||
private String date; | ||
@ManyToOne | ||
@JoinColumn(name = "time_id") | ||
private Time time; | ||
|
||
public Reservation(final String name, final String date, final Time time) { | ||
this.name = name; | ||
this.date = date; | ||
this.time = time; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public Time getTime() { | ||
return time; | ||
} | ||
|
||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
|
||
public void setId(final Long id) { | ||
this.id = id; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
타임리프를 추가하셨군요! 이번 미션에선 사용 안한 것 같은데, 어떤 이유로 쓰셨나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
테스트하다가 지우지 않은 흔적으로 보입니다..;