Código Java do spring security
package br.com.netsoft.seguranca;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
/**
* @author Guilherme Costa
*/
public class SecurityWebConfig extends WebSecurityConfigurerAdapter {
@Autowired
private LoginSucessHandler loginSucessHandler;
@Autowired
private NotaFiscalSeguranca comercialUserDetailsService;
/**
* Utilizamos o passwordEncoder para encriptografar a senha do usuário
*
* @return PasswordEncoder;
*/
@Bean
public PasswordEncoder passwordEncoder() {
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* Sobrescrevendo o metodo configure da classe WebSecurityConfigurerAdapter
* do pacote
* org.springframework.security.config.annotation.web.configuration
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
// Configuração para todos usuarios do sistema
.antMatchers("/error/**", "/resources/**", "/jsCss/**", "/webjars/**", "/recuperarSenha").permitAll()
// Configuração para todos usuarios com permissão de
// ROLE_ADMINISTRADOR
.antMatchers("/codigo/**", "/subCodigo/**", "/tipoCredito/**", "/tipoCancelamento/**", "/usuario/**",
"/servico/**", "/notaFiscal/**", "/erroAlerta/**", "/credito/**", "/configuracao/**",
"/cnaeSubCodigo/**", "/cnae/**", "/erroAlerta/**", "/atualizacaoMonetariaItem/**",
"/atualizacaoMonetaria/**", "/dashboardAdmin/**", "/porcentagemReter/**")
.access("hasRole('ROLE_ADMINISTRADOR')")
// Configuração para todos usuarios do sistema
.and().formLogin().loginPage("/login").successHandler(loginSucessHandler).permitAll().and().rememberMe()
// Logout
.and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).and().sessionManagement()
.maximumSessions(1).maxSessionsPreventsLogin(true).expiredUrl("/login")
.sessionRegistry(sessionRegistry());
}
// Work around https://jira.spring.io/browse/SEC-2855
@Bean
public SessionRegistry sessionRegistry() {
SessionRegistry sessionRegistry = new SessionRegistryImpl();
return sessionRegistry;
}
/**
* Sobrescrevendo o metodo configure da classe WebSecurityConfigurerAdapter
* do pacote
* org.springframework.security.config.annotation.web.configuration
*/
@Override
protected void configure(AuthenticationManagerBuilder builder) throws Exception {
builder.userDetailsService(comercialUserDetailsService).passwordEncoder(new BCryptPasswordEncoder());
}
}