# How to send token authentication to backend using okta and standalone Angular and SpringBoot 3 login widget

**URL:** <https://devforum.okta.com/t/how-to-send-token-authentication-to-backend-using-okta-and-standalone-angular-and-springboot-3-login-widget/28121>\
**Category:** OAuth/OIDC\
**Tags:** spring, angular\
**Created:** [May 7, 2024, 11:05am UTC](https://devforum.okta.com/t/how-to-send-token-authentication-to-backend-using-okta-and-standalone-angular-and-springboot-3-login-widget/28121 "2024-05-07T11:05:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shirukento1](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@shirukento1](https://devforum.okta.com/u/shirukento1)\
**Post date:** [May 7, 2024, 11:05am UTC](https://devforum.okta.com/t/how-to-send-token-authentication-to-backend-using-okta-and-standalone-angular-and-springboot-3-login-widget/28121/1 "2024-05-07T11:05:53Z")

</div>

Below is my code on how to send authentication to Spring boot side but I get an error that cannot be sent, can someone help me?

auth-interceptor.service.ts

@Injectable({  
providedIn: ‘root’  
})  
export class AuthInterceptorService implements HttpInterceptor {

constructor(@Inject(OKTA\_AUTH) private oktaAuth: OktaAuth) { }  
intercept(request: HttpRequest, next: HttpHandler): Observable\<HttpEvent\> {  
console.log(‘Access Token:1111’);  
return from(this.handleAccess(request, next));  
// from() là một hàm từ thư viện RxJS trong Angular được sử dụng để tạo ra một observable từ một promise, một iterable,…  
}  
private async handleAccess(request:HttpRequest, next:HttpHandler): Promise\<HttpEvent\> {  
//thêm mã thông báo cho các api được bảo mật  
const securityEndpoint =[‘[http://localhost:8080/api/orders](http://localhost:8080/api/orders)’];  
console.log(‘Access Token:1111’);  
alert(‘Access Token:1111’);  
if(securityEndpoint.some(url=\>request.urlWithParams.includes(url))){//kiểm tra xem url có chứa trong securityEndpoint không  
const accessToken = this.oktaAuth.getAccessToken();  
console.log(‘Access Token:’, accessToken);  
//clone the reuqest(vì yêu cầu ko thể thay đổi trực tiếp được)  
request = request.clone({  
setHeaders: {  
Authorization: 'Bearer '+accessToken  
}

```
  });
}

```

return await lastValueFrom(next.handle(request));  
//lastValueFrom() chuyển đổi observable thành promise  
}  
}

config.ts

i  
const oktaConfig= new OktaAuth(oicd\_config.oidc);

export const appConfig: ApplicationConfig = {  
providers: [  
provideRouter(routes),  
provideClientHydration(),  
provideHttpClient(),  
{ provide: OKTA\_CONFIG, useValue: { oktaAuth: oktaConfig } },  
importProvidersFrom(OktaAuthModule.forRoot({ oktaAuth: oktaConfig })),  
{ provide: HTTP\_INTERCEPTORS, useClass: AuthInterceptorService, multi: true },

],  
};

---

<div class="post-metadata">

**Author:** ![shirukento1](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@shirukento1](https://devforum.okta.com/u/shirukento1)\
**Post date:** [May 7, 2024, 11:08am UTC](https://devforum.okta.com/t/how-to-send-token-authentication-to-backend-using-okta-and-standalone-angular-and-springboot-3-login-widget/28121/2 "2024-05-07T11:08:24Z")

</div>

Update:  
securityConfiguration.class

package com.example.demo.config;

import com.okta.spring.boot.oauth.Okta;

@ Configuration  
@ EnableWebSecurity  
public class SecurityConfiguration {  
@ Bean  
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {  
http.authorizeHttpRequests((requests) → requests  
.requestMatchers(“/api/orders/\*\*”).authenticated()  
.anyRequest().permitAll());  
http.oauth2ResourceServer(oauth2 → oauth2.jwt(Customizer.withDefaults()));

```
	return http.build();
}

}

```

![Screenshot 2024-05-07 180631](https://global.discourse-cdn.com/oktadev/original/2X/7/7ede1be15beefcbfefc3c2c2bac96dd50b576e3a.png)

---

<div class="post-metadata">

**Author:** ![daniel.sanders](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@daniel.sanders](https://devforum.okta.com/u/daniel.sanders)\
**Post date:** [May 24, 2024, 3:44pm UTC](https://devforum.okta.com/t/how-to-send-token-authentication-to-backend-using-okta-and-standalone-angular-and-springboot-3-login-widget/28121/3 "2024-05-24T15:44:48Z")

</div>

Hello @shirukento1,

You might check some of the solutions over in this thread: [Getting 401 error while calling spring-boot api from angular app - #17 by mraible](https://devforum.okta.com/t/getting-401-error-while-calling-spring-boot-api-from-angular-app/14371/17)

Essentially, I’d make sure that the callback route is correctly configured, and if you are passing through OktaAuthGuard you can check for errors there: [GitHub - okta/okta-angular: Angular SDK for Okta's OIDC flow](https://github.com/okta/okta-angular?tab=readme-ov-file#oktaauthguard) - this could be something you need to use to access a protected route using the ID Token, so if you aren’t using this it may be a good idea. In the case of the above post it was removed to resolve the situation, which indicates it’s not a protected route using that function and possibly that the ID Token check is coming from somewhere else.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/oktadev/original/1X/589c4c5f80de422d01f47cee7185a7fb8227835c.png) [@system](https://devforum.okta.com/u/system)\
**Post date:** [June 23, 2024, 3:45pm UTC](https://devforum.okta.com/t/how-to-send-token-authentication-to-backend-using-okta-and-standalone-angular-and-springboot-3-login-widget/28121/4 "2024-06-23T15:45:42Z")

</div>

Closed on June 23, 2024, 3:45pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
