Ao adicionar Filtro de Autenticação no Spring security, não é possível acessar o h2-console

22/08/2020

0

Ola, criei a minha classe de configuração do spring security, mas ao adicionar o filtro de autorização o meu acesso ao h2-console foi perdido:
Gostaria de saber se esta faltando alguma configurção para liberar o h2-console?

@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);
		}
Abmael Ferreira

Abmael Ferreira

Responder

Posts

22/08/2020

Abmael Ferreira

Ola, criei a minha classe de configuração do spring security, mas ao adicionar o filtro de autorização o meu acesso ao h2-console foi perdido:
Gostaria de saber se esta faltando alguma configurção para liberar o h2-console?

@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);
		}
Responder

22/08/2020

Abmael Ferreira

Resolvido.

Na classe JWTAuthorizationFilter dentro do método doFilterInternal, eu coloquei a chamada do metodo chain.doFilter(request, response); dentro do if errado.

@Override
	protected void doFilterInternal ( HttpServletRequest request, HttpServletResponse response, FilterChain chain) 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);
	}
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar