Enhance tutorial management with user-specific features and improve error handling

This commit is contained in:
liosha84
2025-07-08 20:36:37 +03:00
parent ec1581e88a
commit b37f62e59b
13 changed files with 259 additions and 29 deletions
@@ -24,7 +24,7 @@ import java.util.Optional;
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600, allowCredentials="true")
//@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600, allowCredentials="true")
@RestController
@RequestMapping("/api")
public class TutorialController {
@@ -117,7 +117,7 @@ public class TutorialController {
}
}
@PostMapping("/tutorials")
@PostMapping("user/tutorial-add")
public ResponseEntity<Tutorial> createTutorial(@RequestBody Tutorial tutorial) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
@@ -16,6 +16,9 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -134,7 +137,9 @@ public class WebSecurityConfig implements WebMvcConfigurer {// extends WebSecuri
// Allow public access to tutorials (without login)
.requestMatchers("/api/public/tutorials").permitAll()
.requestMatchers("/api/user").permitAll()
.requestMatchers("/api/public/tutorials/**").permitAll()
.requestMatchers("/api/user/**").permitAll()
.anyRequest().authenticated()
);
@@ -145,4 +150,16 @@ public class WebSecurityConfig implements WebMvcConfigurer {// extends WebSecuri
return http.build();
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("http://localhost:4200");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
@@ -25,6 +25,7 @@ public class AuthEntryPointJwt implements AuthenticationEntryPoint {
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
logger.error("Unauthorized error: {}", authException.getMessage());
logger.error("Unauthorized error to resource {}", request.getRequestURI());
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Error: Unauthorized");
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
+24 -7
View File
@@ -1,9 +1,33 @@
spring.application.name=jambotron
#============Localhost Configurations========================
spring.datasource.url= jdbc:postgresql://localhost:5432/jambotronDB
spring.datasource.username= admin
spring.datasource.password= postgrespw
#
spring.flyway.baseline-on-migrate=true
spring.flyway.validate-on-migrate=true
spring.flyway.url=jdbc:postgresql://localhost:5432/jambotronDB
spring.flyway.user=admin
spring.flyway.password=postgrespw
#============Koyeb Configurations========================
#spring.datasource.url= jdbc:postgresql://ep-red-breeze-a2cxhq5f.eu-central-1.pg.koyeb.app/jambotronDB
#spring.datasource.username= koyeb-adm
#spring.datasource.password= npg_HfFEUA7bay1i
#
#spring.flyway.baseline-on-migrate=true
#spring.flyway.validate-on-migrate=true
#
#spring.flyway.url=jdbc:postgresql://ep-red-breeze-a2cxhq5f.eu-central-1.pg.koyeb.app/jambotronDB
#spring.flyway.user=koyeb-adm
#spring.flyway.password=npg_HfFEUA7bay1i
#spring.datasource.url=${SPRING_DATASOURCE_URL}
#spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
#spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
@@ -38,13 +62,6 @@ spring.mvc.throw-exception-if-no-handler-found=true
spring.docker.compose.file=../Docker/docker-compose.yml
spring.flyway.baseline-on-migrate=true
spring.flyway.validate-on-migrate=true
spring.flyway.user=admin
spring.flyway.password=postgrespw
spring.flyway.url=jdbc:postgresql://localhost:5432/jambotronDB
@@ -0,0 +1,27 @@
DROP TABLE IF EXISTS public.tutorials;
CREATE TABLE IF NOT EXISTS public.tutorials
(
description character varying(255) COLLATE pg_catalog."default",
published boolean,
title character varying(255) COLLATE pg_catalog."default",
id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
userid bigint,
CONSTRAINT "tutorial_user_FK" FOREIGN KEY (userid)
REFERENCES public.users (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID
)
TABLESPACE pg_default;
-- Index: fki_tutorial_user_FK
-- DROP INDEX IF EXISTS public."fki_tutorial_user_FK";
CREATE INDEX IF NOT EXISTS "fki_tutorial_user_FK"
ON public.tutorials USING btree
(userid ASC NULLS LAST)
TABLESPACE pg_default;