jhipster / generator-jhipster-ionic

Ionic for JHipster ✨
https://developer.okta.com/blog/2022/05/12/ionic-angular-jhipster
Apache License 2.0
192 stars 49 forks source link

CORS Error #872

Closed manisi closed 1 year ago

manisi commented 1 year ago

Hello Matt, using jhipster 8 beta gateway app run on docker and generate ionic app with generate-jhppster-ionic(jhipster 7.9.3) and set cors origin in application.yaml (http://localhost:8100) but this error exists before and after login . but signup and authenticate is ok . for /api/account ::: [Error] XMLHttpRequest cannot load http://localhost:8080/api/account due to access control checks. [Error] Failed to load resource: Origin http://localhost:8100 is not allowed by Access-Control-Allow-Origin. Status code: 401 (account, line 0)

thanks alot

mraible commented 1 year ago

Are you sure you added CORS information in the correct part of application.yml? You might look at the application-dev.yml template to see where it should be.

manisi commented 1 year ago

Yes, please pay attention to this screenshot

Screen Shot 2023-07-03 at 21 28 26 Screen Shot 2023-07-03 at 21 35 04
mraible commented 1 year ago

I believe I found the issue. Can you try adding .cors(withDefaults()) to your SecurityConfiguration class. Like this:

public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
        .cors(withDefaults())

This solves the problem for me. If it does for you, too, I'll create a PR to update JHipster.

manisi commented 1 year ago

Looks like the problem still exists.

Screen Shot 2023-07-06 at 08 46 08 Screen Shot 2023-07-06 at 08 54 05
mraible commented 1 year ago

Can you please share the output from jhipster info in your backend project? I did not test with WebFlux.

manisi commented 1 year ago

yes of course.

Welcome to JHipster v8.0.0-beta.1

Welcome to the JHipster Information Sub-Generator

bahilla-gateway@0.0.1-SNAPSHOT /Users/developer/Desktop/bahillaApp/gateway
└── generator-jhipster@8.0.0-beta.1
JHipster configuration, a .yo-rc.json file generated in the root folder
.yo-rc.json file
{
  "applicationType": "gateway",
  "authenticationType": "jwt",
  "baseName": "bahillaGateway",
  "buildTool": "maven",
  "cacheProvider": "hazelcast",
  "clientFramework": "angular",
  "clientTheme": "none",
  "creationTimestamp": 1687544869292,
  "databaseType": "sql",
  "devDatabaseType": "mysql",
  "devServerPort": 4200,
  "dtoSuffix": "DTO",
  "enableGradleEnterprise": null,
  "enableHibernateCache": true,
  "enableSwaggerCodegen": false,
  "enableTranslation": true,
  "entities": [],
  "entitySuffix": "",
  "gradleEnterpriseHost": null,
  "jhiPrefix": "jhi",
  "jhipsterVersion": "8.0.0-beta.1",
  "languages": [
    "fa",
    "en",
    "fr"
  ],
  "messageBroker": false,
  "microfrontend": false,
  "microfrontends": [],
  "nativeLanguage": "fa",
  "packageFolder": "ir/bahilla/gateway",
  "packageName": "ir.bahilla.gateway",
  "pages": [],
  "prodDatabaseType": "mysql",
  "reactive": true,
  "searchEngine": false,
  "serverPort": 8080,
  "serverSideOptions": [],
  "serviceDiscoveryType": "consul",
  "skipCheckLengthOfIdentifier": false,
  "skipClient": false,
  "skipFakeData": false,
  "skipUserManagement": false,
  "testFrameworks": [],
  "websocket": false,
  "withAdminUi": true
}
Environment and Tools

java version "17.0.7" 2023-04-18 LTS Java(TM) SE Runtime Environment (build 17.0.7+8-LTS-224) Java HotSpot(TM) 64-Bit Server VM (build 17.0.7+8-LTS-224, mixed mode, sharing)

git version 2.20.1

node: v18.16.1 npm: 9.7.2

mraible commented 1 year ago

@mshima If I copy the .yo-rc.json file to an empty directory and run jhipster, it doesn't create the project for me. Instead, it prompts me for the baseName. This is new behavior with v8. Is it expected?

Screenshot 2023-07-06 at 8 57 44 AM
manisi commented 1 year ago

.yo-rc.json

manisi commented 1 year ago
Screen Shot 2023-07-06 at 18 40 43
manisi commented 1 year ago

create empty folder and paste yo-rc.json in it and jhipster generate gateway2 thats ok.

mraible commented 1 year ago

I was able to fix it by changing from CorsWebFilter to CorsConfigurationSource. Can you try this:

@Bean
public CorsConfigurationSource corsConfigurationSource() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = jHipsterProperties.getCors();
    if (!CollectionUtils.isEmpty(config.getAllowedOrigins()) || !CollectionUtils.isEmpty(config.getAllowedOriginPatterns())) {
        log.debug("Registering CORS filter");
        source.registerCorsConfiguration("/api/**", config);
        source.registerCorsConfiguration("/management/**", config);
        source.registerCorsConfiguration("/v3/api-docs", config);
        source.registerCorsConfiguration("/swagger-ui/**", config);
        source.registerCorsConfiguration("/*/api/**", config);
        source.registerCorsConfiguration("/services/*/api/**", config);
        source.registerCorsConfiguration("/*/management/**", config);
    }
    return source;
}

Note that I do get the following error after login because fa is not in the Ionic blueprint's languages.

GET http://localhost:8100/assets/i18n/fa.json 404 (Not Found)

You can see the list of languages at https://github.com/jhipster/generator-jhipster-ionic/tree/main/generators/ionic/resources/base/src/assets/i18n.

manisi commented 1 year ago

fixed . thank to you. @mraible