My springboot application (for blog) is using JWT method of authentication. When user tries to find a blog by the
Код: Выделить всё
pageIdКод: Выделить всё
NoSuchElementExceptionIn BlogService:
Код: Выделить всё
public BlogResponseDTO findBlogByPageId(String pageId) { BlogEntity blogEntity = blogRepository.findByPageId(pageId); if (blogEntity == null) { throw new NoSuchElementException("Blog not found for pageId: " + pageId); } return buildBlogResponseDTO(blogEntity); } Код: Выделить всё
401 UnauthorizedWebSecurityConfig:
Код: Выделить всё
//Imports import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; 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.Http403ForbiddenEntryPoint; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.web.multipart.support.MultipartFilter; import static org.springframework.security.config.Customizer.withDefaults; @Configuration @EnableWebSecurity public class WebSecurityConfig { @Autowired private CustomUserDetailsService userDetailsService; @Autowired private JwtRequestFilter jwtRequestFilter; @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests((auth) -> auth .requestMatchers("/auth").permitAll() .anyRequest().authenticated() ) .csrf(AbstractHttpConfigurer::disable) .httpBasic(withDefaults()) .userDetailsService(userDetailsService) .sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .exceptionHandling(httpSecurityExceptionHandlingConfigurer -> { httpSecurityExceptionHandlingConfigurer.authenticationEntryPoint((request, response, authException) -> { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.getWriter().write("Authentication failed: " + authException.getMessage()); }); }); http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class); return http.build(); } @Bean public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception { return authenticationConfiguration.getAuthenticationManager(); } } Источник: https://stackoverflow.com/questions/781 ... -exception
Мобильная версия