I am developing an application with Angular 7 and Spring Boot. My Probleme is with the authorization request.
I've searched a lot and all the solutions that i found are the same as in my code but the problem is that requests works fine with Postman and it doesn't work with Angular.
this is the Error
and this is my profile service :
getEmployeeById(id: number) {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa('user:userPass'),
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Origin, Content-Type, X-Auth-Token'
})
};
return this.http.get<UserProfile>( this.baseUrl.oneEmployee.replace(':id', id),
httpOptions) ;
}
and this is the security configuration in Spring boot :
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin").password(encoder().encode("adminPass")).roles("ADMIN")
.and()
.withUser("user").password(encoder().encode("userPass")).roles("USER");
}
@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
/*http
.csrf().disable()
.authorizeRequests().anyRequest().authenticated()
.and()
.httpBasic();*/
//http.addFilterBefore(new CorsFilter(), ChannelProcessingFilter.class);
http.authorizeRequests().antMatchers("/").permitAll()
.anyRequest().fullyAuthenticated().and().httpBasic().and().csrf().disable();
}