簡體   English   中英

Angular 的 401 未授權響應

[英]401 Unauthorized Response for Angular

我正在嘗試使用 Spring Security 和 Angular 為我的網站創建一個登錄頁面。 即使授權適用於 Postman,它也不適用於 Angular。

這是我的SecurityConfiguration class:

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors();
        http.csrf().disable().
                authorizeRequests().antMatchers("/**","/login","/faqbio/auth").
                fullyAuthenticated().and().httpBasic();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

}

當我在 Postman 上使用 Basic Auth 傳遞用戶名和密碼時,它的工作原理如您所見:

在此處輸入圖像描述

這是我在 Angular 上的身份驗證服務:

  auth(request : UserRequest): Observable<any> {
    const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(request.username + ':' + request.password) });
    return this.http.post<any>("http://localhost:8085/faqbio/auth",{headers});
  }

我試圖將用戶名和密碼作為硬編碼傳遞,但它沒有用。

這是我的網絡標簽: 在此處輸入圖像描述

你能幫我解決這個問題嗎? 謝謝你。

我解決了將 PostMapping 替換為 GetMapping 的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM