无法将基于令牌的授权POST请求与Angular集成

时间:2019-07-23 07:54:30

标签: angular rest authentication angular6

需要发送标头中带有授权令牌的POST请求。但是在服务头接收令牌时会丢失。

我尝试了stackoverflow中提供的几种解决方案,但无法解决。

SecurityConfig类

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http
      .csrf().disable()
                  .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
      .and()

      .addFilter(new JwtAuthorizationFilter(authenticationManager()))
      .authorizeRequests()
      .anyRequest().authenticated();
}}

AuthorizationFilter类

@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {

        String header = request.getHeader("Authorization");


        if (header == null || !header.startsWith("Bearer")) {
            chain.doFilter(request, response);
            return;
        }


        Authentication authentication = getUsernamePasswordAuthentication(request);
        SecurityContextHolder.getContext().setAuthentication(authentication);

        // Continue filter execution
        chain.doFilter(request, response);
    }}

从Angular调用

let headers = new HttpHeaders();
     headers = headers.append('Authorization', 'Bearer '+tokenId);
    return this._http.post<any>(this._url,JSON.stringify(categorySubcategory), 
     {
      headers, responseType: 'json'
    });

我应该使用 AuthorizationFilter 类中的 request.getHeader(“ Authorization”)获取令牌。

0 个答案:

没有答案