#API Web not working

43 messages · Page 1 of 1 (latest)

novel citrusBOT
#

This post has been reserved for your question.

Hey @sudden plinth! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

dawn ember
#

How do you know you have no return?

novel citrusBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

sudden plinth
gray prairie
#

Is TEST printed to the console?

sudden plinth
#

Is not

gray prairie
#

If you stop the application, does it still happen?

sudden plinth
#

if i relaunch the app still same error, if i send request if app stop : Could not send request

gray prairie
#

Are there any other controllers?

#

Are you using IntelliJ Community or IntelliJ Ultimate?

sudden plinth
#

There are other controller but not using same path.
i'm Using Ultimate

#

my auth controller doesnt work, thats why i created new one to test api

gray prairie
#

Are you getting any logs when executing the request?

#

What's the package of your main class? Do you have any @Configuration classes?

#

In IntelliJ, is there an endpoint list?

sudden plinth
#

When i send the FIRST request, i got these logs. Then nothing even if i send request again

gray prairie
#

Maybe next to the console/to the left?

sudden plinth
gray prairie
#

Do you have a filter?

sudden plinth
#

i do

@Service
public class JwtFilter extends OncePerRequestFilter {

    private final JwtService jwtService;
    private final UserDetailsService userDetailsService;

    public JwtFilter(JwtService jwtService, UserDetailsService userDetailsService) {
        this.jwtService = jwtService;
        this.userDetailsService = userDetailsService;
    }

    @Override
    protected void doFilterInternal(
            @NonNull HttpServletRequest request,
             @NonNull HttpServletResponse response,
             @NonNull FilterChain filterChain
    ) throws ServletException, IOException {
        if(request.getServletPath().contains("/auth/register")) {
            filterChain.doFilter(request, response);
            return;
        }
        final String authHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
        final String jwt;
        final String userEmail;
        if (authHeader != null && authHeader.startsWith("Bearer ")) {
            jwt = authHeader.substring(7);
            userEmail = jwtService.extractUsername(jwt);
            if(userEmail != null & SecurityContextHolder.getContext().getAuthentication() == null) {
                UserDetails userDetails = userDetailsService.loadUserByUsername(userEmail);
                if (jwtService.isTokenValid(jwt, userDetails)) {
                    UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
                            userDetails,
                            null,
                            userDetails.getAuthorities()
                    );
                    authToken.setDetails(
                            new WebAuthenticationDetailsSource().buildDetails(request)
                    );
                    SecurityContextHolder.getContext().setAuthentication(authToken);
                }
            }
            filterChain.doFilter(request, response);
        }
    }
}
gray prairie
#

Is it executed?

sudden plinth
#

it doesn't enter in any if

gray prairie
#

So what should happen if it doesn't enter any if?

#

i.e. if there's no authentication

sudden plinth
#

Ok understood. So something is missing

#

(First time i use filter to implement Jwt)

gray prairie
#

What should happen if the user is not authenticated?

#

I think you want to move the last doFilter() down one line

sudden plinth
#

Thanks much !

gray prairie
#

And after that, you can do authorization in another @Configuration

sudden plinth
#

thanks, it's now clear