2
respostas

Acesso negado ao acessar o endpoint /swagger-ui.html

package com.sys.api.infra.security;

import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
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.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@EnableWebSecurity
@Configuration
@AllArgsConstructor
public class SecurityConfiguration {
    private SecurityFilter securityFilter;

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                .sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS)).authorizeHttpRequests(req -> {
                    req.requestMatchers(HttpMethod.POST, "/api/v1/signin", "/api/v1/signup").permitAll();
                    req.requestMatchers(HttpMethod.GET, "/swagger-ui/**", "/v3/api-docs/**","/swagger-ui.html").permitAll();
                    req.anyRequest().authenticated();
                })
                .csrf(AbstractHttpConfigurer::disable)
                .addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class).build();
    }

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

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
package com.sys.api.infra.security;

import com.sys.api.domain.repository.UserRepository;
import jakarta.annotation.Nonnull;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;


@Component
@AllArgsConstructor
@Slf4j
public class SecurityFilter extends OncePerRequestFilter {
    private TokenService tokenService;
    private UserRepository userRepository;

    @Override
    protected void doFilterInternal(@Nonnull HttpServletRequest request,@Nonnull HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String tokenJWT = getToken(request);
        if (tokenJWT != null) {
            String subject = tokenService.getSubject(tokenJWT);
            if (subject != null) {
                UserDetails user = userRepository.findByUsername(subject);
                if (user != null) {
                    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
                    SecurityContextHolder.getContext().setAuthentication(authentication);
                }
            }
        }
        filterChain.doFilter(request, response);
    }

    private String getToken(HttpServletRequest request) {
       var authorizationHeader =  request.getHeader("Authorization");
       if (authorizationHeader != null) {
           return authorizationHeader.replace("Bearer ", "").trim();
       }
       return null;
    }
}
2 respostas

Modo debug mostrou isso 2024-06-20T22:51:33.039-03:00 DEBUG 153196 --- [nio-8080-exec-3] o.s.s.w.session.SessionManagementFilter : Request requested invalid session id 27268CE95CA8DD79CC666F2478F0A664 2024-06-20T22:51:33.040-03:00 DEBUG 153196 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : Secured GET /swagger-ui.html 2024-06-20T22:51:33.044-03:00 DEBUG 153196 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : Securing GET /error 2024-06-20T22:51:33.044-03:00 DEBUG 153196 --- [nio-8080-exec-3] o.s.s.w.a.AnonymousAuthenticationFilter : Set SecurityContextHolder to anonymous SecurityContext 2024-06-20T22:51:33.044-03:00 DEBUG 153196 --- [nio-8080-exec-3] o.s.s.w.a.Http403ForbiddenEntryPoint : Pre-authenticated entry point called. Rejecting access

Oi!

Manda aqui como está o seu pom.xml.