WebSecurityConfig.java 2.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package com.roshka.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
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;
12
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
13 14 15 16 17


@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
18
    
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

    @Bean
    public UserDetailsService userDetailsService() {
        return new CustomUserDetailsService();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService());
        authProvider.setPasswordEncoder(passwordEncoder());

        return authProvider;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
46 47 48
        http
//            .csrf().disable()
            .authorizeRequests()
49 50
                .mvcMatchers("/").authenticated()
                .mvcMatchers("/home").authenticated()
51 52
                .mvcMatchers("/cargos","/cargos/**").authenticated()
                .mvcMatchers("/convocatorias","/convocatorias/**").authenticated()
53
                .mvcMatchers("/tecnologias","/tecnologias/**").authenticated()
54
                .mvcMatchers("/cumples","/cumples/**").authenticated()
55
                .mvcMatchers("/beneficios","/beneficios/**").authenticated()
56 57
                .mvcMatchers("/postulantes","/postulantes/**").authenticated()
                .mvcMatchers("/edit-user-data").authenticated()
58 59
                .anyRequest().permitAll()
                .and()
60 61 62 63 64
            .formLogin()
                .loginPage("/login")
                .usernameParameter("email")
                .defaultSuccessUrl("/home")
                .permitAll()
65
                .and()
66 67 68 69 70 71 72
            .logout()
                .logoutUrl("/logout")
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
                .clearAuthentication(true)
                .invalidateHttpSession(true)
                .deleteCookies("JSESSIONID", "remember-me")
                .logoutSuccessUrl("/login");
73 74 75
    }

}