Access denied
+You do not have permission to view this page.
+ +diff --git a/src/main/java/com/ericbouchut/learndev/common/config/SecurityConfig.java b/src/main/java/com/ericbouchut/learndev/common/config/SecurityConfig.java index 46efbe9..4320970 100644 --- a/src/main/java/com/ericbouchut/learndev/common/config/SecurityConfig.java +++ b/src/main/java/com/ericbouchut/learndev/common/config/SecurityConfig.java @@ -1,7 +1,9 @@ package com.ericbouchut.learndev.common.config; +import jakarta.servlet.DispatcherType; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @@ -10,6 +12,8 @@ @Configuration @EnableWebSecurity +// Enables @PreAuthorize for service-level rules (e.g. course ownership). +@EnableMethodSecurity public class SecurityConfig { @Bean @@ -21,7 +25,13 @@ public PasswordEncoder passwordEncoder() { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(auth -> auth + // The ERROR dispatch renders templates/error/*.html for a + // response already authorized (or denied) on its way in. + .dispatcherTypeMatchers(DispatcherType.ERROR).permitAll() .requestMatchers("/", "/privacy", "/auth/**", "/css/**", "/js/**", "/fonts/**").permitAll() + .requestMatchers("/instructor/**").hasRole("INSTRUCTOR") + .requestMatchers("/admin/**").hasRole("ADMIN") + .requestMatchers("/courses/**").authenticated() .anyRequest().authenticated()) .formLogin(form -> form .loginPage("/auth/login") // GET: show the login form diff --git a/src/main/resources/templates/error/403.html b/src/main/resources/templates/error/403.html new file mode 100644 index 0000000..c3f70ba --- /dev/null +++ b/src/main/resources/templates/error/403.html @@ -0,0 +1,17 @@ + + +
+ +You do not have permission to view this page.
+ +The page you are looking for does not exist or has moved.
+ +An unexpected error occurred on our side. Please try again in a moment.
+ +