【发布时间】:2019-08-23 22:07:01
【问题描述】:
我正在使用 Angular 7 和 Spring Boot 开发应用程序。 我的问题是授权请求。
我已经搜索了很多,我找到的所有解决方案都与我的代码中的相同,但问题是 requests 在 Postman 中运行良好,但不适用于 Angular。
这是错误
这是我的个人资料服务:
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) ;
}
这是 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();
}
【问题讨论】:
-
CORS 被抛出,因为您请求另一个 url。我不使用弹簧,但(通常)您需要配置请求标头。您可以使用本指南stackoverflow.com/questions/51640206/angular-client-enable-cors
-
不,问题是我的请求适用于 PostMan,但是当我想使用 Angular 7 运行它时,它传递了正确的标头,但不想授权请求
标签: angular spring-boot spring-security