Class SecurityEvaluationContextExtension
java.lang.Object
org.springframework.security.data.repository.query.SecurityEvaluationContextExtension
- All Implemented Interfaces:
org.springframework.data.spel.spi.EvaluationContextExtension,org.springframework.data.spel.spi.ExtensionIdAware
public class SecurityEvaluationContextExtension
extends Object
implements org.springframework.data.spel.spi.EvaluationContextExtension
By defining this object as a Bean, Spring Security is exposed as SpEL expressions for creating Spring Data queries.
With Java based configuration, we can define the bean using the following:
For example, if you return a UserDetails that extends the following User object:
@Entity
public class User {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long id;
...
}
And you have a Message object that looks like the following:
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne
private User to;
...
}
You can use the following Query annotation to search for only messages that are
to the current user:
@Repository
public interface SecurityMessageRepository extends MessageRepository {
@Query("select m from Message m where m.to.id = ?#{ principal?.id }")
List<Message> findAll();
}
This works because the principal in this instance is a User which has an id field on
it.- Since:
- 4.0
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance that uses the currentAuthenticationfound on theSecurityContextHolder.SecurityEvaluationContextExtension(Authentication authentication) Creates a new instance that always uses the sameAuthenticationobject. -
Method Summary
Modifier and TypeMethodDescriptionvoidsetDefaultRolePrefix(String defaultRolePrefix) Sets the default prefix to be added toSecurityExpressionRoot.hasAnyRole(String...)orSecurityExpressionRoot.hasRole(String).voidsetPermissionEvaluator(PermissionEvaluator permissionEvaluator) Sets thePermissionEvaluatorto be used.voidsetRoleHierarchy(RoleHierarchy roleHierarchy) Sets theRoleHierarchyto be used.voidsetSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) Sets theSecurityContextHolderStrategyto use.voidsetTrustResolver(AuthenticationTrustResolver trustResolver) Sets theAuthenticationTrustResolverto be used.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.data.spel.spi.EvaluationContextExtension
getFunctions, getProperties
-
Constructor Details
-
SecurityEvaluationContextExtension
public SecurityEvaluationContextExtension()Creates a new instance that uses the currentAuthenticationfound on theSecurityContextHolder. -
SecurityEvaluationContextExtension
Creates a new instance that always uses the sameAuthenticationobject.- Parameters:
authentication- theAuthenticationto use
-
-
Method Details
-
getExtensionId
- Specified by:
getExtensionIdin interfaceorg.springframework.data.spel.spi.ExtensionIdAware
-
getRootObject
- Specified by:
getRootObjectin interfaceorg.springframework.data.spel.spi.EvaluationContextExtension
-
setSecurityContextHolderStrategy
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) Sets theSecurityContextHolderStrategyto use. The default action is to use theSecurityContextHolderStrategystored inSecurityContextHolder.- Since:
- 5.8
-
setTrustResolver
Sets theAuthenticationTrustResolverto be used. Default isAuthenticationTrustResolverImpl. Cannot be null.- Parameters:
trustResolver- theAuthenticationTrustResolverto use- Since:
- 5.8
-
setRoleHierarchy
Sets theRoleHierarchyto be used. Default isNullRoleHierarchy. Cannot be null.- Parameters:
roleHierarchy- theRoleHierarchyto use- Since:
- 5.8
-
setPermissionEvaluator
Sets thePermissionEvaluatorto be used. Default isDenyAllPermissionEvaluator. Cannot be null.- Parameters:
permissionEvaluator- thePermissionEvaluatorto use- Since:
- 5.8
-
setDefaultRolePrefix
Sets the default prefix to be added toSecurityExpressionRoot.hasAnyRole(String...)orSecurityExpressionRoot.hasRole(String). For example, if hasRole("ADMIN") or hasRole("ROLE_ADMIN") is passed in, then the role ROLE_ADMIN will be used when the defaultRolePrefix is "ROLE_" (default).- Parameters:
defaultRolePrefix- the default prefix to add to roles. The default is "ROLE_".- Since:
- 5.8
-