Spring Security is a powerful and highly customizable authentication and authorization framework for Spring-based applications. It provides authentication via mechanisms like username/password, LDAP, and SSO. Authorization can be implemented through voting-based access control or expression-based access control at the web (URL) level and method level. It includes filters, providers, and services to handle authentication, authorization, logout, and remember-me functionality. Configuration can be done through XML or Java configuration with support for common annotations.
Spring Framework -Security
SPRING FRAMEWORK
Dmitry Noskov Spring Security 3.0
2.
Application security
Security is arguably one of the most critical
architectural components of any application written
in the 21st century
Spring Framework - Security Dmitry Noskov
3.
What is SpringSecurity
a powerful and highly customizable authentication
and access-control framework
build on top of Spring Framework
de-facto standard for securing Spring-based
applications
Spring Framework - Security Dmitry Noskov
4.
Fundamentals (1)
principal
user that performs the action
authentication
confirming truth of credentials
authorization
define access policy for principal
Spring Framework - Security Dmitry Noskov
5.
Fundamentals (2)
Authentication
the principal in a Spring Security-specific manner
GrantedAuthority
application-wide permissions granted to a principal
SecurityContext
hold the Authentication and other security information
SecurityContextHolder
provide access to SecurityContext
Spring Framework - Security Dmitry Noskov
6.
SecurityContextHolder
provide access to SecurityContext
strategies
ThreadLocal
InreritableThreadLocal
Global
Spring Framework - Security Dmitry Noskov
7.
Getting started
SecurityContextcontext = SecurityContextHolder.getContext();
Object principal = context.getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
String username = ((UserDetails)principal).getUsername();
} else {
String username = principal.toString();
}
Spring Framework - Security Dmitry Noskov
8.
Use case
Spring Framework - Security Dmitry Noskov
Basic filters
Filter Description
ChannelProcessingFilter ensures that a request is being sent over HTTP or HTTPS
SecurityContextPersistentFilter Populates the security context using information obtained from the
repository (http session)
LogoutFilter Used to log a user out of the application
UsernamePasswordAuthenticationFilter Accepts the user’s principal and credentials and attempts to
authenticate the user
BasicAuthenticationFilter Attempts to authenticate a user by processing an HTTP Basic
authentication
ExceptionTranslationFilter Handles any AccessDeniedException or
AuthenticationException
FilterSecurityInterceptor Decides whether or not to allow access to a secured resource
http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-
config.html#ns-custom-filters
Spring Framework - Security Dmitry Noskov
Core authentication services
AuthenticationManager
handles authentication requests
AuthenticationProvider
performs authentication
UserDetailsService
responsible for returning an UserDetails object
UerDetails
provides the core user information
Spring Framework - Security Dmitry Noskov
23.
AuthenticationManager
public interface AuthenticationManager{
/* Attempts to authenticate the passed Authentication object,
* returning a fully populated Authentication object (including
* granted authorities) if successful.
* @param authentication the authentication request object
* @return a fully authenticated object including credentials
* @throws AuthenticationException if authentication fails */
Authentication authenticate(Authentication authentication)
throws AuthenticationException;
}
Spring Framework - Security Dmitry Noskov
24.
AuthenticationProvider
public interface AuthenticationProvider{
/* Performs authentication.
* @param authentication the authentication request object.
* @return a fully authenticated object including credentials.
* @throws AuthenticationException if authentication fails.*/
Authentication authenticate(Authentication authentication)
throws AuthenticationException;
/*Returns true if this provider supports the indicated
*Authentication object.*/
boolean supports(Class<? extends Object> authentication);
}
Spring Framework - Security Dmitry Noskov
25.
UserDetailsService
/*Core interface whichloads user-specific data.*/
public interface UserDetailsService {
/* Locates the user based on the username.
* @param username the username identifying the user
* @return a fully populated user record (never null)
* @throws UsernameNotFoundException if the user could not be
found or the user has no GrantedAuthority
* @throws DataAccessException if user could not be found for a
repository-specific reason*/
UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException;
}
Spring Framework - Security Dmitry Noskov
Decision managers
Decision manager Description
AffirmativeBased Allows access if at least one voter votes to grant access
ConsensusBased Allows access if a consensus of voters vote to grant access
UnanimousBased Allows access if all voters vote to grant access
Spring Framework - Security Dmitry Noskov
46.
Decision voter
public interfaceAccessDecisionVoter {
int ACCESS_GRANTED = 1;
int ACCESS_ABSTAIN = 0;
int ACCESS_DENIED = -1;
boolean supports(ConfigAttribute attribute);
boolean supports(Class<?> clazz);
int vote(Authentication authentication,
Object object,
Collection<ConfigAttribute> attributes);
}
Spring Framework - Security Dmitry Noskov
47.
Basic expressions
Expression Description
hasRole(‘ROLE_USER’) Returns true if the current principal has the specified role
hasAnyRole(‘ROLE_USER’, ‘ROLE_ADMIN’) Returns true if the current principal has any of the roles
principal Allows direct access to the principal object representing
the current user
authentication Allows direct access to the current Authentication object
obtained from the SecurityContext
permitAll Always evaluates to true
denyAll Always evaluates to false
isAnonymous() Returns true if the current principal is an anonymous user
isRememberMe() Returns true if the current principal is a remember-me user
Spring Framework - Security Dmitry Noskov
RunAsManager
/*Creates a newtemporary Authentication object.*/
public interface RunAsManager {
/ *Returns a replacement Authentication object for the current
*secure object, or null if replacement not required*/
Authentication buildRunAs(Authentication authentication,
Object object,
Collection<ConfigAttribute> attr);
boolean supports(ConfigAttribute attribute);
boolean supports(Class<?> clazz);
}
Spring Framework - Security Dmitry Noskov
Authorize (2)
<%@ taglibprefix="sec"
uri="http://www.springframework.org/security/tags" %>
<sec:authorize access="hasRole('supervisor')">
This content will only be visible to users who have
the "supervisor" authority in their list of
<tt>GrantedAuthority</tt>s.
</sec:authorize>
Spring Framework - Security Dmitry Noskov
86.
Authorize (3)
JSP
<sec:authorize url="/admin" >
This content will only be visible to users who are authorized
to send requests to the "/admin" URL.
</sec:authorize>
security interceptor
<bean id="..." class="web.access.intercept.FilterSecurityInterceptor">
<property name="securityMetadataSource">
<sec:filter-security-metadata-source>
<sec:intercept-url pattern="/admin*" access="ROLE_ADMIN"/>
</sec:filter-security-metadata-source>
</property>
</bean>
Spring Framework - Security Dmitry Noskov
87.
ACL
<%@ taglib prefix="sec"
uri="http://www.springframework.org/security/tags" %>
<sec:accesscontrollist hasPermission="1,2" domainObject="object">
This will be shown if the user has either of the permissions
represented by the values "1" or "2" on the given object.
</sec:accesscontrollist>
Spring Framework - Security Dmitry Noskov
Separation of concerns
business logic is decoupled from security concern
authentication and authorization are decoupled
Spring Framework - Security Dmitry Noskov
90.
Flexibility
authentication mechanisms
basic, form, cookies, SSO
user data storage
RDBMS, LDAP, etc.
based on Spring
Spring Framework - Security Dmitry Noskov
91.
Portability
portable across containers
can be deployed as-is
runs in standalone environment
Spring Framework - Security Dmitry Noskov