From e7737c5f9df7836838f8a52889d44beaf09743dc Mon Sep 17 00:00:00 2001 From: Artur Kmieckowiak Date: Fri, 14 Feb 2020 00:27:00 +0100 Subject: [PATCH] CORS changes to work with frontend application. Modified getThread method --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 5 +- .../amu/wmi/bookapi/BookapiApplication.java | 4 +- .../amu/wmi/bookapi/api/BookController.java | 11 ++- .../amu/wmi/bookapi/api/CustomCorsFilter.java | 41 +++++++++++ .../wmi/bookapi/api/MessageController.java | 7 +- .../amu/wmi/bookapi/api/UserController.java | 21 +++--- .../wmi/bookapi/config/DateTimeProvider.java | 2 - .../amu/wmi/bookapi/models/UserDocument.java | 4 +- .../bookapi/repositories/BookRepository.java | 1 - .../repositories/ThreadRepository.java | 7 +- .../repositories/ThreadRepositoryCustom.java | 1 + .../ThreadRepositoryCustomImpl.java | 10 +++ .../bookapi/repositories/UserRepository.java | 4 +- .../security/JWTAuthenticationFilter.java | 35 ++++++---- .../security/JWTAuthorizationFilter.java | 25 ++++--- .../UserDetailsSecurityServiceImpl.java | 13 ++-- .../amu/wmi/bookapi/security/WebSecurity.java | 29 +++++--- .../service/ImageProcessingService.java | 69 ++++++++----------- .../wmi/bookapi/BookapiApplicationTests.java | 7 +- .../amu/wmi/bookapi/Integration/BaseInt.java | 13 ++-- .../Integration/api/BookControllerInt.java | 8 +-- .../Integration/api/MessageControllerInt.java | 5 +- .../Integration/api/UserControllerInt.java | 25 ++++--- .../bookapi/fixtures/IntegrationTestUtil.java | 15 ++-- .../fixtures/api/BookControllerRequest.java | 5 +- .../api/MessageControllerRequests.java | 6 +- .../fixtures/api/UserControllerRequests.java | 2 +- 28 files changed, 224 insertions(+), 153 deletions(-) create mode 100644 src/main/java/pl/edu/amu/wmi/bookapi/api/CustomCorsFilter.java diff --git a/build.gradle b/build.gradle index 13f2215..b17ab70 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'com.auth0:java-jwt:3.4.0' - implementation 'org.openpnp:opencv:3.2.0-0' +// implementation name: 'opencv-420' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation('org.junit.jupiter:junit-jupiter-api') testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine') diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 9492014..29d95ac 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Thu Feb 13 22:25:46 CET 2020 +distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/BookapiApplication.java b/src/main/java/pl/edu/amu/wmi/bookapi/BookapiApplication.java index ceab4cc..6526e80 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/BookapiApplication.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/BookapiApplication.java @@ -2,8 +2,8 @@ package pl.edu.amu.wmi.bookapi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.*; -import org.springframework.security.crypto.bcrypt.*; +import org.springframework.context.annotation.Bean; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @SpringBootApplication public class BookapiApplication { diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/api/BookController.java b/src/main/java/pl/edu/amu/wmi/bookapi/api/BookController.java index 07a4378..257d6dc 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/api/BookController.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/api/BookController.java @@ -1,10 +1,17 @@ package pl.edu.amu.wmi.bookapi.api; -import org.apache.coyote.Response; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import pl.edu.amu.wmi.bookapi.api.dto.BookDto; import pl.edu.amu.wmi.bookapi.service.BookService; diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/api/CustomCorsFilter.java b/src/main/java/pl/edu/amu/wmi/bookapi/api/CustomCorsFilter.java new file mode 100644 index 0000000..90e9535 --- /dev/null +++ b/src/main/java/pl/edu/amu/wmi/bookapi/api/CustomCorsFilter.java @@ -0,0 +1,41 @@ +package pl.edu.amu.wmi.bookapi.api; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +//@Component +//@Order(Ordered.HIGHEST_PRECEDENCE) +public class CustomCorsFilter implements Filter { + + @Override + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { + + HttpServletRequest request = (HttpServletRequest) req; + HttpServletResponse response = (HttpServletResponse) res; + + response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); + response.setHeader("Access-Control-Allow-Credentials", "true"); + response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); + response.setHeader("Access-Control-Max-Age", "3600"); + response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me"); + + System.out.println(response.getHeaderNames()); + chain.doFilter(req, res); + } + + @Override + public void init(FilterConfig filterConfig) { + } + + @Override + public void destroy() { + } + +} diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/api/MessageController.java b/src/main/java/pl/edu/amu/wmi/bookapi/api/MessageController.java index d26c111..d5037a5 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/api/MessageController.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/api/MessageController.java @@ -3,7 +3,12 @@ package pl.edu.amu.wmi.bookapi.api; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import pl.edu.amu.wmi.bookapi.api.dto.MessageDto; import pl.edu.amu.wmi.bookapi.service.MessageService; diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/api/UserController.java b/src/main/java/pl/edu/amu/wmi/bookapi/api/UserController.java index 0a884c3..ba1aba9 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/api/UserController.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/api/UserController.java @@ -1,13 +1,15 @@ package pl.edu.amu.wmi.bookapi.api; import org.springframework.dao.DuplicateKeyException; -import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.crypto.bcrypt.*; -import org.springframework.web.bind.annotation.*; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import pl.edu.amu.wmi.bookapi.exceptions.RegisterException; -import pl.edu.amu.wmi.bookapi.models.*; -import pl.edu.amu.wmi.bookapi.repositories.*; +import pl.edu.amu.wmi.bookapi.models.UserDocument; +import pl.edu.amu.wmi.bookapi.repositories.UserRepository; import javax.security.auth.login.LoginException; @@ -26,17 +28,14 @@ public class UserController { @PostMapping("/sign-up") public void signUp(@RequestBody UserDocument user) throws RegisterException { + System.out.println("Registering user"); + System.out.println(user); user.setPassword(bCryptPasswordEncoder.encode(user.getPassword())); try { userRepository.save(user); - System.out.println("hello"); } catch (DuplicateKeyException e) { throw new RegisterException("Login already in use"); } - } - - @PostMapping("/sign-in") - public void signIn(@RequestBody UserDocument user) throws LoginException { - Object princ = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + System.out.println(userRepository.findAll()); } } diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/config/DateTimeProvider.java b/src/main/java/pl/edu/amu/wmi/bookapi/config/DateTimeProvider.java index d736ac9..6d37fdd 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/config/DateTimeProvider.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/config/DateTimeProvider.java @@ -3,9 +3,7 @@ package pl.edu.amu.wmi.bookapi.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import java.time.Clock; import java.time.Instant; -import java.time.ZoneId; @Configuration public class DateTimeProvider { diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/models/UserDocument.java b/src/main/java/pl/edu/amu/wmi/bookapi/models/UserDocument.java index 675b404..099f577 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/models/UserDocument.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/models/UserDocument.java @@ -1,8 +1,8 @@ package pl.edu.amu.wmi.bookapi.models; -import org.springframework.data.annotation.*; +import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.Indexed; -import org.springframework.data.mongodb.core.mapping.*; +import org.springframework.data.mongodb.core.mapping.Document; @Document public class UserDocument { diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/repositories/BookRepository.java b/src/main/java/pl/edu/amu/wmi/bookapi/repositories/BookRepository.java index 7af072d..c2a9bad 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/repositories/BookRepository.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/repositories/BookRepository.java @@ -1,7 +1,6 @@ package pl.edu.amu.wmi.bookapi.repositories; import org.springframework.data.mongodb.repository.MongoRepository; -import pl.edu.amu.wmi.bookapi.api.dto.BookDto; import pl.edu.amu.wmi.bookapi.models.BookDocument; import java.util.List; diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepository.java b/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepository.java index 9d70f43..1407e87 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepository.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepository.java @@ -1,14 +1,11 @@ package pl.edu.amu.wmi.bookapi.repositories; import org.springframework.data.mongodb.repository.MongoRepository; -import org.springframework.data.mongodb.repository.Query; import pl.edu.amu.wmi.bookapi.models.ThreadDocument; -import java.util.List; - public interface ThreadRepository extends MongoRepository, ThreadRepositoryCustom { // @Query("'participantsIds': { $in: [?0, ?1]}") // List findByParticipants(String participantId1, String participantId2); - @Query("{'participantsIds': {$in: [?0]}}") - List findByParticipant(String participant); +// @Query("{'participantsIds': {$in: [?0]}}") +// List findByParticipant(String participant); } \ No newline at end of file diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepositoryCustom.java b/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepositoryCustom.java index 625e44c..75557c8 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepositoryCustom.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepositoryCustom.java @@ -6,4 +6,5 @@ import java.util.List; public interface ThreadRepositoryCustom { List findByParticipants(String participantId1, String participantId2); + List findByParticipant(String participant); } diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepositoryCustomImpl.java b/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepositoryCustomImpl.java index 903ace3..f5e52a6 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepositoryCustomImpl.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/repositories/ThreadRepositoryCustomImpl.java @@ -28,4 +28,14 @@ public class ThreadRepositoryCustomImpl implements ThreadRepositoryCustom { return mongoTemplate.find(query, ThreadDocument.class); } + + @Override + public List findByParticipant(String participant) { + Query query = new Query(); + query.addCriteria( + Criteria.where("participantsIds").in(List.of(participant)) + ); + + return mongoTemplate.find(query, ThreadDocument.class); + } } diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/repositories/UserRepository.java b/src/main/java/pl/edu/amu/wmi/bookapi/repositories/UserRepository.java index b998283..368b305 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/repositories/UserRepository.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/repositories/UserRepository.java @@ -1,7 +1,7 @@ package pl.edu.amu.wmi.bookapi.repositories; -import org.springframework.data.mongodb.repository.*; -import pl.edu.amu.wmi.bookapi.models.*; +import org.springframework.data.mongodb.repository.MongoRepository; +import pl.edu.amu.wmi.bookapi.models.UserDocument; public interface UserRepository extends MongoRepository { UserDocument findByUsername(String username); diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/security/JWTAuthenticationFilter.java b/src/main/java/pl/edu/amu/wmi/bookapi/security/JWTAuthenticationFilter.java index c6a20df..b2c9f2c 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/security/JWTAuthenticationFilter.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/security/JWTAuthenticationFilter.java @@ -1,19 +1,25 @@ package pl.edu.amu.wmi.bookapi.security; -import com.auth0.jwt.*; -import com.fasterxml.jackson.databind.*; -import org.springframework.security.authentication.*; -import org.springframework.security.core.*; -import org.springframework.security.core.userdetails.*; -import org.springframework.security.web.authentication.*; -import pl.edu.amu.wmi.bookapi.models.*; +import com.auth0.jwt.JWT; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; +import org.springframework.web.cors.CorsUtils; +import pl.edu.amu.wmi.bookapi.models.UserDocument; -import javax.servlet.*; -import javax.servlet.http.*; -import java.io.*; -import java.util.*; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; -import static com.auth0.jwt.algorithms.Algorithm.*; +import static com.auth0.jwt.algorithms.Algorithm.HMAC512; public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter { private AuthenticationManager authenticationManager; @@ -33,6 +39,11 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res) throws AuthenticationException { try { + if (CorsUtils.isPreFlightRequest(req)) { + res.setStatus(HttpServletResponse.SC_OK); + return null; + } + UserDocument creds = new ObjectMapper() .readValue(req.getInputStream(), UserDocument.class); diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/security/JWTAuthorizationFilter.java b/src/main/java/pl/edu/amu/wmi/bookapi/security/JWTAuthorizationFilter.java index 3c40aab..02b9147 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/security/JWTAuthorizationFilter.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/security/JWTAuthorizationFilter.java @@ -1,17 +1,22 @@ package pl.edu.amu.wmi.bookapi.security; -import com.auth0.jwt.*; -import com.auth0.jwt.algorithms.*; -import org.springframework.security.authentication.*; -import org.springframework.security.core.context.*; -import org.springframework.security.web.authentication.www.*; +import com.auth0.jwt.JWT; +import com.auth0.jwt.algorithms.Algorithm; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; -import javax.servlet.*; -import javax.servlet.http.*; -import java.io.*; -import java.util.*; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.ArrayList; -import static pl.edu.amu.wmi.bookapi.security.JWTAuthenticationFilter.*; +import static pl.edu.amu.wmi.bookapi.security.JWTAuthenticationFilter.HEADER_STRING; +import static pl.edu.amu.wmi.bookapi.security.JWTAuthenticationFilter.SECRET; +import static pl.edu.amu.wmi.bookapi.security.JWTAuthenticationFilter.TOKEN_PREFIX; public class JWTAuthorizationFilter extends BasicAuthenticationFilter { diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/security/UserDetailsSecurityServiceImpl.java b/src/main/java/pl/edu/amu/wmi/bookapi/security/UserDetailsSecurityServiceImpl.java index fa19b39..114fe8b 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/security/UserDetailsSecurityServiceImpl.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/security/UserDetailsSecurityServiceImpl.java @@ -1,11 +1,14 @@ package pl.edu.amu.wmi.bookapi.security; -import org.springframework.security.core.userdetails.*; -import org.springframework.stereotype.*; -import pl.edu.amu.wmi.bookapi.models.*; -import pl.edu.amu.wmi.bookapi.repositories.*; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; +import pl.edu.amu.wmi.bookapi.models.UserDocument; +import pl.edu.amu.wmi.bookapi.repositories.UserRepository; -import static java.util.Collections.*; +import static java.util.Collections.emptyList; @Service public class UserDetailsSecurityServiceImpl implements UserDetailsService { diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/security/WebSecurity.java b/src/main/java/pl/edu/amu/wmi/bookapi/security/WebSecurity.java index defc885..13bad36 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/security/WebSecurity.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/security/WebSecurity.java @@ -1,15 +1,19 @@ package pl.edu.amu.wmi.bookapi.security; -import org.springframework.context.annotation.*; -import org.springframework.http.*; -import org.springframework.security.config.annotation.authentication.builders.*; -import org.springframework.security.config.annotation.web.builders.*; -import org.springframework.security.config.annotation.web.configuration.*; -import org.springframework.security.config.http.*; -import org.springframework.security.crypto.bcrypt.*; -import org.springframework.web.cors.*; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpMethod; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.config.http.SessionCreationPolicy; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.CorsConfigurationSource; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; -import static pl.edu.amu.wmi.bookapi.security.JWTAuthenticationFilter.*; +import static pl.edu.amu.wmi.bookapi.security.JWTAuthenticationFilter.LOG_IN_URL; +import static pl.edu.amu.wmi.bookapi.security.JWTAuthenticationFilter.SIGN_UP_URL; @EnableWebSecurity public class WebSecurity extends WebSecurityConfigurerAdapter { @@ -23,13 +27,14 @@ public class WebSecurity extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { - http.cors().and().csrf().disable() + http.csrf().disable() .authorizeRequests() // .antMatchers(HttpMethod.GET, "/api/books", "/api/messages/", "/api/messages/*", "/api/books/public").permitAll() // .antMatchers(HttpMethod.DELETE, "/api/books/*").permitAll() // .antMatchers(HttpMethod.PATCH, "/api/books/*").permitAll() .antMatchers(HttpMethod.POST, - SIGN_UP_URL).permitAll() + SIGN_UP_URL, + LOG_IN_URL).permitAll() .anyRequest().authenticated() .and() .addFilter( @@ -42,8 +47,10 @@ public class WebSecurity extends WebSecurityConfigurerAdapter { } private JWTAuthenticationFilter getConfiguredJwtAuthenticationFilter() throws Exception{ + System.out.println("JWT Auth"); JWTAuthenticationFilter jwtFilter = new JWTAuthenticationFilter(authenticationManager()); jwtFilter.setFilterProcessesUrl("/users/login"); + return jwtFilter; } diff --git a/src/main/java/pl/edu/amu/wmi/bookapi/service/ImageProcessingService.java b/src/main/java/pl/edu/amu/wmi/bookapi/service/ImageProcessingService.java index eaa8a5b..65024b4 100644 --- a/src/main/java/pl/edu/amu/wmi/bookapi/service/ImageProcessingService.java +++ b/src/main/java/pl/edu/amu/wmi/bookapi/service/ImageProcessingService.java @@ -1,24 +1,9 @@ package pl.edu.amu.wmi.bookapi.service; -import org.opencv.core.Core; -import org.opencv.core.Mat; -import org.opencv.core.MatOfByte; -import org.opencv.imgcodecs.Imgcodecs; -import org.opencv.imgproc.Imgproc; + import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.UUID; - -import static java.lang.System.loadLibrary; -import static org.opencv.imgproc.Imgproc.COLOR_BGR2GRAY; - - - @Service public class ImageProcessingService { @@ -28,32 +13,34 @@ public class ImageProcessingService { public String getDecodedEan(MultipartFile imageFile) throws Exception { - String fileId = UUID.randomUUID().toString(); - Path path = Path.of(new File(".").getCanonicalPath()); - - byte[] starting = imageFile.getBytes(); - - saveImg(starting, path, fileId, 1); - - Mat mat = Imgcodecs.imdecode(new MatOfByte(starting), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED); - - Mat gray = new Mat(); - Imgproc.cvtColor(mat, gray, COLOR_BGR2GRAY); - - byte[] grayed = mat2byteArr(gray); - - saveImg(grayed, path, fileId, 2); - return ""; +// +// String fileId = UUID.randomUUID().toString(); +// Path path = Path.of(new File(".").getCanonicalPath()); +// +// byte[] starting = imageFile.getBytes(); +// +// saveImg(starting, path, fileId, 1); +// +// Mat mat = Imgcodecs.imdecode(new MatOfByte(starting), Imgcodecs.IMREAD_GRAYSCALE); +// +// Mat gray = new Mat(); +// Imgproc.cvtColor(mat, gray, COLOR_BGR2GRAY); +// +// byte[] grayed = mat2byteArr(gray); +// +// saveImg(grayed, path, fileId, 2); +// +// return ""; } - private byte[] mat2byteArr(Mat mat) { - byte[] bytes = new byte[0]; - mat.get(0, 0, bytes); - return bytes; - } - - private void saveImg(byte[] bytes, Path path, String imageName, Integer version) throws IOException { - Files.write(Path.of(path.toString(), "/" + imageName + "__v" + version + ".png"), bytes); - } +// private byte[] mat2byteArr(Mat mat) { +// byte[] bytes = new byte[0]; +// mat.get(0, 0, bytes); +// return bytes; +// } +// +// private void saveImg(byte[] bytes, Path path, String imageName, Integer version) throws IOException { +// Files.write(Path.of(path.toString(), "/" + imageName + "__v" + version + ".png"), bytes); +// } } diff --git a/src/test/java/pl/edu/amu/wmi/bookapi/BookapiApplicationTests.java b/src/test/java/pl/edu/amu/wmi/bookapi/BookapiApplicationTests.java index 6810f92..8573a8d 100644 --- a/src/test/java/pl/edu/amu/wmi/bookapi/BookapiApplicationTests.java +++ b/src/test/java/pl/edu/amu/wmi/bookapi/BookapiApplicationTests.java @@ -1,10 +1,10 @@ package pl.edu.amu.wmi.bookapi; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.*; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.data.mongodb.core.*; -import pl.edu.amu.wmi.bookapi.models.*; +import org.springframework.data.mongodb.core.MongoTemplate; +import pl.edu.amu.wmi.bookapi.models.UserDocument; @SpringBootTest class BookapiApplicationTests { @@ -14,7 +14,6 @@ class BookapiApplicationTests { @Test void contextLoads() { - mongoTemplate.save(new UserDocument("test", "test")); System.out.println(mongoTemplate.findAll(UserDocument.class)); } diff --git a/src/test/java/pl/edu/amu/wmi/bookapi/Integration/BaseInt.java b/src/test/java/pl/edu/amu/wmi/bookapi/Integration/BaseInt.java index 16ff089..6ea5ef3 100644 --- a/src/test/java/pl/edu/amu/wmi/bookapi/Integration/BaseInt.java +++ b/src/test/java/pl/edu/amu/wmi/bookapi/Integration/BaseInt.java @@ -1,14 +1,11 @@ package pl.edu.amu.wmi.bookapi.Integration; -import com.mongodb.*; -import org.junit.jupiter.api.*; -import org.springframework.beans.factory.annotation.*; -import org.springframework.boot.test.context.*; -import org.springframework.data.mongodb.core.*; -import org.springframework.data.mongodb.core.query.*; -import pl.edu.amu.wmi.bookapi.models.*; +import org.junit.jupiter.api.BeforeAll; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import pl.edu.amu.wmi.bookapi.models.UserDocument; -import java.util.*; +import java.util.List; public class BaseInt { public MongoTemplate mongoTemplate; diff --git a/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/BookControllerInt.java b/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/BookControllerInt.java index 5147f24..c78e6c8 100644 --- a/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/BookControllerInt.java +++ b/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/BookControllerInt.java @@ -18,10 +18,10 @@ import pl.edu.amu.wmi.bookapi.models.BookDocument; import java.util.List; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc diff --git a/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/MessageControllerInt.java b/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/MessageControllerInt.java index 4807663..ace8a6d 100644 --- a/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/MessageControllerInt.java +++ b/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/MessageControllerInt.java @@ -12,14 +12,13 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.test.web.servlet.MockMvc; import pl.edu.amu.wmi.bookapi.fixtures.IntegrationTestUtil; -import pl.edu.amu.wmi.bookapi.fixtures.api.BookControllerRequest; import pl.edu.amu.wmi.bookapi.fixtures.api.MessageControllerRequests; import pl.edu.amu.wmi.bookapi.fixtures.api.UserControllerRequests; import pl.edu.amu.wmi.bookapi.models.MessageDocument; import pl.edu.amu.wmi.bookapi.models.ThreadDocument; -import static org.junit.Assert.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.junit.Assert.assertEquals; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc diff --git a/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/UserControllerInt.java b/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/UserControllerInt.java index c3b71b2..f479909 100644 --- a/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/UserControllerInt.java +++ b/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/UserControllerInt.java @@ -1,21 +1,20 @@ package pl.edu.amu.wmi.bookapi.Integration.api; -import com.fasterxml.jackson.databind.*; -import org.junit.*; -import org.junit.jupiter.api.*; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.*; -import org.springframework.boot.test.autoconfigure.web.servlet.*; -import org.springframework.boot.test.context.*; -import org.springframework.data.mongodb.core.*; -import org.springframework.test.web.servlet.*; -import pl.edu.amu.wmi.bookapi.fixtures.*; -import pl.edu.amu.wmi.bookapi.fixtures.api.*; +import org.junit.jupiter.api.TestInstance; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.test.web.servlet.MockMvc; +import pl.edu.amu.wmi.bookapi.fixtures.IntegrationTestUtil; +import pl.edu.amu.wmi.bookapi.fixtures.api.UserControllerRequests; import pl.edu.amu.wmi.bookapi.models.UserDocument; -import static org.junit.Assert.*; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.junit.Assert.assertEquals; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc diff --git a/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/IntegrationTestUtil.java b/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/IntegrationTestUtil.java index 3b89874..46529ca 100644 --- a/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/IntegrationTestUtil.java +++ b/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/IntegrationTestUtil.java @@ -1,12 +1,15 @@ package pl.edu.amu.wmi.bookapi.fixtures; -import org.springframework.beans.factory.annotation.*; -import org.springframework.data.mongodb.core.*; -import org.springframework.data.mongodb.core.query.*; -import org.springframework.stereotype.*; -import pl.edu.amu.wmi.bookapi.models.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.stereotype.Component; +import pl.edu.amu.wmi.bookapi.models.BookDocument; +import pl.edu.amu.wmi.bookapi.models.MessageDocument; +import pl.edu.amu.wmi.bookapi.models.ThreadDocument; +import pl.edu.amu.wmi.bookapi.models.UserDocument; -import java.util.*; +import java.util.List; @Component public class IntegrationTestUtil { diff --git a/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/BookControllerRequest.java b/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/BookControllerRequest.java index 447b8c8..7393fc2 100644 --- a/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/BookControllerRequest.java +++ b/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/BookControllerRequest.java @@ -7,7 +7,10 @@ import org.springframework.test.web.servlet.ResultActions; import java.net.URI; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; public class BookControllerRequest { private final MockMvc mvc; diff --git a/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/MessageControllerRequests.java b/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/MessageControllerRequests.java index 58c15cc..8573acb 100644 --- a/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/MessageControllerRequests.java +++ b/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/MessageControllerRequests.java @@ -4,9 +4,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; -import pl.edu.amu.wmi.bookapi.api.dto.MessageDto; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; public class MessageControllerRequests { private final MockMvc mvc; @@ -23,7 +23,7 @@ public class MessageControllerRequests { } public ResultActions getMessages(String bearer, String threadId) throws Exception { - return mvc.perform(get("/api/messages" + threadId) + return mvc.perform(get("/api/messages/" + threadId) .header("Authorization", bearer)); } diff --git a/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/UserControllerRequests.java b/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/UserControllerRequests.java index 72de67a..1a8d52d 100644 --- a/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/UserControllerRequests.java +++ b/src/test/java/pl/edu/amu/wmi/bookapi/fixtures/api/UserControllerRequests.java @@ -7,7 +7,7 @@ import org.springframework.test.web.servlet.ResultActions; import java.net.URI; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;