content: package spring.security.config;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.security.web.access.AccessDeniedHandler;
  
  
  /**
   * @Created 15 / 05 / 2020 - 10:08 AM
   * @project SpringSecurity
   * @Author Hamdamboy
   */
  
  @Configuration
  @EnableWebSecurity
  public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private AccessDeniedHandler accessDeniedHandler;
  
      @Override
      protected void configure(HttpSecurity httpSecurity) throws Exception {
          httpSecurity
                  .authorizeRequests()
                  .antMatchers("/","/home","/about").permitAll()
                  .antMatchers("/admin/**").hasAnyRole("ADMIN")
                  .antMatchers("/user/**").hasAnyRole("USER")
                  .anyRequest().authenticated()
                  .and()
                  .formLogin()
                  .loginPage("/login")
                  .permitAll()
                  .and()
                  .logout()
                  .permitAll()
                  .and()
                  .exceptionHandling().accessDeniedHandler(accessDeniedHandler);
      }
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  
          auth.inMemoryAuthentication()
                  .withUser("user").password("user123").roles("USER")
                  .and()
                  .withUser("admin").password("admin123").roles("ADMIN");
      }
  
      @Bean
      PasswordEncoder passwordEncoder(){
          return new BCryptPasswordEncoder();
      }
  
  }
  