mirror of
https://github.com/pcvolkmer/onkostar-plugin-dnpm.git
synced 2025-07-03 17:52:53 +00:00
Issue #24: Füge Annotationen und Spring-AOP Aspect zum Absichern von Methodenaufrufen hinzu
This commit is contained in:
@ -0,0 +1,13 @@
|
||||
package DNPM.security;
|
||||
|
||||
public class IllegalSecuredObjectAccessException extends RuntimeException {
|
||||
|
||||
public IllegalSecuredObjectAccessException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public IllegalSecuredObjectAccessException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.security.access.PermissionEvaluator;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.Serializable;
|
||||
@ -14,6 +15,7 @@ import java.util.List;
|
||||
/**
|
||||
* Permission-Evaluator zur Auswertung der Berechtigung auf Objekte aufgrund der Personenstammberechtigung
|
||||
*/
|
||||
@Component
|
||||
public class PersonPoolBasedPermissionEvaluator implements PermissionEvaluator {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
@ -71,7 +73,7 @@ public class PersonPoolBasedPermissionEvaluator implements PermissionEvaluator {
|
||||
var userDetails = (UserDetails)authentication.getPrincipal();
|
||||
|
||||
return jdbcTemplate
|
||||
.query(sql, new Object[]{userDetails.getUsername()}, (rs, rowNum) -> rs.getString("id"));
|
||||
.query(sql, new Object[]{userDetails.getUsername()}, (rs, rowNum) -> rs.getString("kennung"));
|
||||
}
|
||||
|
||||
|
||||
|
14
src/main/java/DNPM/security/PersonPoolSecured.java
Normal file
14
src/main/java/DNPM/security/PersonPoolSecured.java
Normal file
@ -0,0 +1,14 @@
|
||||
package DNPM.security;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface PersonPoolSecured {
|
||||
|
||||
PermissionType value() default PermissionType.READ_WRITE;
|
||||
|
||||
}
|
14
src/main/java/DNPM/security/PersonPoolSecuredResult.java
Normal file
14
src/main/java/DNPM/security/PersonPoolSecuredResult.java
Normal file
@ -0,0 +1,14 @@
|
||||
package DNPM.security;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface PersonPoolSecuredResult {
|
||||
|
||||
PermissionType value() default PermissionType.READ_WRITE;
|
||||
|
||||
}
|
74
src/main/java/DNPM/security/SecurityAspects.java
Normal file
74
src/main/java/DNPM/security/SecurityAspects.java
Normal file
@ -0,0 +1,74 @@
|
||||
package DNPM.security;
|
||||
|
||||
import de.itc.onkostar.api.Patient;
|
||||
import de.itc.onkostar.api.Procedure;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Component
|
||||
@Aspect
|
||||
public class SecurityAspects {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private final PersonPoolBasedPermissionEvaluator permissionEvaluator;
|
||||
|
||||
public SecurityAspects(PersonPoolBasedPermissionEvaluator permissionEvaluator) {
|
||||
this.permissionEvaluator = permissionEvaluator;
|
||||
}
|
||||
|
||||
@AfterReturning(value = "@annotation(PersonPoolSecuredResult) ", returning = "patient")
|
||||
public void afterPatient(Patient patient) {
|
||||
if (
|
||||
null != patient
|
||||
&& ! permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), patient, PermissionType.READ_WRITE)
|
||||
) {
|
||||
logger.warn("Rückgabe von Patient blockiert: {}", patient.getId());
|
||||
throw new IllegalSecuredObjectAccessException();
|
||||
}
|
||||
}
|
||||
|
||||
@AfterReturning(value = "@annotation(PersonPoolSecuredResult)", returning = "procedure")
|
||||
public void afterProcedure(Procedure procedure) {
|
||||
if (
|
||||
null != procedure
|
||||
&& ! permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), procedure, PermissionType.READ_WRITE)
|
||||
) {
|
||||
logger.warn("Rückgabe von Prozedur blockiert: {}", procedure.getId());
|
||||
throw new IllegalSecuredObjectAccessException();
|
||||
}
|
||||
}
|
||||
|
||||
@Before(value = "@annotation(PersonPoolSecured)")
|
||||
public void beforePatient(JoinPoint jp) {
|
||||
Arrays.stream(jp.getArgs())
|
||||
.filter(arg -> arg instanceof Patient)
|
||||
.forEach(patient -> {
|
||||
if (! permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), patient, PermissionType.READ_WRITE)) {
|
||||
logger.warn("Zugriff auf Patient blockiert: {}", ((Patient)patient).getId());
|
||||
throw new IllegalSecuredObjectAccessException();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Before(value = "@annotation(PersonPoolSecured)")
|
||||
public void beforeProcedure(JoinPoint jp) {
|
||||
Arrays.stream(jp.getArgs())
|
||||
.filter(arg -> arg instanceof Procedure)
|
||||
.forEach(procedure -> {
|
||||
if (! permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), procedure, PermissionType.READ_WRITE)) {
|
||||
logger.warn("Zugriff auf Prozedur blockiert: {}", ((Procedure)procedure).getId());
|
||||
throw new IllegalSecuredObjectAccessException();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user