Как получить идентификатор пользователя от незащищенных URL -адресов?JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Как получить идентификатор пользователя от незащищенных URL -адресов?

Сообщение Anonymous »

У меня есть аутентифицированный класс, который расширяет org.springframework.security.core.userdetails.user. Он построен из jwt. < /P>
Каждый запрос из браузера имеет 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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»