5
respostas

Configurações no Spring Security 2.7?

Como fazer essas configurações no Spring Security 2.7.0 ? Pois, a classe WebSecurityConfigurerAdapter está depracated.

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

@Override
protected void configure(HttpSecurity http) throws Exception {}

@Override
protected void configure(WebSecurity web) throws Exception {}
5 respostas

Oi Phillip,

Agora a classe deve ficar assim:

@Configuration
public class SecurityConfigurations {

    @Autowired
    private AutenticacaoService autenticacaoService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UsuarioRepository usuarioRepository;

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

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

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeRequests()
        .antMatchers(HttpMethod.GET, "/topicos").permitAll()
        .antMatchers(HttpMethod.GET, "/topicos/*").permitAll()
        .antMatchers(HttpMethod.POST, "/auth").permitAll()
        .antMatchers(HttpMethod.GET, "/actuator/**").permitAll()
        .anyRequest().authenticated()
        .and().csrf().disable()
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and().addFilterBefore(new AutenticacaoViaTokenFilter(tokenService, usuarioRepository), UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().antMatchers("/**.html", "/v2/api-docs", "/webjars/**", "/configuration/**", "/swagger-resources/**");
    }

}

Bons estudos!

A classe AutenticacaoService não é chamada em nenhum ponto do código que enviou Rodrigo, está faltando alguma tratativa, posso somente retira-lo? Ao mesmo tempo a classe AutenticacaoViaTokenFilter também não existe no projeto até então então não compila.

Oi João,

A clase AutenticacaoViaTokenFilter vai ser desenvolvida posteriormente no curso. Pode ignorar por enquanto.

A parte do AutenticacaoService não precisa mais configurar no SecurityConfigurations, pois o Spring detecta automaticamente.

faltou o return do security filter Chain; acredito que seja

return http.build();

Verdade Mario!

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.authorizeRequests()
    .antMatchers(HttpMethod.GET, "/topicos").permitAll()
    .antMatchers(HttpMethod.GET, "/topicos/*").permitAll()
    .antMatchers(HttpMethod.POST, "/auth").permitAll()
    .antMatchers(HttpMethod.GET, "/actuator/**").permitAll()
    .anyRequest().authenticated()
    .and().csrf().disable()
    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and().addFilterBefore(new AutenticacaoViaTokenFilter(tokenService, usuarioRepository), UsernamePasswordAuthenticationFilter.class);

    return http.build();
}

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software