Keycloak with Angular and Spring error: GET http://localhost:8180/auth/realms/Storage/protocol/openid-connect/3p-cookies/step1.html 404 (Not Found)

Viewed 14035

I'm trying to integrate Keycloak with Spring Boot and Angular.

Security works correctly. When I'm trying to access secured endpoint manually through browser localhost:8080/products/1 (products is entity in my database), then I'm redirected to Keycloak login client, after logging in I have access to the endpoint.

I've been folowing this tutorial.

Problem occurs when I'm trying to access localhost:4200 (Angular app main page), in browser console I receive this error: SCREENSHOT

keycloak.js:1305 GET http://localhost:8180/auth/realms/Storage/protocol/openid-connect/3p-cookies/step1.html 404 (Not Found)

Spring configuration file: application-local.yml

keycloak:
  auth-server-url: 'http://localhost:8180/auth'
  realm: 'Storage'
  resource: 'storage-app-client'
  public-client: true
  principal-attribute: test123

Rest of the configuration files is taken from tutorial mentioned above.

Keycloak realm: Security defenses

Keycloak initialization in Angular:

@Injectable({
  providedIn: 'root'
})
export class KeycloakService
{
  private keycloakAuth: KeycloakInstance;

  constructor()
  {
  }

  init(): Promise<any>
  {
    return new Promise((resolve, reject) =>
    {
      const config = {
        'url': 'http://localhost:8180/auth',
        'realm': 'Storage',
        'clientId': 'storage-app-client'
      };
      // @ts-ignore
      this.keycloakAuth = new Keycloak(config);
      this.keycloakAuth.init({onLoad: 'login-required'})
        .success(() =>
        {
          resolve();
        })
        .error(() =>
        {
          reject();
        });
    });
  }

  getToken(): string
  {
    return this.keycloakAuth.token;
  }

  logout()
  {
    const options = {
      'redirectUri': 'http://localhost:4200',
      'realm': 'Storage',
      'clientId': 'storage-app-client'
    };
    this.keycloakAuth.logout(options);
  }
}

app.module.ts

import {BrowserModule} from '@angular/platform-browser';
import {APP_INITIALIZER, NgModule} from '@angular/core';

import {AppComponent} from './app.component';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {HTTP_INTERCEPTORS, HttpClient, HttpClientModule} from "@angular/common/http";
import {MatSliderModule} from '@angular/material/slider';
import {MatToolbarModule} from '@angular/material/toolbar';
import {MatIconModule} from '@angular/material/icon';
import {RouterModule} from '@angular/router';
import {NavbarComponent} from './components/navbar/navbar.component';
import {MainContentComponent} from './components/main-content/main-content.component';
import {ProductComponent} from './components/products/product/product.component';
import {HomeComponent} from './components/home/home.component';
import {ProductGroupComponent} from './components/productGroups/productGroup/product-group.component';
import {FooterComponent} from './components/footer/footer.component';
import {MatTableModule} from "@angular/material/table";
import {MatButtonModule} from "@angular/material/button";
import {MatPaginatorModule} from "@angular/material/paginator";
import {ProductUpdateComponent} from './components/products/product-update/product-update.component';
import {MatFormFieldModule} from "@angular/material/form-field";
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
import {MatInputModule} from "@angular/material/input";
import {ProductCreateComponent} from './components/products/product-create/product-create.component';
import {MatSortModule} from "@angular/material/sort";
import {ProductGroupUpdateComponent} from './components/productGroups/product-group-update/product-group-update.component';
import {ProductGroupCreateComponent} from './components/productGroups/product-group-create/product-group-create.component';
import {MatExpansionModule} from "@angular/material/expansion";
import {MatSelectModule} from "@angular/material/select";
import {LogoutComponent} from "./components/logout/logout.component";
import {KeycloakService} from "./services/keycloak-service.service";
import {TokenInterceptor} from "./interceptors/token-interceptor";

export function kcFactory(keycloakService: KeycloakService) {
  return () => keycloakService.init();
}

