0

I VERY NEED HELP. So, I have a server on Spring, here is its controller:

@RestController
@RequestMapping("/auth")
@CrossOrigin(
        origins = "*"
)
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())));
    }
}

Now I need to authorize the user on the client (I use Angular), and for this I first made a proxy:

{
  "/api/*": {
    "target": "http://localhost:8080",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true
  }
}

And then my service for authentication:

@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){
    return this.http.post("/api/auth", JSON.stringify({
      username: login,
      password: password
    })).pipe(
      tap((response: any) => {
        this._isLoggedIn$.next(true)
        localStorage.setItem('token', response.token)
        localStorage.setItem('username', response.username)
        localStorage.setItem('roles', response.roles.join(' '))
      })
    )
  }
}

So, the point is that while I did not use a proxy, the method worked and sent a request to the server and received the desired response, after adding the proxy, I get 403 forbidden. I found question like that but cant undertand what i have to to: CLI CORS Proxy not changing origin, still get 403 on API reqs? . I shared project on GitHub. Angular: https://github.com/MatveyAndrosyukk/labinvent-task. Spring: https://github.com/MatveyAndrosyukk/labinvent-task-backend.

Matisiuk
  • 23
  • 2

0 Answers0