1
resposta

HTTP Status 403 – Forbidden

Ao preencher os campos com os dados de usuário e senha (da forma feita na aula) sou recebido com o seguinte erro:

HTTP Status 403 – Forbidden
Type Status Report

Message Acesso negado

Description The server understood the request but refuses to authorize it.

Apache Tomcat/9.0.37

Tal erro não ocorre quando "driblo" a autenticação com o acréscimo da barra ao final do endereço na URL assim como está descrito na classe SecurityConfiguration:

package br.com.casadocodigo.loja.conf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.web.filter.CharacterEncodingFilter;

import br.com.casadocodigo.loja.dao.UsuarioDAO;

@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UsuarioDAO usuarioDao;

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

        http.authorizeRequests() 
            .antMatchers("/produtos/form").hasRole("ADMIN")
            .antMatchers(HttpMethod.POST, "/produtos").hasRole("ADMIN")
            .antMatchers(HttpMethod.GET, "/produtos").hasRole("ADMIN")
            .antMatchers("/produtos/**").permitAll()
            .antMatchers("/carrinho/**").permitAll()
            .antMatchers("/pagamento/**").permitAll()
            .antMatchers("/resources/**").permitAll()
            .antMatchers("/").permitAll()
            .anyRequest().authenticated()
            .and().formLogin();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(usuarioDao)
            .passwordEncoder(new BCryptPasswordEncoder());
    }
}

Agredeço a atenção e estou a disposição para enviar mais dados caso necessário.

1 resposta

Olá Guilherme, tudo bem? Acho que esse seu tópico é o mesmo que esse outro tópico, então podemos continuar por lá? !

Aguardo retorno lá!

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