@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent,
    MainContentComponent,
    ProductComponent,
    HomeComponent,
    ProductGroupComponent,
    FooterComponent,
    ProductUpdateComponent,
    ProductCreateComponent,
    ProductGroupUpdateComponent,
    ProductGroupCreateComponent,
    LogoutComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,
    MatSliderModule,
    MatToolbarModule,
    MatIconModule,
    RouterModule.forRoot([
      {path: '', component: HomeComponent},
      {path: 'products/update/:id', component: ProductUpdateComponent},
      {path: 'products/add', component: ProductCreateComponent},
      {path: 'groups', component: ProductGroupComponent},
      {path: 'groups/add', component: ProductGroupCreateComponent},
      {path: 'groups/update/:id', component: ProductGroupUpdateComponent},
      {path: 'logout', component: LogoutComponent},
    ]),
    MatTableModule,
    MatButtonModule,
    MatPaginatorModule,
    MatFormFieldModule,
    ReactiveFormsModule,
    MatInputModule,
    MatSortModule,
    MatExpansionModule,
    MatSelectModule,
    FormsModule
  ],
  providers: [KeycloakService,
    {
      provide: APP_INITIALIZER,
      useFactory: kcFactory,
      deps: [KeycloakService],
      multi: true
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: TokenInterceptor,
      multi: true
    }],
  bootstrap: [AppComponent]
})
export class AppModule {
}

Keycloak client enter image description here

9 Answers

I feel your pain. I was following today this tutorial and got stuck in the same GET 404 error:

https://dev.to/anjnkmr/keycloak-integration-in-angular-application-5a43

The error comes from keycloak-js package. After a couple of hours of trying to find the cause I checked the release versions of keycloak-js and found that new version 11 was released 6 days ago (and which I was using).

I switched to version 10.0.2 for keycloak-js package.

To sum up here are the versions:

keycloak: 10.0.1

keycloak-js: 10.0.2

keycloak-angular: 8.0.1 (if you use it)

angular: 10** (in a desperate move I have upgraded from angular 9 to 10, I do not know if it had an impact)

Therefore for me it was the new version of keycloak-js package that caused all of the trouble.

I would suggest you try downgrading the version of keycloak-js to 10.0.2

Try to use a version of keycloak.js from your keycloak server by path /auth/js/keycloak.js

The library can be retrieved directly from the Keycloak server at /auth/js/keycloak.js and is also distributed as a ZIP archive.

A best practice is to load the JavaScript adapter directly from Keycloak Server as it will automatically be updated when you upgrade the server. If you copy the adapter to your web application instead, make sure you upgrade the adapter only after you have upgraded the server.

You can refer this for futher details.

I faced the same. I used keycloak.js from server and my app is react.

in index html

<script src="https://sso-blabla.com/auth/js/keycloak.js"></script>

Then used this in react app like below

const Keycloak = window.Keycloak;

I am not sure this is the best way to do it. But I used like this because it was only a POC.

My case was even more simpler, my local keycloak instance didn't respond to /auth/* URLs and always returned page not found error. It is because it works without proxy. Once I removed auth from the url, it started to work.

Make sure your set up properly the keycloak URL when you init it.

For local environment I use: auth-server-url: 'http://localhost:8180/'

For staging and production (because they run behind the proxy server with custom path auth added to it): auth-server-url: 'https://myServer/auth'

BTW. I faced this problem with a new Keycloak version 18.0, previously it worked with /auth/ in local too.

I spent quite a bit of time researching this problem, here are some possible solutions that I found on other pages:

  1. (this was my problem) - Incorrect real name - it's case sensitive and in my case I had entered "Master" instead of the correct one - "master"
  2. Using a too recent keycloak-js version with an older keycloak server - solution is to downgrade to an older keycloak-js version
  3. In some cases people had messed up their url - usually forgetting to add "/auth/ - e.g. the correct URL would be "https://example.com/auth/"
  4. Wrong settings - ROOT URL or others could be wrong.

On a rare occasion, perhaps the Keycloak URL is wrong as I did before. So, instead of http://localhost:8282/auth you specified http://localhost:8282/keycloak/auth

I got that same exact error and the problem wasn't related with the front-end but with the backend version of keycloak. After I replaced the base image I was using for keycloak it fix the problem:

In this case I am using in package.json:

"keycloak-angular": "^8.4.0",
"keycloak-js": "^15.0.2",

and version 13.0.1 of keycloak.

  1. Update package.json

"keycloak-angular": "^8.4.0"

"keycloak-js": "^15.1.0"

  1. execute

npm install

  1. execute

ng serve

Success!!!!

change the "keycloak-js" lib to 10.0.2 version works to me.

my keycloak server version is:

Server Version 10.0.1

Related