简体   繁体   English

Angular 的 401 未授权响应

[英]401 Unauthorized Response for Angular

I am trying to make a login page to my website with using Spring Security and Angular.我正在尝试使用 Spring Security 和 Angular 为我的网站创建一个登录页面。 Even if authorization works on Postman, it doesnt work on Angular.即使授权适用于 Postman,它也不适用于 Angular。

Here my SecurityConfiguration class:这是我的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();
    }

}

When i pass username and password using Basic Auth on Postman, it works as you can see:当我在 Postman 上使用 Basic Auth 传递用户名和密码时,它的工作原理如您所见:

在此处输入图像描述

Here my auth service on Angular:这是我在 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});
  }

I tried to pass username and password as hardcoded but it didnt work.我试图将用户名和密码作为硬编码传递,但它没有用。

Here my network tab:这是我的网络标签: 在此处输入图像描述

Can you help me to fix this issue?你能帮我解决这个问题吗? Thank you.谢谢你。

I solved the issue with replacing PostMapping to GetMapping.我解决了将 PostMapping 替换为 GetMapping 的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM