com.sapito.config.SecurityConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.sapito.config.SecurityConfig.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sapito.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 *
 * @author giovanni
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    @Qualifier("userDetailsService")
    UserDetailsService userDetailsService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);//.passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/admin/**").hasAuthority("JEFE_DEPARTAMENTO_ROL")
                .antMatchers("/ventas/**").hasAnyAuthority("VENTAS").antMatchers("/recursoshumanos/**")
                .hasAuthority("RH").antMatchers("/gdaAlta/**").permitAll()
                //.antMatchers("/compras/*").access("hasRole('COMPRAS')")
                .and().formLogin().loginPage("/login").failureUrl("/login?error")
                .loginProcessingUrl("/j_spring_security_check").usernameParameter("username")
                .passwordParameter("password").and().logout().logoutSuccessUrl("/login?logout")
                .logoutUrl("/j_spring_security_logout").and().csrf().disable();

    }

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

}