Class AbstractAccessCheckInterceptor
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
InMemoryAccessCheckInterceptor
AccessCheck.
How it works:
- Intercepts any method annotated with
@AccessCheck - Determines the resource identifier:
- For
callAccessPointMethod()on aBaseDyntabCdiBean(which is annotated with@AccessCheck): uses the bean'suniqueIdentifier(from the@DynTabannotation that matches this specific tab instance). This solves the problem of multiple@DynTabannotations on the same class with differentsecuredResourcevalues. - For other methods: uses the fully qualified method name
(
com.example.MyBean.myMethod)
- For
- Calls
isResourceSecured(String)— if not secured, allows access - If secured, retrieves user roles from the session and calls
hasPermission(String, Set) - If denied and the method is
callAccessPointMethod(), setsaccessDenied=trueon the bean and proceeds (the bean renders an "access denied" page instead of the main content) - If denied and it's a regular method, shows a FacesMessage error and throws a RuntimeException
Two implementation strategies:
- Declarative (InMemory): Use
InMemoryAccessCheckInterceptor, paired withInMemorySecuredResourceScanner. Access rules are declared directly in annotations (@DynTab(allowedRoles=...)and@AccessCheck(allowedRoles=...)). This is the default approach provided by the DynTabs library. - DB-based: The developer creates a custom
DBAccessCheckInterceptorextending this class, paired with a customDBSecuredResourceScanner. TheallowedRolesannotation attribute is ignored — roles are managed through an Admin UI and stored in the database.
User roles: By default, reads user roles from the session attribute
"user_roles" (a Set<String>). Override getUserRoles()
to customize.
IMPORTANT: Any interceptor applied to a ViewScoped or TabScoped CDI bean
MUST implement Serializable, otherwise passivation will fail.
NOTE: CDI interceptors only intercept external method calls (via proxy).
When a bean calls its own @AccessCheck method internally,
the interceptor will NOT fire.
- Author:
- DynTabs
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncheckPermissions(jakarta.interceptor.InvocationContext context) Retrieves the current user's roles from the session.protected abstract booleanhasPermission(String resource, Set<String> userRoles) Checks whether the given user roles grant access to the specified resource.protected abstract booleanisResourceSecured(String resource) Checks whether the given resource is secured (requires access control).
-
Field Details
-
log
protected static final org.slf4j.Logger log
-
-
Constructor Details
-
AbstractAccessCheckInterceptor
public AbstractAccessCheckInterceptor()
-
-
Method Details
-
checkPermissions
- Throws:
Exception
-
getUserRoles
Retrieves the current user's roles from the session.Default implementation reads the
"user_roles"attribute from the JSF external context session map. Override to customize (e.g., read from a different session attribute, or from Jakarta Security).- Returns:
- set of role names for the current user, or empty set if not available
-
isResourceSecured
Checks whether the given resource is secured (requires access control).If this returns
false, access is allowed without any role check.- Parameters:
resource- the resource identifier (uniqueIdentifier for tabs, or fully qualified method name)- Returns:
- true if the resource is secured and requires permission check
-
hasPermission
Checks whether the given user roles grant access to the specified resource.Called only if
isResourceSecured(String)returnedtrue.- Parameters:
resource- the resource identifieruserRoles- the current user's roles- Returns:
- true if access should be granted
-