Giter Site home page Giter Site logo

jv-spring-security-part-2's Introduction

Spring security part 2

  • Configure DB authentication instead of In memory authentication

  • Add Role entity, Dao and Service layer for it.

      public interface RoleService {
          Role add(Role role);
      
          Role getByName(String roleName);
      }
  • Configure role access to specific resources for ADMIN and for USER. You should configure access to all endpoints in your application. Example:

POST: /register - all
GET: /cinema-halls - user/admin
POST: /cinema-halls - admin
GET: /movies - user/admin
POST: /movies - admin
GET: /movie-sessions/available - user/admin
POST: /movie-sessions - admin
PUT: /movie-sessions/{id} - admin
DELETE: /movie-sessions/{id} - admin
GET: /orders - user
POST: /orders/complete - user
PUT: /shopping-carts/movie-sessions - user
GET: /shopping-carts/by-user - user
GET: /users/by-email - admin
...

HINT:

  • Let's store role names as enums and add enum RoleName inside Role class.
  • Roles and first Admin user can be injected inside DataInitializer class using annotation @PostConstruct.
@PostConstruct
public void inject() {
    Role adminRole = new Role();
    adminRole.setRoleName(Role.RoleName.ADMIN);
    roleService.add(adminRole);
    Role userRole = new Role();
    userRole.setRoleName(Role.RoleName.USER);
    roleService.add(userRole);
    User user = new User();
    user.setEmail("[email protected]");
    user.setPassword("admin123");
    user.setRoles(Set.of(adminRole));
    userService.add(user);
}
  • You can specify the different HTTP method access for the same endpoint. For example:
        protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers(HttpMethod.POST,"/movies/**").hasRole("ADMIN")
                .antMatchers(HttpMethod.GET,"/movies/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .permitAll()
                .and()
                .httpBasic()
                .and()
                .csrf().disable();
    }

You can check yourself using this checklist

jv-spring-security-part-2's People

Contributors

resci avatar sofasmile avatar nick97-git avatar kseniiamakarova avatar max-pochepets avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.