{"id":340651,"date":"2022-11-02T15:00:17","date_gmt":"2022-11-02T15:00:17","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=340651"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=340651","title":{"rendered":"<span>Security, Cloud \u0441 JWT \u0438 WebFlux<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>Spring Security &#8212; \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043a\u0440\u0443\u0442\u0430\u044f \u0448\u0442\u0443\u043a\u0430, \u043d\u0430 \u0442\u0435\u043c\u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043d\u043e\u0433\u043e \u0433\u0430\u0439\u0434\u043e\u0432, \u0441\u0442\u0430\u0442\u0435\u0439 \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430\u0445. \u041d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u044d\u0442\u0438\u0445 \u0432\u0438\u0434\u0435\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u043e\u043d\u043e\u043b\u0438\u0442\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0439. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0441\u0432\u043e\u0435\u043c \u043b\u0438\u0447\u043d\u043e\u043c \u043e\u043f\u044b\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0435\u0435 \u0434\u043b\u044f \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u044d\u0442\u043e \u043d\u0435 \u0441\u0442\u0430\u0442\u044c\u044f \u0443\u0440\u043e\u0432\u043d\u044f \u0422\u0430\u0433\u0438\u0440\u0430 \u0412\u0430\u043b\u0435\u0435\u0432\u0430. \u042d\u0442\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u0438\u0447\u043d\u044b\u0439 \u043e\u043f\u044b\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f, \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c, \u043a\u043e\u043c\u0443 \u0442\u043e \u043e\u043d \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c.<\/p>\n<p><strong>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0438 \u0432\u044b\u0434\u0430\u0447\u0438 JWT \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c (\u043a\u0440\u0430\u0442\u043a\u043e)<\/p>\n<\/li>\n<li>\n<p>\u041c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 (\u043a\u0440\u0430\u0442\u043a\u043e)<\/p>\n<\/li>\n<li>\n<p>Security \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0440\u043e\u043b\u0435\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/df8\/7e2\/e62\/df87e2e622bc81f74807f4a895f1e8cd.png\" width=\"599\" height=\"336\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/df8\/7e2\/e62\/df87e2e622bc81f74807f4a895f1e8cd.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438:<\/strong><\/p>\n<ul>\n<li>\n<p>Spring Boot<\/p>\n<\/li>\n<li>\n<p>Spring Cloud<\/p>\n<\/li>\n<li>\n<p>Spring Security<\/p>\n<\/li>\n<li>\n<p>JWT<\/p>\n<\/li>\n<li>\n<p>WebFlux<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u0435\u0445\u0430\u043d\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0434\u0443\u043c\u0430\u044e, \u043c\u043d\u043e\u0433\u0438\u043c \u043f\u043e\u043d\u044f\u0442\u043d\u0430. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 \u043d\u0438\u0436\u0435 \u0432\u043a\u0440\u0430\u0442\u0446\u0435 \u0432\u0441\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e88\/b31\/386\/e88b31386f5eeb69fd87b29dabec74b0.png\" width=\"749\" height=\"596\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e88\/b31\/386\/e88b31386f5eeb69fd87b29dabec74b0.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u041e\u043d \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u043e\u0440\u0442 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u043e\u0433\u043e Gateway, \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u043c\u044f \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u0438 \u0434\u0430\u043b\u0435\u0435 \u0438\u0434\u0443\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 end-\u043f\u043e\u0438\u043d\u0442\u044b \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443: localhost:8888\/microserviceName\/users.<\/p>\n<h3>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443!<\/h3>\n<p>\u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0431\u0435\u0436\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0443 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438, \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0432 \u0431\u0430\u0437\u0435 \u0438 \u0432\u044b\u0434\u0430\u0447\u0438 JWT \u0442\u043e\u043a\u0435\u043d\u043e\u0432. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0430\u044f Person entity, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f Id, username, password, role.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437 UserService:<\/p>\n<pre><code class=\"java\">public AuthResponse createPerson(PersonDto dto) {         Person personEntity = mapper.dtoToPerson(dto);         personEntity.setRole(Role.USER);         personEntity.setPassword(BCrypt.hashpw(dto.getPassword(), BCrypt.gensalt()));          repository.save(personEntity);         return getAuthResponse(personEntity);     }  private AuthResponse getAuthResponse(Person personEntity) {         String accessToken = jwt.generate(personEntity, accessType);         String refreshToken = jwt.generate(personEntity, refreshType);         return new AuthResponse(accessToken, refreshToken, personEntity.getMRID());     }<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 \u21164 \u043c\u044b \u0445\u044d\u0448\u0438\u043c \u043f\u0430\u0440\u043e\u043b\u044c \u0438 \u0445\u0440\u0430\u043d\u0438\u043c \u0435\u0433\u043e \u0432 \u0411\u0414 \u0432 \u0437\u0430\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435. \u041d\u0430 \u0442\u0435\u043c\u0443 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 JWT \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u0430\u0445 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u0441\u0442\u0430\u0442\u0435\u0439 \u0438 \u0432\u0438\u0434\u0435\u043e. \u0414\u0430\u043d\u043d\u0430\u044f \u0436\u0435 \u0441\u0442\u0430\u0442\u044c\u044f \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u0432\u043e\u0435\u043c \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 Security \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p><strong>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443. Api Gateway! \u041d\u0430 \u043d\u0435\u043c \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043f\u043e\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/strong><\/p>\n<p>\u0422\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"java\">    implementation 'org.springframework.boot:spring-boot-starter-security:2.6.8'     implementation 'org.springframework.boot:spring-boot-starter-webflux:2.6.8'     implementation 'org.springframework:spring-webmvc:5.3.22'      implementation 'io.jsonwebtoken:jjwt-api:0.11.2'     runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'     runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.1'<\/code><\/pre>\n<p>Security config:<\/p>\n<pre><code class=\"java\">@EnableWebFluxSecurity @RequiredArgsConstructor public class SecurityConfig {      private final AuthenticationManager authenticationManager;     private final SecurityContextRepository securityContextRepository;      @Bean     public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {         http                 .csrf()                 .disable()                 .authenticationManager(authenticationManager)                 .securityContextRepository(securityContextRepository)                 .authorizeExchange()                 .pathMatchers(\"\/microservice1\/users\").permitAll()                 .pathMatchers(\"\/microservice2\/emails\").authenticated()                 .pathMatchers(\"\/microservice3\/persons\").hasAuthority(\"ADMIN\")                 .anyExchange()                 .permitAll()                 .and()                 .httpBasic()                 .disable()                 .formLogin()                 .disable();         return http.build();     } }<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435, \u043c\u044b \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043d\u0435<a class=\"mention\" href=\"\/users\/enablewebsecurity,\">@EnableWebSecurity,<\/a>\u0430 <a class=\"mention\" href=\"\/users\/EnableWebFluxSecurity\">@EnableWebFluxSecurity<\/a>. \u0414\u0430\u043d\u043d\u0430\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430, \u043e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c Security \u0432 Gateway, \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0431\u0435\u0433\u0430\u044f \u043f\u043e \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c. <\/p>\n<p>\u041a\u0430\u043a \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 WebSecurityConfigurerAdapter &#8212; deprecated. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c SecurityWebFilterChain \u0438 \u043e\u043f\u0438\u0448\u0435\u043c \u0432 \u043d\u0435\u043c \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b. <\/p>\n<p>\u0412 5,6 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u0432\u0430\u0436\u043d\u044b\u0435 \u0448\u0442\u0443\u043a\u0438, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e: authenticationManager, securityContextRepository. <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c SecurityContextRepository:<\/p>\n<pre><code class=\"java\">@Component @RequiredArgsConstructor public class SecurityContextRepository implements ServerSecurityContextRepository {      private final AuthenticationManager authenticationManager;      @Override     public Mono&lt;Void> save(ServerWebExchange swe, SecurityContext sc) {         throw new UnsupportedOperationException(\"Not supported yet.\");     }      @Override     public Mono&lt;SecurityContext> load(ServerWebExchange swe) {         Mono&lt;String> stringMono = Mono.justOrEmpty(swe.getRequest().getHeaders().getFirst(HttpHeaders.AUTHORIZATION));         return stringMono.flatMap(this::getSecurityContext);     }      private Mono&lt;? extends SecurityContext> getSecurityContext(String token) {         Authentication auth = new UsernamePasswordAuthenticationToken(token, token);         return authenticationManager.authenticate(auth).map(SecurityContextImpl::new);     } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u043a\u0440\u0430\u0442\u0446\u0435 \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, \u0442\u043e \u043c\u044b \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u0438\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 Authorizarion header \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432 \u043c\u0435\u0442\u043e\u0434 authenticate \u0438\u0437 AuthenticationManager.<\/p>\n<p>\u0410 \u0432\u043e\u0442 \u0438 AuthenticationManager:<\/p>\n<pre><code class=\"java\">@Lazy @Component @RequiredArgsConstructor public class AuthenticationManager implements ReactiveAuthenticationManager {      private final Builder webClient;      @Override     public Mono&lt;Authentication> authenticate(Authentication authentication) {         String jwtToken = authentication.getCredentials().toString();         return tokenValidate(jwtToken)                 .bodyToMono(UserAuthorities.class)                 .map(this::getAuthorities);     }      private UsernamePasswordAuthenticationToken getAuthorities(UserAuthorities userAuthorities) {         return new UsernamePasswordAuthenticationToken(                 userAuthorities.getUsername(), null,                 userAuthorities.getAuthorities().stream()                         .map(SimpleGrantedAuthority::new)                         .collect(Collectors.toList()));     }      private ResponseSpec tokenValidate(String token) {         return webClient.build()                 .get()                 .uri(uriBuilder -> uriBuilder.host(\"registration\").path(\"\/token\/auth\").queryParam(\"token\", token).build())                 .retrieve()                 .onStatus(HttpStatus.FORBIDDEN::equals, response -> Mono.error(new IllegalStateException(\"Token is not valid\")));     } }<\/code><\/pre>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u0435 tokenValidate \u043c\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u0432 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441 registration, \u0432 endpoint token\/auth. \u0412 \u043d\u0435\u043c \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 JWT \u0442\u043e\u043a\u0435\u043d\u0430. \u0412 \u043d\u0435\u043c \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 claims \u0438\u0437 JWT \u0442\u043e\u043a\u0435\u043d\u0430 \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 DTO. \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u044d\u0442\u043e, \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e, \u0442\u0430\u043a:<\/p>\n<pre><code class=\"java\">public UserAuthorizationInfo getUserInfoFromToken(String token) {         \/\/ \u0437\u0434\u0435\u0441\u044c \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0432\u0430\u0448\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430          Claims allClaimsFromToken = jwt.getAllClaimsFromToken(token);         UserAuthorizationInfo userInfo = new UserAuthorizationInfo();          userInfo.setPersonId(allClaimsFromToken.get(\"id\").toString());         userInfo.setUsername(allClaimsFromToken.getSubject());          List&lt;String> authorities = new ArrayList&lt;>();         authorities.add(allClaimsFromToken.get(ROLES).toString());         userInfo.setAuthorities(authorities);         return userInfo;     }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c UserAuthorities, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0443\u044e username \u0438 Collection&lt;String> authorities. \u0418 \u043f\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u0437 header-\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f username \u0438 \u0440\u043e\u043b\u044c. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 endpoint-\u044b \u043a\u043e\u043c\u0443 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432 Security Config \u0438\u0437 Api Gateway \u0438 \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u0438 \u043a\u0441\u0442\u0430\u0442\u0438, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e, \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u0449\u0438\u043d\u0430 \u0432\u0435\u0434\u044c:)<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/697098\/\"> https:\/\/habr.com\/ru\/post\/697098\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>Spring Security &#8212; \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043a\u0440\u0443\u0442\u0430\u044f \u0448\u0442\u0443\u043a\u0430, \u043d\u0430 \u0442\u0435\u043c\u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043d\u043e\u0433\u043e \u0433\u0430\u0439\u0434\u043e\u0432, \u0441\u0442\u0430\u0442\u0435\u0439 \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430\u0445. \u041d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u044d\u0442\u0438\u0445 \u0432\u0438\u0434\u0435\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u043e\u043d\u043e\u043b\u0438\u0442\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0439. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0441\u0432\u043e\u0435\u043c \u043b\u0438\u0447\u043d\u043e\u043c \u043e\u043f\u044b\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0435\u0435 \u0434\u043b\u044f \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u044d\u0442\u043e \u043d\u0435 \u0441\u0442\u0430\u0442\u044c\u044f \u0443\u0440\u043e\u0432\u043d\u044f \u0422\u0430\u0433\u0438\u0440\u0430 \u0412\u0430\u043b\u0435\u0435\u0432\u0430. \u042d\u0442\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u0438\u0447\u043d\u044b\u0439 \u043e\u043f\u044b\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f, \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c, \u043a\u043e\u043c\u0443 \u0442\u043e \u043e\u043d \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c.<\/p>\n<p><strong>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0438 \u0432\u044b\u0434\u0430\u0447\u0438 JWT \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c (\u043a\u0440\u0430\u0442\u043a\u043e)<\/p>\n<\/li>\n<li>\n<p>\u041c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 (\u043a\u0440\u0430\u0442\u043a\u043e)<\/p>\n<\/li>\n<li>\n<p>Security \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0440\u043e\u043b\u0435\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438:<\/strong><\/p>\n<ul>\n<li>\n<p>Spring Boot<\/p>\n<\/li>\n<li>\n<p>Spring Cloud<\/p>\n<\/li>\n<li>\n<p>Spring Security<\/p>\n<\/li>\n<li>\n<p>JWT<\/p>\n<\/li>\n<li>\n<p>WebFlux<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u0435\u0445\u0430\u043d\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0434\u0443\u043c\u0430\u044e, \u043c\u043d\u043e\u0433\u0438\u043c \u043f\u043e\u043d\u044f\u0442\u043d\u0430. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 \u043d\u0438\u0436\u0435 \u0432\u043a\u0440\u0430\u0442\u0446\u0435 \u0432\u0441\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u041e\u043d \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u043e\u0440\u0442 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u043e\u0433\u043e Gateway, \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u043c\u044f \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u0438 \u0434\u0430\u043b\u0435\u0435 \u0438\u0434\u0443\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 end-\u043f\u043e\u0438\u043d\u0442\u044b \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443: localhost:8888\/microserviceName\/users.<\/p>\n<h3>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443!<\/h3>\n<p>\u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0431\u0435\u0436\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0443 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438, \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0432 \u0431\u0430\u0437\u0435 \u0438 \u0432\u044b\u0434\u0430\u0447\u0438 JWT \u0442\u043e\u043a\u0435\u043d\u043e\u0432. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0430\u044f Person entity, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f Id, username, password, role.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437 UserService:<\/p>\n<pre><code class=\"java\">public AuthResponse createPerson(PersonDto dto) {         Person personEntity = mapper.dtoToPerson(dto);         personEntity.setRole(Role.USER);         personEntity.setPassword(BCrypt.hashpw(dto.getPassword(), BCrypt.gensalt()));          repository.save(personEntity);         return getAuthResponse(personEntity);     }  private AuthResponse getAuthResponse(Person personEntity) {         String accessToken = jwt.generate(personEntity, accessType);         String refreshToken = jwt.generate(personEntity, refreshType);         return new AuthResponse(accessToken, refreshToken, personEntity.getMRID());     }<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 \u21164 \u043c\u044b \u0445\u044d\u0448\u0438\u043c \u043f\u0430\u0440\u043e\u043b\u044c \u0438 \u0445\u0440\u0430\u043d\u0438\u043c \u0435\u0433\u043e \u0432 \u0411\u0414 \u0432 \u0437\u0430\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435. \u041d\u0430 \u0442\u0435\u043c\u0443 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 JWT \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u0430\u0445 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u0441\u0442\u0430\u0442\u0435\u0439 \u0438 \u0432\u0438\u0434\u0435\u043e. \u0414\u0430\u043d\u043d\u0430\u044f \u0436\u0435 \u0441\u0442\u0430\u0442\u044c\u044f \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u0432\u043e\u0435\u043c \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 Security \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p><strong>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443. Api Gateway! \u041d\u0430 \u043d\u0435\u043c \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043f\u043e\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/strong><\/p>\n<p>\u0422\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"java\">    implementation 'org.springframework.boot:spring-boot-starter-security:2.6.8'     implementation 'org.springframework.boot:spring-boot-starter-webflux:2.6.8'     implementation 'org.springframework:spring-webmvc:5.3.22'      implementation 'io.jsonwebtoken:jjwt-api:0.11.2'     runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'     runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.1'<\/code><\/pre>\n<p>Security config:<\/p>\n<pre><code class=\"java\">@EnableWebFluxSecurity @RequiredArgsConstructor public class SecurityConfig {      private final AuthenticationManager authenticationManager;     private final SecurityContextRepository securityContextRepository;      @Bean     public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {         http                 .csrf()                 .disable()                 .authenticationManager(authenticationManager)                 .securityContextRepository(securityContextRepository)                 .authorizeExchange()                 .pathMatchers(\"\/microservice1\/users\").permitAll()                 .pathMatchers(\"\/microservice2\/emails\").authenticated()                 .pathMatchers(\"\/microservice3\/persons\").hasAuthority(\"ADMIN\")                 .anyExchange()                 .permitAll()                 .and()                 .httpBasic()                 .disable()                 .formLogin()                 .disable();         return http.build();     } }<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435, \u043c\u044b \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043d\u0435<a class=\"mention\" href=\"\/users\/enablewebsecurity,\">@EnableWebSecurity,<\/a>\u0430 <a class=\"mention\" href=\"\/users\/EnableWebFluxSecurity\">@EnableWebFluxSecurity<\/a>. \u0414\u0430\u043d\u043d\u0430\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430, \u043e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c Security \u0432 Gateway, \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0431\u0435\u0433\u0430\u044f \u043f\u043e \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c. <\/p>\n<p>\u041a\u0430\u043a \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 WebSecurityConfigurerAdapter &#8212; deprecated. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c SecurityWebFilterChain \u0438 \u043e\u043f\u0438\u0448\u0435\u043c \u0432 \u043d\u0435\u043c \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b. <\/p>\n<p>\u0412 5,6 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u0432\u0430\u0436\u043d\u044b\u0435 \u0448\u0442\u0443\u043a\u0438, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e: authenticationManager, securityContextRepository. <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c SecurityContextRepository:<\/p>\n<pre><code class=\"java\">@Component @RequiredArgsConstructor public class SecurityContextRepository implements ServerSecurityContextRepository {      private final AuthenticationManager authenticationManager;      @Override     public Mono&lt;Void> save(ServerWebExchange swe, SecurityContext sc) {         throw new UnsupportedOperationException(\"Not supported yet.\");     }      @Override     public Mono&lt;SecurityContext> load(ServerWebExchange swe) {         Mono&lt;String> stringMono = Mono.justOrEmpty(swe.getRequest().getHeaders().getFirst(HttpHeaders.AUTHORIZATION));         return stringMono.flatMap(this::getSecurityContext);     }      private Mono&lt;? extends SecurityContext> getSecurityContext(String token) {         Authentication auth = new UsernamePasswordAuthenticationToken(token, token);         return authenticationManager.authenticate(auth).map(SecurityContextImpl::new);     } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u043a\u0440\u0430\u0442\u0446\u0435 \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, \u0442\u043e \u043c\u044b \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u0438\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 Authorizarion header \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432 \u043c\u0435\u0442\u043e\u0434 authenticate \u0438\u0437 AuthenticationManager.<\/p>\n<p>\u0410 \u0432\u043e\u0442 \u0438 AuthenticationManager:<\/p>\n<pre><code class=\"java\">@Lazy @Component @RequiredArgsConstructor public class AuthenticationManager implements ReactiveAuthenticationManager {      private final Builder webClient;      @Override     public Mono&lt;Authentication> authenticate(Authentication authentication) {         String jwtToken = authentication.getCredentials().toString();         return tokenValidate(jwtToken)                 .bodyToMono(UserAuthorities.class)                 .map(this::getAuthorities);     }      private UsernamePasswordAuthenticationToken getAuthorities(UserAuthorities userAuthorities) {         return new UsernamePasswordAuthenticationToken(                 userAuthorities.getUsername(), null,                 userAuthorities.getAuthorities().stream()                         .map(SimpleGrantedAuthority::new)                         .collect(Collectors.toList()));     }      private ResponseSpec tokenValidate(String token) {         return webClient.build()                 .get()                 .uri(uriBuilder -> uriBuilder.host(\"registration\").path(\"\/token\/auth\").queryParam(\"token\", token).build())                 .retrieve()                 .onStatus(HttpStatus.FORBIDDEN::equals, response -> Mono.error(new IllegalStateException(\"Token is not valid\")));     } }<\/code><\/pre>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u0435 tokenValidate \u043c\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u0432 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441 registration, \u0432 endpoint token\/auth. \u0412 \u043d\u0435\u043c \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 JWT \u0442\u043e\u043a\u0435\u043d\u0430. \u0412 \u043d\u0435\u043c \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 claims \u0438\u0437 JWT \u0442\u043e\u043a\u0435\u043d\u0430 \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 DTO. \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u044d\u0442\u043e, \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e, \u0442\u0430\u043a:<\/p>\n<pre><code class=\"java\">public UserAuthorizationInfo getUserInfoFromToken(String token) {         \/\/ \u0437\u0434\u0435\u0441\u044c \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0432\u0430\u0448\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430          Claims allClaimsFromToken = jwt.getAllClaimsFromToken(token);         UserAuthorizationInfo userInfo = new UserAuthorizationInfo();          userInfo.setPersonId(allClaimsFromToken.get(\"id\").toString());         userInfo.setUsername(allClaimsFromToken.getSubject());          List&lt;String> authorities = new ArrayList&lt;>();         authorities.add(allClaimsFromToken.get(ROLES).toString());         userInfo.setAuthorities(authorities);         return userInfo;     }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c UserAuthorities, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0443\u044e username \u0438 Collection&lt;String> authorities. \u0418 \u043f\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u0437 header-\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f username \u0438 \u0440\u043e\u043b\u044c. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 endpoint-\u044b \u043a\u043e\u043c\u0443 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432 Security Config \u0438\u0437 Api Gateway \u0438 \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u0438 \u043a\u0441\u0442\u0430\u0442\u0438, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e, \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u0449\u0438\u043d\u0430 \u0432\u0435\u0434\u044c:)<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/697098\/\"> https:\/\/habr.com\/ru\/post\/697098\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-340651","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/340651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=340651"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/340651\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=340651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=340651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=340651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}