reading-notes-ma


Project maintained by mohnalkhateeb Hosted on GitHub Pages — Theme by mattgraham

Spring Authentication

Spring Security Architecture

Authentication and Access Control

Application security boils down to two more or less independent problems: authentication (who are you?) and authorization or access control (what are you allowed to do?)

Authentication

authon

Customizing Authentication Managers

Authorization or Access Control

-An AccessDecisionVoter considers an Authentication (representing a principal) and a secure Object, which has been decorated with ConfigAttributes:

    boolean supports(ConfigAttribute attribute);
    boolean supports(Class<?> clazz);
    int vote(Authentication authentication, S object,
            Collection<ConfigAttribute> attributes); ### Web Security - Spring Security in the web tier (for UIs and HTTP back ends) is based on Servlet Filters, so it is helpful to first look at the role of Filters generally. ![websec](https://github.com/spring-guides/top-spring-security-architecture/raw/main/images/filters.png)

Creating and Customizing Filter Chains

Request Matching for Dispatch and Authorization

Combining Application Security Rules with Actuator Rules

Working with Threads

Spring Security is fundamentally thread-bound, because it needs to make the current authenticated principal available to a wide variety of downstream consumers. The basic building block is the SecurityContext, which may contain an Authentication (and when a user is logged in it is an Authentication that is explicitly authenticated). You can always access and manipulate the SecurityContext through static convenience methods in SecurityContextHolder, which, in turn, manipulate a ThreadLocal.

    SecurityContext context = SecurityContextHolder.getContext();
    Authentication authentication = context.getAuthentication();
    assert(authentication.isAuthenticated); - It is not common for user application code to do this, but it can be useful if you, for instance, need to write a custom authentication filter (although, even then, there are base classes in Spring Security that you can use so that you could avoid needing to use the `SecurityContextHolder`). - If you need access to the currently authenticated user in a web endpoint, you can use a method parameter in a `@RequestMapping`, as follows:

    @RequestMapping("/foo")
    public String foo(@AuthenticationPrincipal User user) {
    ... // do stuff with user
    }

Processing Secure Methods Asynchronously

Spring Auth Cheat Sheet

Step 1: set up a user model and repo

Step 2: create a controller for that model

Step 3: UserDetailsServiceImpl implements UserDetailsService

Step 4: ApplicationUser implements UserDetails

Step 5: WebSecurityConfig extends WebSecurityConfigurerAdapter

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

Step 6: registration page

Step 7: login page