Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| de:modul:ffit:3-jahr:java:learningunits:lu10:a [2025/11/09 23:21] – apeter | de:modul:ffit:3-jahr:java:learningunits:lu10:a [2025/11/10 00:02] (aktuell) – apeter | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| ====== LU10a - Umgang mit Exceptions ====== | ====== LU10a - Umgang mit Exceptions ====== | ||
| + | |||
| + | ==== Spring ResponseStatusException ==== | ||
| + | |||
| + | Allgemein gilt, dass Exceptions in Bezug auf die Performanz schlechter abschneiden als die entsprechenden Checks. Daher sollte man Exceptions nie in " | ||
| + | |||
| + | Spring bietet eine sehr einfache Version, um Fehlerfälle mittels Exceptions abzuhandeln. Dabei wird direkt automatisch eine Antwort mit dem entsprechenden HTTP-Statuscode erstellt und zurückgeschickt. | ||
| + | |||
| + | Im nachfolgenden Beispiel wird jeweils ein Project-Objekt geladen und falls kein Objekt in der Datenbank vorhanden ist, wird der Statuscode 404 zurückgegeben. | ||
| + | |||
| + | ^ Lange Variante ^ Verkürzte Variante ^ Variante mit Exception ^ | ||
| + | | < | ||
| + | <code java> | ||
| + | Optional< | ||
| + | if(optionalProject.isEmpty()){ | ||
| + | return ResponseEntity.notFound().build(); | ||
| + | } | ||
| + | Project project = optionalProject.get(); | ||
| + | </ | ||
| + | </ | ||
| + | <code java> | ||
| + | Project project = projectRepository.findById(projectName).orElse(null); | ||
| + | if(null == project){ | ||
| + | return ResponseEntity.notFound().build(); | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | <code java> | ||
| + | Project project = projectRepository.findById(projectName).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | Ein sehr grosser Vorteil der dritten Variante ist die Wiederverwendbarkeit: | ||
| ==== Validierung des JWT ==== | ==== Validierung des JWT ==== | ||
| Zeile 22: | Zeile 54: | ||
| </ | </ | ||
| - | Es ist anzumerken, dass Exceptions in aller Regel in Bezug auf die Performanz schlechter abschneiden als die entsprechenden Checks. Daher sollte man Exceptions nie in " | + | Wenn nun zudem Informationen wie das " |
| - | + | ||
| - | Wenn zudem Informationen wie das " | + | |
| <code java> | <code java> | ||
| + | public String verifyTokenAndExtractSubject() { | ||
| + | try{ | ||
| + | String token = extractTokenFromHeader(); | ||
| + | return extractSubject(token); | ||
| + | } catch (Exception e) { | ||
| + | // Exception mitschicken | ||
| + | // throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, | ||
| + | // Exception loggen | ||
| + | log.warn(" | ||
| + | throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | private String extractSubject(String token) { | ||
| + | return Jwts.parserBuilder() | ||
| + | .setSigningKey(key) | ||
| + | .build() | ||
| + | .parseClaimsJws(token) | ||
| + | .getBody() | ||
| + | .getSubject(); | ||
| + | } | ||
| + | |||
| + | private String extractTokenFromHeader() { | ||
| + | ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
| + | if (attributes == null){ | ||
| + | return null; | ||
| + | } | ||
| + | String authHeader = attributes.getRequest().getHeader(" | ||
| + | if (authHeader == null || !authHeader.startsWith(" | ||
| + | return null; | ||
| + | } | ||
| + | return authHeader.substring(7); | ||
| + | } | ||
| </ | </ | ||