#secure my auth with being able to acces swagger

1 messages · Page 1 of 1 (latest)

spark lake
#

// @Override
// protected boolean shouldNotFilter(HttpServletRequest request) {
// return PUBLIC_URLS.stream().anyMatch(m -> m.matches(request));
// }

@Override
protected boolean shouldNotFilter(HttpServletRequest request) {
    return true;
}
hazy spadeBOT
#

This post has been reserved for your question.

Hey @spark lake! 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 marked as dormant 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.

hazy spadeBOT
spark lake
#

don t want that and don t wanna enter pws

manic radishBOT
#

@Component ```java

public class JWTFilter extends OncePerRequestFilter {
private static final String BEARER_PREFIX = "Bearer ";

private final UserRepository userRepository;
private final UserService userService;
private final JWTUtil jwtTokenUtil;

private static final List<RequestMatcher> PUBLIC_URLS =
        Arrays.stream(SecurityConfig.PUBLIC_URL_PATTERNS)
                .map(p -> (RequestMatcher) new AntPathRequestMatcher(p))
                .toList();

@Autowired
public JWTFilter(
        UserService userService,
        JWTUtil jwtTokenUtil,
        UserRepository userRepository
) {
    this.userService = userService;
    this.jwtTokenUtil = jwtTokenUtil;
    this.userRepository = userRepository;
}




@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException  {
    final Optional<String> authHeader = Optional.ofNullable(request.getHeader("Authorization"));

    if(authHeader.isPresent()) {
        authHeader
                .map(header -> header.substring(BEARER_PREFIX.length()))
                .map(jwtTokenUtil::validateTokenAndRetrieveSubject)
                .map(email -> userRepository
                        .findByEmail(email)
                        .orElseThrow(() -> new IllegalArgumentException("Invalid bearer token")))
                .ifPresent(user -> {
                    UserDetails userDetails = userService.loadUserByUsername(user.getEmail());
                    UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(user, userDetails.getPassword(), userDetails.getAuthorities());
                    SecurityContextHolder.getContext().setAuthentication(authToken);
                });
    }
    filterChain.doFilter(request, response);
}

} ```

This message has been formatted automatically. You can disable this using /preferences.

spark lake
hazy spadeBOT
#

💤 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.

spark lake
hardy raven
#

try this i wanna see what it prints

spark lake
#

Will check when ik home going to work rn

hazy spadeBOT
#

💤 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.

spark lake
#

Thank you

hazy spadeBOT
# spark lake Thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.