Spring + Vue + OAuth2 – CORS Missing Allow Origin

The Backend runs on localhost:8088
The Frontend runs on localhost:8000

When making the request to login, the frontend sends a POST to the specific backend API

return axios.post('/oauth2/authorization/github');

This returns a redirect URL to github.com/login/oauth/authorize/, which is correct. I was able to confirm this by using the provided URL manually.

The browser now sends an OPTIONS request to the /authorize, but then yields “CORS Missing Allow Origin” as “Transferred” and thereby blocks the request.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://github.com/login/oauth/authorize?response_type=code&client_id=XXX&scope=read:user&state=XXX%3D&redirect_uri=http://localhost:8088/login/oauth2/code/github. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 404.

Indeed, the Allow Origin is missing from the requests getting blocked.
Is there something I am missing?

First POST Request (passes)
CORS Missing Allow Origin

What I already tried:

I have already defined a CORS Confiuration in the backend, as well as default Access-Control-Allow-Origin headers for the frontend sending the first request.

@Bean
public CorsConfigurationSource corsConfigurationSource() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration corsConfiguration = new CorsConfiguration().applyPermitDefaultValues();
    corsConfiguration.addAllowedOrigin("http://localhost:8000");
    corsConfiguration.addAllowedOrigin("GitHub.com");
    corsConfiguration.setAllowedMethods(Arrays.asList("POST", "GET", "PUT", "PATCH", "DELETE", "OPTIONS"));
    corsConfiguration.setAllowCredentials(true);
    source.registerCorsConfiguration("/**", corsConfiguration);
    return source;
}

I have also found out that manually resending the first POST request also appears to work, which is weird, considering I am not editing it in any way. Possibly due to missing “strict-origin-when-cross-origin” as Referrer Policy?

New contributor

Quadrum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

This is a OAuth2 authorization endpoint and you’re not supposed to make fetch() requests to it. You need to redirect the user to this URL, after which they will get redirected back to your app.

3

What fails is a preflight request to Github. Adjusting the CORS configuration in your Spring application can’t have an impact.

You want to redirect the user to the authorization server, not really to send a cross-origin request from your Vue frontend. What I do in this situation, is change the origin by setting the windows.location.href. But as this is done in single-page code, I have to change the status of the redirection from 302 to something in the 2xx range for the Typescript code to be able to intercept it. I wrote a Spring Boot starter to configure from just application properties this response status (among many other things).

Also note that as you’re using oauth2Login, requests from your Vue app are authorized with session cookies (not Bearer tokens). This complies with current recommendations but implies two things:

  • protection against CSRF (cookie based in the case of a single page or mobile app)
  • the single-page and Spring backend must be served with the same origin (session cookie is flagged with same-site) => what you need for REST requests from your Vue app to Spring backend is a reverse proxy, not Spring CORS configuration.

I also wrote a Baeldung article with a complete solution: Vue, React and Angular frontends, Spring Cloud Gateway configured with the TokenRelay filter (bridge between session and Bearer based authorizations). and Spring REST APIs configured as resource server.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Spring + Vue + OAuth2 – CORS Missing Allow Origin

The Backend runs on localhost:8088
The Frontend runs on localhost:8000

When making the request to login, the frontend sends a POST to the specific backend API

return axios.post('/oauth2/authorization/github');

This returns a redirect URL to github.com/login/oauth/authorize/, which is correct. I was able to confirm this by using the provided URL manually.

The browser now sends an OPTIONS request to the /authorize, but then yields “CORS Missing Allow Origin” as “Transferred” and thereby blocks the request.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://github.com/login/oauth/authorize?response_type=code&client_id=XXX&scope=read:user&state=XXX%3D&redirect_uri=http://localhost:8088/login/oauth2/code/github. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 404.

Indeed, the Allow Origin is missing from the requests getting blocked.
Is there something I am missing?

First POST Request (passes)
CORS Missing Allow Origin

What I already tried:

I have already defined a CORS Confiuration in the backend, as well as default Access-Control-Allow-Origin headers for the frontend sending the first request.

@Bean
public CorsConfigurationSource corsConfigurationSource() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration corsConfiguration = new CorsConfiguration().applyPermitDefaultValues();
    corsConfiguration.addAllowedOrigin("http://localhost:8000");
    corsConfiguration.addAllowedOrigin("GitHub.com");
    corsConfiguration.setAllowedMethods(Arrays.asList("POST", "GET", "PUT", "PATCH", "DELETE", "OPTIONS"));
    corsConfiguration.setAllowCredentials(true);
    source.registerCorsConfiguration("/**", corsConfiguration);
    return source;
}

I have also found out that manually resending the first POST request also appears to work, which is weird, considering I am not editing it in any way. Possibly due to missing “strict-origin-when-cross-origin” as Referrer Policy?

New contributor

Quadrum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

This is a OAuth2 authorization endpoint and you’re not supposed to make fetch() requests to it. You need to redirect the user to this URL, after which they will get redirected back to your app.

3

What fails is a preflight request to Github. Adjusting the CORS configuration in your Spring application can’t have an impact.

You want to redirect the user to the authorization server, not really to send a cross-origin request from your Vue frontend. What I do in this situation, is change the origin by setting the windows.location.href. But as this is done in single-page code, I have to change the status of the redirection from 302 to something in the 2xx range for the Typescript code to be able to intercept it. I wrote a Spring Boot starter to configure from just application properties this response status (among many other things).

Also note that as you’re using oauth2Login, requests from your Vue app are authorized with session cookies (not Bearer tokens). This complies with current recommendations but implies two things:

  • protection against CSRF (cookie based in the case of a single page or mobile app)
  • the single-page and Spring backend must be served with the same origin (session cookie is flagged with same-site) => what you need for REST requests from your Vue app to Spring backend is a reverse proxy, not Spring CORS configuration.

I also wrote a Baeldung article with a complete solution: Vue, React and Angular frontends, Spring Cloud Gateway configured with the TokenRelay filter (bridge between session and Bearer based authorizations). and Spring REST APIs configured as resource server.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật