package ciai.security; import org.springframework.beans.factory.annotation.Autowired; 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.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService users; @Autowired AuthenticationFailure customAuthFailure; @Autowired AuthenticationSuccess customAuthSucc; @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers("/img/**", "/js/**", "/").permitAll() .antMatchers("/student/**").hasRole("STUDENT") .antMatchers("/edition/**").hasAnyRole("PROFESSOR","ASSISTANT") .antMatchers("/professor/**").hasAnyRole("PROFESSOR","ASSISTANT").and() .formLogin() .permitAll() .loginPage("/login") .successHandler(customAuthSucc) .failureHandler(customAuthFailure).and() .logout().logoutSuccessUrl("/").and() .exceptionHandling().accessDeniedPage("/").and(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(users).passwordEncoder(new BCryptPasswordEncoder()); } }