После добавления проки получаю 403 ошибку Forbidden Angular/Spring
Итак, у меня есть сервер на Spring, вот его контроллер
@RestController
@RequestMapping("/auth")
@CrossOrigin(
origins = "*",
allowedHeaders = {"Access-Control-Allow-Origin"},
methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS}
)
public class AuthenticationController {
private final UserService userService;
private final AuthenticationService authenticationService;
public AuthenticationController(UserService userService, AuthenticationService authenticationService) {
this.userService = userService;
this.authenticationService = authenticationService;
}
@PostMapping()
public ResponseEntity<AuthenticationResponseDto> login(@RequestBody AuthenticationRequestDto requestDto) {
String username = requestDto.getUsername();
String token = authenticationService.login(requestDto);
User user = userService.findByUsername(username);
return ResponseEntity.ok(new AuthenticationResponseDto(username, token, RolesToStringConverter.convert(user.getRoles())));
}
}
Теперь мне нужно авторизовать пользователя на клиенте(использую Angular), и для этого я для начала сделал прокси:
{
"/api/*": {
"target": "http://localhost:8080",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
И далее мой сервис для аутентификации:
@Injectable({
providedIn: 'root'
})
export class AuthService {
private _isLoggedIn$ = new BehaviorSubject<boolean>(false)
isLoggedIn$ = this._isLoggedIn$.asObservable()
constructor(private http: HttpClient) {
const token = localStorage.getItem('token')
this._isLoggedIn$.next(!!token)
}
logIn(login: string, password: string){
const headers = new HttpHeaders()
.set("Access-Control-Allow-Origin", "*")
const options = {headers}
return this.http.post("/api/auth", {
username: login,
password: password
}, options).pipe(
tap((response: any) => {
this._isLoggedIn$.next(true)
localStorage.setItem('token', response.token)
localStorage.setItem('username', response.username)
localStorage.setItem('roles', response.roles.join(' '))
})
)
}
}
Итак, дело в том, что пока я не использовал прокси, метод работал и посылал запрос на сервер и получал нужный ответ, после добавления прокси я получаю 403 отказано.