Commit 668415b4 authored by kasmiii's avatar kasmiii
Browse files

[FRONT-END]Configuring interceptor service-fix

parent 32523a86
......@@ -6,7 +6,6 @@ import com.example.server.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
......@@ -47,17 +46,15 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
// http.cors().disable();
http.csrf().disable()
http.cors().and().csrf().disable()
.authorizeRequests()
.antMatchers("/api/admin/**").hasRole(Const.ADMIN)
.antMatchers("/api/home").permitAll()
// .antMatchers("/api/user/**").authenticated()
.antMatchers("/api/users/add").permitAll()
.antMatchers("/api/users/login").permitAll()
.antMatchers("/api/post/**").authenticated()
.antMatchers("/api/comment/**").authenticated()
.antMatchers("/api/users/all").authenticated()
.antMatchers("/api/users/add").permitAll()
// .antMatchers("/api").permitAll()
.antMatchers("/api/users/login").permitAll()
.and().exceptionHandling().and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
......
......@@ -61,8 +61,9 @@ public class UserController {
return new ResponseEntity<>("Deleted successfully !", HttpStatus.OK);
}
@GetMapping()
public ResponseEntity<List<User>> getAll() {
@GetMapping("/all")
public ResponseEntity<List<User>> getAllUsers() {
System.out.println("get all users method is called...");
return new ResponseEntity<>(userService.findAll(), HttpStatus.OK);
}
......
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { UserComponent } from './components/user/user.component';
import { PostComponent } from './components/post/post.component';
import { CommentComponent } from './components/comment/comment.component';
import {AppRoutingModule} from './app-routing.module';
import {AppComponent} from './app.component';
import {UserComponent} from './components/user/user.component';
import {PostComponent} from './components/post/post.component';
import {CommentComponent} from './components/comment/comment.component';
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
import { HomeComponent } from './home/home.component';
import { NotfoundComponent } from './notfound/notfound.component';
import { MessagetestComponent } from './messagetest/messagetest.component';
import { HeaderComponent } from './header/header.component';
import { LoginComponent } from './login/login.component';
import { SignupComponent } from './signup/signup.component';
import { QuestionComponent } from './question/question.component';
import { PostDetailsComponent } from './post-details/post-details.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpClientModule } from '@angular/common/http';
import { ToastrModule } from 'ngx-toastr';
import {HomeComponent} from './home/home.component';
import {NotfoundComponent} from './notfound/notfound.component';
import {MessagetestComponent} from './messagetest/messagetest.component';
import {HeaderComponent} from './header/header.component';
import {LoginComponent} from './login/login.component';
import {SignupComponent} from './signup/signup.component';
import {QuestionComponent} from './question/question.component';
import {PostDetailsComponent} from './post-details/post-details.component';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {HttpClientModule} from '@angular/common/http';
import {ToastrModule} from 'ngx-toastr';
import {JwtClientService} from "./services/jwt-client.service";
import { CookieService } from 'ngx-cookie-service';
import { SignoutComponent } from './signout/signout.component';
import {CookieService} from 'ngx-cookie-service';
import {SignoutComponent} from './signout/signout.component';
import {httpInterceptorProviders} from "./services";
@NgModule({
declarations: [
......@@ -47,7 +48,8 @@ import { SignoutComponent } from './signout/signout.component';
FormsModule,
ReactiveFormsModule
],
providers: [JwtClientService, CookieService],
providers: [JwtClientService, CookieService, httpInterceptorProviders],
bootstrap: [AppComponent]
})
export class AppModule { }
export class AppModule {
}
import {Component, OnInit} from '@angular/core';
import {FormBuilder} from "@angular/forms";
import {HttpClient} from "@angular/common/http";
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {Router} from "@angular/router";
import {Global} from "../global-classes/global";
......@@ -19,8 +19,23 @@ export class HomeComponent implements OnInit {
}
ngOnInit(): void {
console.log("token retrieved is:: "+Global.TOKEN);
this.http.get(this.baseUrl + "/all").subscribe(
console.log("token retrieved is:: " + Global.TOKEN);
const headers: HttpHeaders = new HttpHeaders();
// headers.set('Authorization', 'Bearer ' + Global.TOKEN);
// const httpOptions = {
// headers: new HttpHeaders({
// Authorization: 'Bearer ' + Global.TOKEN
// })
// };
const httpOptions = {
headers: new HttpHeaders({
// Authorization: 'Bearer ' + Global.TOKEN
})
};
this.http.get(this.baseUrl + "/all").subscribe(// httpOptions
(data: any) => {
console.log("list of users:: " + data[0].firstName + " " + data[0].lastName);
},
......
......@@ -2,8 +2,9 @@ import {Component, OnInit} from '@angular/core';
import {JwtClientService} from "../services/jwt-client.service";
import {FormBuilder, Validators} from "@angular/forms";
import {HttpClient} from "@angular/common/http";
import { ToastrService } from 'ngx-toastr';
import {ToastrService} from 'ngx-toastr';
import {Router} from "@angular/router";
import {Global} from "../global-classes/global";
@Component({
selector: 'app-login',
......@@ -14,6 +15,7 @@ export class LoginComponent implements OnInit {
loginForm: any;
token: string = "";
baseUrl: string = "http://127.0.0.1:8000/api/users";
constructor(private jwtClientService: JwtClientService,
private fb: FormBuilder,
private http: HttpClient,
......@@ -43,6 +45,7 @@ export class LoginComponent implements OnInit {
.subscribe(
data => {
console.warn("Token " + data);
Global.TOKEN = data;
this.router.navigate(["/"]);
this.jwtClientService.connect(data.toString())
this.toastr.success("Bon retour parmis nous " + authRequest.userName + " !")
......
import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable, of} from "rxjs";
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {HttpEvent, HttpHandler, HttpHeaders, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {Global} from "../global-classes/global";
@Injectable({providedIn: 'root'})
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
private AUTH_HEADER = 'Authorization';
private token = Global.TOKEN;
......@@ -12,17 +12,12 @@ export class AuthInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (!req.headers.has('Content-Type')) {
//set authorization for path /users/all
if (req.url.includes("/users/all")){
req = req.clone({
headers: req.headers.set('Content-Type', 'application/json')
headers: new HttpHeaders({Authorization: 'Bearer ' + Global.TOKEN})
});
}
req = req.clone({
headers: req.headers.set('Access-Control-Allow-Origin', 'http://127.0.0.1:8000')
});
req = this.addAuthenticationToken(req);
return next.handle(req);
// return next.handle(req).pipe(
// catchError((error: HttpErrorResponse) => {
......@@ -68,7 +63,7 @@ export class AuthInterceptor implements HttpInterceptor {
return request;
}
return request.clone({
headers: request.headers.set(this.AUTH_HEADER, 'Bearer ' + this.token)
headers: request.headers.append('Authorization', 'Bearer ' + this.token)//this.AUTH_HEADER
});
}
}
/* "Barrel" of Http Interceptors */
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import {AuthInterceptor} from "./auth-interceptor.service";
/** Http interceptor providers in outside-in order */
export const httpInterceptorProviders = [
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
];
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment