Загрузил JAR-файл моего приложения Spring Boot.
UserController:
Код: Выделить всё
@RestController
@RequestMapping("/api")
public class UserController {
...
@PostMapping("/login")
public LoginResponse loginUser(@RequestBody UserDto userDto) {
return userService.loginUser(userDto);
}
Код: Выделить всё
@Configuration
@EnableWebSecurity
public class SecurityConfig {
...
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.cors(withDefaults())
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(request -> request
.requestMatchers(POST, "/api/register").permitAll()
.requestMatchers(GET, "/api/register/confirm**").permitAll()
.requestMatchers(POST, "/api/login").permitAll()
.anyRequest().authenticated())
.sessionManagement(session -> session.sessionCreationPolicy(STATELESS))
.authenticationProvider(authenticationConfig.authenticationProvider())
.addFilterBefore(jsonWebTokenFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.addAllowedOrigin("*");
configuration.setAllowedHeaders(Arrays.asList("Authorization", "Content-Type", "Accept"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
Код: Выделить всё
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"POST",
"GET",
"PUT",
"DELETE"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": []
}
]
http://mibook.s3-website.eu-north-1.ama ... ginAngular, UserService:
Код: Выделить всё
export class UserService {
private baseUrl = 'http://mybooks.eu-north-1.elasticbeanstalk.com/api';
...
login(user: any): Observable {
return this.httpClient.post(`${this.baseUrl}/login`, user);
}
Я пробовал @CrossOrigin в классе контроллера.
Подробнее здесь: https://stackoverflow.com/questions/786 ... ngular-app