42-Securing-REST-Application Flashcards
List out security concepts
Principal Authentication Authorization Authority Secure resource
What is authority?
Permission or credential enabling access. The most familiar example of authority is a role (string)
List out 5 authentication mechanisms:
Basic Digest Form X.509 OAuth
What is LDAP?
DAP: directory access protocol
LDAP: DAP using TCP/IP
What does authorization do?
check if user has the required authority
What are the advantages of Spring Security?
- Portable: can use for any Spring project
- Separation of concerns
- Flexible & extensible: support many:
a. authentication mechanisms
b. storages
c. customizable
What does AuthenticationManager do?
authenticate(Authentication) return full Authentication object if successful. Throw AuthenticationException or sub-class if fails
What inside Authentication?
- List of GrantedAuthority
- Principal: Object
- Credentials: Object
What does AccessDecisionManager do?
Holding list of Voter to decide if the user allows accessing a secured resource
List out default voters in AccessDecisionManager
- RoleVoter: having the right role or not
2. AuthenticatedVoter: logged in as a valid user or not
What is the result of AccessDecisionManager.decide()?
- AccessDeniedException: same as 403
- InsufficientAuthenticationException: same as 401
- successful otherwise
In the big picture of Spring Security, how do components work together to protects secured resources?
- AuthenticationManager populates SecurityContext with Authentication object
- Security Interceptor obtains SecurityContext and passes it to AccessDecisionManager to check
- If pass, allow access to the secured resource
What are the steps to setup Spring security in a web environment?
- Setup Filter Chain - Spring Boot does this for you
- Configure authorization rules
- Setup web authentication
How springSecurityFilterChain works with DelegatingFilterProxy?
- DelegatingFilterProxy stays in front of DispatcherServlet
- DelegatingFilterProxy delegates request to springSecurityFilterChain to check authentication/authorization
- springSecurityFilterChain having a list of filters to check the permission
- If successful, DelegatingFilterProxy will forward the request to DispatcherServlet
Note: all implement javax.servlet.Filter
List out default filters in springSecurityFilterChain
- SecurityContextPersistenceFilter
- LogoutFilter
- UsernamePasswordAuthenticationFilter
- ExceptionTranslationFilter
- FilterSecurityInterceptor: does the hard work of performing permission checking
What does SecurityContextPersistenceFilter do?
- Finds security context in session and sets it for the current thread
- Stores security context back into session
What does UsernamePasswordAuthenticationFilter do?
Puts Authentication into the SecurityContext on login request.
What does ExceptionTranslationFilter do?
Converts SpringSecurity exceptions into HTTP response or redirect
What is the default username?
user
How to configure custom security for your application?
@Configuration
@EnableWebSecurity // not need if Spring Boot
class ClassName extends WebSecurityConfigurerAdapter
How to enable security in a web app?
Using @EnableWebSecurity if not using Spring Boot
Ant style: /admin/* vs. /admin/**
/admin/** any path under /admin
/admin/* only matches /admin/xxx
antMatchers vs. mvcMatchers
Similar but there are some issue with antMatchers e.g. antMatchers(“/admin”) matches “/admin/”: potential security risk
mvcMatchers: recommended
Overloads of mvcMatchers
- mvcMatchers(path1, path2)
2. mvcMatchers(httpMethod, path)
hasRole vs. hasAnyRole
e. g. hasRole(roleName)
e. g. hasAnyRole(roleName1, roleName2)
What is the prefix value of authority?
ROLE_
Write custom role checking?
Using SpEL:
mvcMatchers(“…”).access(“hasRole(‘ADMIN’) && hasRole(‘USER’)”)