architecture deep dive in spring security - meetupfiles.meetup.com/6015342/spring toronto - joe...

24
Architecture Deep Dive in Spring Security Joe Grandja Spring Security Team github.com/jgrandja @joe_grandja

Upload: others

Post on 25-Feb-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Architecture Deep Dive in

Spring Security

Joe Grandja Spring Security Team

github.com/jgrandja @joe_grandja

Page 2: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

• Authentication

• Authorization

• Exception Handling

3 Key Areas in Security

Page 3: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

User Database

Username Password Authorities

[email protected] password ROLE_USER

[email protected] password ROLE_USER

[email protected] password ROLE_USER, ROLE_ADMIN

Page 4: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Demo

Page 5: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Authentication

Authorization

Exception Handling

Page 6: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Authentication Filter

Page 7: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

AuthenticationAuthentication

Principal: [email protected]: passwordAuthorities: ——

Authenticated: FALSE

public interface Authentication extends Principal, Serializable { Object getPrincipal(); Object getCredentials(); Collection<? extends GrantedAuthority> getAuthorities();

. . .}

AuthenticationPrincipal: UserDetails

Credentials: ——Authorities: ROLE_USER

Authenticated: TRUE

Page 8: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

UserDetails / Service

public interface UserDetails extends Serializable { String getUsername(); String getPassword(); Collection<? extends GrantedAuthority> getAuthorities();

. . .}

AuthenticationPrincipal: UserDetails

Credentials: ——Authorities: ROLE_USER

Authenticated: TRUE

public interface UserDetailsService { UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;}

Page 9: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Security Context

SecurityContextHolder.getContext().setAuthentication(authenticated);

public interface SecurityContext extends Serializable { Authentication getAuthentication(); void setAuthentication(Authentication authentication);}

Page 10: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

• Authentication Filter creates an “Authentication Request" and passes it to the Authentication Manager

• Authentication Manager delegates to the Authentication Provider

• Authentication Provider uses a UserDetailsService to load the UserDetails and returns an “Authenticated Principal”

• Authentication Filter sets the Authentication in the SecurityContext

Authentication Recap

Page 11: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Authentication

Authorization

Exception Handling

Page 12: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Filter Security Interceptor

Request URI: /messages/inbox

Security Metadata

Request Pattern: /messages/**

Config Attributes: ROLE_USER

AuthenticationPrincipal: UserDetails

Credentials: ——Authorities: ROLE_USER

Authenticated: TRUE

Page 13: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Access DecisionAuthentication

Principal: UserDetailsCredentials: ——Authorities: ROLE_USER

Authenticated: TRUE

Security Metadata

Request Pattern: /messages/**

Config Attributes: ROLE_USER

Request URI: /messages/inbox

Page 14: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

• FilterSecurityInterceptor obtains the “Security Metadata” by matching on the current request

• FilterSecurityInterceptor gets the current Authentication

• The Authentication, Security Metadata and Request is passed to the AccessDecisionManager

• The AccessDecisionManager delegates to it's AccessDecisionVoter(s) for decisioning

Authorization Recap

Page 15: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Authentication

Authorization

Exception Handling

Page 16: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Access Denied

Security Metadata

Request Pattern: /admin/**

Config Attributes: ROLE_ADMIN

Request URI: /admin/messages

AuthenticationPrincipal: UserDetails

Credentials: ——Authorities: ROLE_USER

Authenticated: TRUE

Page 17: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Access Denied Handler

public interface AccessDeniedHandler {

void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException;

}

Page 18: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

“Unauthenticated”

Security Metadata

Request Pattern: /messsages/**

Config Attributes: ROLE_USER

Request URI: /messages/inbox

Authentication

Principal: anonymousUser

Page 19: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Start Authentication

WWW-Authenticate: Basic realm=spring

public interface AuthenticationEntryPoint { void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException;}

Page 20: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

• When "Access Denied" for current Authentication, the ExceptionTranslationFilter delegates to the AccessDeniedHandler, which by default, returns a 403 Status.

• When current Authentication is "Anonymous", the ExceptionTranslationFilter delegates to the AuthenticationEntryPoint to start the Authentication process.

Exception Handling Recap

Page 21: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Authentication

Authorization

Exception Handling

Summary

Page 22: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Thank You! …for coming out

Page 23: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Demo Sample

Joe Grandja Spring Security Team

github.com/jgrandja @joe_grandja

github.com/jgrandja/messaging-sample

Page 24: Architecture Deep Dive in Spring Security - Meetupfiles.meetup.com/6015342/Spring Toronto - Joe Grandja.pdf•Authentication Filter creates an “Authentication Request" and passes

Spring Security Filter Chain