Skip to content

Commit 00808bd

Browse files
committed
Token API Secure Access implementatoin with JWT
1 parent 73b9838 commit 00808bd

33 files changed

+1209
-10
lines changed

.DS_Store

0 Bytes
Binary file not shown.

backend/.DS_Store

0 Bytes
Binary file not shown.

backend/pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@
5656
</dependency>
5757

5858
<dependency>
59-
<groupId>org.json</groupId>
60-
<artifactId>json</artifactId>
61-
<version>20080701</version>
62-
</dependency>
59+
<groupId>org.json</groupId>
60+
<artifactId>json</artifactId>
61+
<version>20080701</version>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>io.jsonwebtoken</groupId>
66+
<artifactId>jjwt</artifactId>
67+
<version>0.7.0</version>
68+
</dependency>
6369
</dependencies>
6470

6571
<build>

backend/src/.DS_Store

0 Bytes
Binary file not shown.

backend/src/main/.DS_Store

0 Bytes
Binary file not shown.

backend/src/main/java/.DS_Store

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.youdemy.controller;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.URI;
6+
import java.nio.file.Files;
7+
import java.sql.SQLException;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.Collection;
11+
import java.util.List;
12+
import java.util.Objects;
13+
import java.util.Optional;
14+
15+
import org.hibernate.engine.jdbc.BlobProxy;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.http.HttpStatus;
18+
import org.springframework.http.ResponseEntity;
19+
import org.springframework.ui.Model;
20+
import org.springframework.util.ResourceUtils;
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.PathVariable;
23+
import org.springframework.web.bind.annotation.PostMapping;
24+
import org.springframework.web.bind.annotation.PutMapping;
25+
import org.springframework.web.bind.annotation.RequestBody;
26+
import org.springframework.web.bind.annotation.RequestMapping;
27+
import org.springframework.web.bind.annotation.RequestParam;
28+
import org.springframework.web.bind.annotation.ResponseStatus;
29+
import org.springframework.web.bind.annotation.RestController;
30+
import org.springframework.web.multipart.MultipartFile;
31+
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
32+
33+
import com.fasterxml.jackson.databind.ObjectMapper;
34+
import com.youdemy.model.Course;
35+
import com.youdemy.model.Lesson;
36+
import com.youdemy.model.User;
37+
import com.youdemy.service.CourseService;
38+
import com.youdemy.service.UserService;
39+
40+
41+
@RestController
42+
@RequestMapping("/api/courses")
43+
public class CourseRestController {
44+
45+
@Autowired
46+
private CourseService courseService;
47+
48+
@Autowired
49+
UserService userService;
50+
51+
@GetMapping("/")
52+
public Collection<Course> getCourses() {
53+
return courseService.findAll();
54+
}
55+
56+
@GetMapping("/{id}")
57+
public ResponseEntity<Course> getCourse(@PathVariable long id){
58+
Optional<Course> op = courseService.findById(id);
59+
if (op.isPresent()) {
60+
Course course = op.get();
61+
return new ResponseEntity<>(course, HttpStatus.OK);
62+
} else {
63+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
64+
}
65+
66+
}
67+
68+
// @PostMapping("/")
69+
// public ResponseEntity<Course> createcourse(@RequestBody Course course) {
70+
//
71+
// courseService.save(course);
72+
//
73+
// URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(course.getId()).toUri();
74+
//
75+
// return ResponseEntity.created(location).body(course);
76+
// }
77+
78+
@PostMapping("/")
79+
public String postNewCourse(@RequestParam("title") String title, @RequestParam("thumbnail") String image,
80+
@RequestParam("description") String description, @RequestParam("price") int price,
81+
@RequestParam("tags") List<String> tags, @RequestBody("lessons") List<Lesson> lessons,
82+
Model model) throws IOException {
83+
84+
Course course = new Course();
85+
User author = userService.findByFirstName(Objects.requireNonNull(model.getAttribute("userName")).toString());
86+
87+
List<Lesson> lessonList = new ArrayList<>(Arrays.asList(new ObjectMapper().readValues(lessons, Lesson[].class)));
88+
lessonList.forEach(lesson -> {
89+
lesson.setAuthor(author);
90+
lesson.setCourse(course);
91+
});
92+
93+
course.setAuthor(author);
94+
course.setThumbnail(loadRandomImage());
95+
course.setTitle(title);
96+
course.setDescription(description);
97+
course.setPrice(price);
98+
course.setTags(tags);
99+
course.setLessons(lessonList);
100+
101+
courseService.save(course);
102+
return "redirect:/courses";
103+
}
104+
105+
public byte[] loadRandomImage() throws IOException {
106+
int randomImgNum = (int) Math.floor(Math.random() * 9) + 1;
107+
File image = ResourceUtils.getFile("classpath:./fakeImages/" + randomImgNum + ".jpg");
108+
109+
return Files.readAllBytes(image.toPath());
110+
}
111+
112+
113+
114+
115+
116+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.youdemy.controller.auth;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
5+
import javax.servlet.http.HttpServletResponse;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.CookieValue;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
import com.youdemy.security.jwt.AuthResponse;
16+
import com.youdemy.security.jwt.AuthResponse.Status;
17+
import com.youdemy.security.jwt.LoginRequest;
18+
import com.youdemy.security.jwt.UserLoginService;
19+
20+
21+
@RestController
22+
@RequestMapping("/api/auth")
23+
public class LoginController {
24+
25+
@Autowired
26+
private UserLoginService userService;
27+
28+
@PostMapping("/login")
29+
public ResponseEntity<AuthResponse> login(
30+
@CookieValue(name = "accessToken", required = false) String accessToken,
31+
@CookieValue(name = "refreshToken", required = false) String refreshToken,
32+
@RequestBody LoginRequest loginRequest) {
33+
34+
return userService.login(loginRequest, accessToken, refreshToken);
35+
}
36+
37+
@PostMapping("/refresh")
38+
public ResponseEntity<AuthResponse> refreshToken(
39+
@CookieValue(name = "refreshToken", required = false) String refreshToken) {
40+
41+
return userService.refresh(refreshToken);
42+
}
43+
44+
@PostMapping("/logout")
45+
public ResponseEntity<AuthResponse> logOut(HttpServletRequest request, HttpServletResponse response) {
46+
47+
return ResponseEntity.ok(new AuthResponse(Status.SUCCESS, userService.logout(request, response)));
48+
}
49+
}
50+

0 commit comments

Comments
 (0)