Каждый запрос из браузера имеет JWT, прикрепленный к запросу. Страницы либо защищены, либо публики. Если страница защищена, мой @AuthenticationPrincipal AuthenticatedUser Принципал заполняется, но если страница не защищена, моя @AuthenticationPrincipal AuthenticatedUser Principal не заполнена. И проблема возникает здесь. Теперь у меня есть случай, когда я хочу получить @AuthenticationPrincipal AuthenticatedUser Principal , даже если страница не защищена. Но теперь на незащищенных страницах мой @AuthenticationPrincipal AuthenticatedUser Принципал кажется нулевым, хотя я отправляю jwt.
Код: Выделить всё
@Configuration
@EnableWebSecurity
public class ResourceServerConfig {
private final List protectedPaths = List.of(
"/secret/*/**",
"/private"
);
// here if I add the public into the list principal is populated but the anon users cannot access the public link. If I don't add the public link then principal appears to be null.
@Bean
@Order(2)
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.securityMatchers(customizer -> customizer.requestMatchers(protectedPaths.toArray(new String[0])))
.csrf().disable()
.authorizeHttpRequests(requests -> requests.anyRequest().authenticated())
.oauth2ResourceServer()
.jwt(customizer -> customizer.jwtAuthenticationConverter(new UserAuthenticationTokenConverter()));
return http.build();
}
}
< /code>
и мой контроллер: < /p>
@GetMapping(path = "/public/{id}")
public ListingDetailedView
findById(@PathVariable String id, @AuthenticationPrincipal AuthenticatedUser principal) {
// here principal is null
return listingService.findBySearchId(id, principal);
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... ected-urls
Мобильная версия