liveBook · Manning

Authorization

As explained in Spring in Action, Sixth Edition

Authorization is the process of determining which actions or resources an authenticated entity is permitted to access.

Authorization determines which requests an authenticated user may access. In Spring Security, you can match URL paths to requirements such as a granted role or permission for everyone. section 5.3.1

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  return http
    .authorizeRequests()
      .antMatchers("/design", "/orders").hasRole("USER")
      .antMatchers("/", "/**").permitAll()
 
    .and()
    .build();
}

This configuration protects /design and /orders with the USER role and permits other requests. section 5.3.1

When writing URL rules, keep these behaviors in mind:

  • Rules are checked in declaration order, so put specific protected paths before a broad permit-all rule. section 5.3.1
  • hasRole("USER") checks for ROLE_USER; Spring supplies the ROLE_ prefix. section 5.3.1 section 5.2.2

A conditional access rule

Spring Expression Language (SpEL) lets a rule combine conditions. For example, a rule can require ROLE_USER and allow taco creation only on Tuesdays. section 5.3.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines path rules, role checks, rule precedence, and richer SpEL conditions with configuration examples.
Examples
Uses /design and /orders role checks, public routes, and a role-plus-weekday condition.
What you can do after
Configure URL access rules with role checks, permit-all behavior, or SpEL conditions.
How it connects
Connects authorization to authentication, granted authorities, roles, Spring Expression Language, and SecurityFilterChain configuration.

Also explained in

Compare how 28 books explain authorization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.