Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/main/java/org/example/alfs/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,35 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers("/auth/logout").permitAll()
.requestMatchers("/auth/hash").permitAll()
.requestMatchers("/h2-console/**").permitAll()
.requestMatchers("/startPage", "/").permitAll()
.requestMatchers("/tickets/create").permitAll()
.requestMatchers("/", "/startPage").permitAll()
.requestMatchers(HttpMethod.GET,"/tickets/create").permitAll()
.requestMatchers("/error/**").permitAll()

//allow access to endpoints during development
.requestMatchers("/tickets/previewTicket").permitAll()
.requestMatchers(HttpMethod.POST, "/api/files/upload").permitAll()
//allow access to endpoints during development
.requestMatchers("/tickets/**").permitAll()
.requestMatchers("/css/**", "/js/**", "/images/**", "/static/**").permitAll()
.requestMatchers("/login", "/login-form").permitAll()
.requestMatchers("/signup", "/signup-form").permitAll()
.anyRequest().authenticated()
)

.exceptionHandling(exception -> exception

// Use custom error page for 403 (Spring Security access denied)
.accessDeniedHandler((request, response, ex) -> {
request.getRequestDispatcher("/error/403")
.forward(request, response);
})

// Use custom error page for 401 (unauthorized)
.authenticationEntryPoint((request, response, ex) -> {
request.getRequestDispatcher("/error/401")
.forward(request, response);
})
)


// disable DEFAULT LOGIN
.formLogin(form -> form.disable())
.httpBasic(basic -> basic.disable())
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/example/alfs/controllers/ErrorController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.example.alfs.controllers;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
@RequestMapping("/error")
public class ErrorController {

@RequestMapping("/403")
@ResponseStatus(HttpStatus.FORBIDDEN)
public String forbidden(Model model) {
model.addAttribute("status", 403);
model.addAttribute("error", "Access denied");
return "error";
}

@RequestMapping("/401")
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public String unauthorized(Model model) {
model.addAttribute("status", 401);
model.addAttribute("error", "You need to log in to access this page");
return "error";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.example.alfs.exceptions;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.server.ResponseStatusException;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(ResponseStatusException.class)
public String handleResponseStatusException(
ResponseStatusException ex,
Model model,
HttpServletResponse response
) {

int status = ex.getStatusCode().value();
response.setStatus(status);

model.addAttribute("status", status);
model.addAttribute("error", ex.getReason());

return "error";
}

@ExceptionHandler(Exception.class)
public String handleException(
Exception ex,
Model model,
HttpServletResponse response
) {

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

model.addAttribute("status", 500);
model.addAttribute("error", "Something went wrong");

return "error";
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}
26 changes: 26 additions & 0 deletions src/main/jte/error.jte
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@param String error
@param Integer status
@param String success


@template.layout(title = "Error", content = @`
<div class="max-w-xl mx-auto mt-16 text-center">

<h1 class="text-3xl font-serif text-[#1a2b49] mb-6">
Something went wrong
</h1>

<p class="text-lg mb-4">
Status: ${status}
</p>

<p class="text-gray-700 mb-6">
${error}
</p>

<a href="/startPage" class="btn-primary px-6 py-3 rounded">
Back to start page
</a>

</div>
` , success = null)
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
gg.jte.development-mode=true
spring.application.name=alfs

#Disable whitelabel
spring.web.error.whitelabel.enabled=false
Comment thread
coderabbitai[bot] marked this conversation as resolved.

# Storage (MinIO/S3) ? default local dev values; override via env in andra milj�er
storage.s3.endpoint=http://localhost:9000
storage.s3.accessKey=minio
Expand Down
Loading