Конфигурация Spring Security:
Код: Выделить всё
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(AbstractHttpConfigurer::disable)
.cors(Customizer.withDefaults())
.authorizeHttpRequests(auth ->
auth.requestMatchers("/api/v1/auth/authenticate",
"/actuator/**",
"/api/v1/service-point/{idOrCode}",
"/api/v1/service-point/parent/{servicePointId}")
.permitAll()
.requestMatchers(AUTH_WHITELIST)
.permitAll()
.requestMatchers("/api/v1/auth/change/admindata").hasRole(RoleEnum.BURNS.name())
.requestMatchers("/api/v1/auth/register").hasRole(RoleEnum.ADMIN.name())
.requestMatchers("/api/v1/auth/change/password")
.hasAnyRole(RoleEnum.ADMIN.name())
.requestMatchers(HttpMethod.POST).hasRole(RoleEnum.ADMIN.name())
.requestMatchers(HttpMethod.DELETE).hasRole(RoleEnum.ADMIN.name())
.requestMatchers(HttpMethod.PUT).hasRole(RoleEnum.ADMIN.name())
.requestMatchers(HttpMethod.PATCH).hasRole(RoleEnum.ADMIN.name())
.anyRequest()
.authenticated())
.sessionManagement(ssmng -> ssmng.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authenticationProvider(authProvider)
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
Код: Выделить всё
@Override
protected void doFilterInternal(
@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull FilterChain filterChain
) throws ServletException, IOException {
final String authHeader = request.getHeader("Authorization");
final String jwt;
final String userName;
if (StringUtils.isBlank(authHeader) || !authHeader.startsWith("Bearer ")) {
filterChain.doFilter(request, response);
return;
}
...
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... thvariable
Мобильная версия