When adding Authentication Filter in Spring security, it is not possible to access the H2-console

Asked

Viewed 37 times

-1

Hello, I created my spring security configuration class, but when adding the authorization filter my access to H2-console was lost:

 @Configuration
    @EnableWebSecurity 
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class SecurityConfig extends WebSecurityConfigurerAdapter{
        @Autowired 
        private JWTUtil jwtUtil;
        
        @Autowired
        private UserDetailsService userDetailsService;
        
        @Autowired
        private Environment env;
    
        
            private static final String[] PUBLIC_MATCHERS = {
                    "/h2-console/**"
                    
                    
            };
            
            private static final String[] PUBLIC_MATCHERS_GET = {
                    
                    "/produtos/**",
                    "/clientes/**",
                    "/categorias/**"
            };

            @Override
            protected void configure(HttpSecurity http) throws Exception{
                
                if(Arrays.asList(env.getActiveProfiles()).contains("test")){
                    http.headers().frameOptions().disable();
                }
    
                http.cors().and().csrf().disable();
                
                http.authorizeRequests()
                        
                            .antMatchers(HttpMethod.GET, PUBLIC_MATCHERS_GET).permitAll()
                            .antMatchers(PUBLIC_MATCHERS).permitAll()
                            .anyRequest().authenticated();
                    
                
                http.addFilter(new  JWTAuthenticationFilter(authenticationManager(), jwtUtil));
                
                **http.addFilter(new  JWTAuthorizationFilter(authenticationManager(), jwtUtil, userDetailsService));**
                
                http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
            }

inserir a descrição da imagem aqui

1 answer

0

Solved.

In the Jwtauthorizationfilter class within the doFilterInternal method, I placed the call from the chain.doFilter(request, Response) method; inside the wrong if.

@Override protected void doFilterInternal ( Httpservletrequest request, Httpservletresponse Response, Filterchain) throws Ioexception, Servletexception {

    String header = request.getHeader("Authorization");
    
    if(header != null && header.startsWith("Bearer ")) {
        UsernamePasswordAuthenticationToken auth = getAuthentication(header);
        //  UsernamePasswordAuthenticationToken auth = getAuthentication(header.substring(7));  
        
        if(auth != null) {
            
            SecurityContextHolder.getContext().setAuthentication(auth);
        }
              **//Tinha colocado o metodo aqui**
    }
    
    chain.doFilter(request, response);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.