Angular 2+身份验证标头

时间:2019-01-25 11:07:45

标签: java angular api http header

我有一个Java后端,它为我提供了一些受用户和密码保护的API。

因此,如果我使用具有授权功能的任何浏览器或邮递员进行呼叫,我都会做出回应。 但是,如果我使用相同的用户名和密码设置httpHeaders,则会得到401(未授权)...

我的代码:

turtles-own [sex availa-males mates mate-count max-mate-count strategy n-max prop_B proba_B] 
breed [males male]
breed [females female]
to setup
clear-all
  create-males 50
  create-females 1
  ask turtles [
  setxy random-xcor random-ycor
  ifelse random 2 = 1 [set strategy 1] [set strategy 0]  
   ]

  ask males [set color red]
  ask females [set color blue]
 reset-ticks 
end 

to go 
  ask males [
 ; fd 1
    ]
  ask turtles [
  set mates ( turtle-set ) 
  ]
  ask females [choose]
  tick
end 

to choose

    ; set a cap on possible mates for females; 5, or the number
    ; available within the radius if less than 5
  set availa-males males in-radius 5
    set n-max count availa-males
    set max-mate-count ifelse-value ( n-max < 5 ) [ n-max ] [ 5 ] ; 5 5

; Until a female has chosen up to her maximum number of mates:
  while [ mate-count < max-mate-count ]

     [; determine which available males are not already in her 'mates' agentset
      set availa-males availa-males with [ not member? self [mates] of myself ]

; assess the proportion of the '0' strategy in remaining available males
      set prop_B ( count availa-males with [ strategy = 0 ] ) / n-max

      ; example probability choice, just meant to choose '0 strategy' males
      ; with a frequency disproportionate to availability
      set proba_B ifelse-value ( prop_B <= 0.1 ) [ 0.8 ] [ 0.2 ] 

      ; use a random float to determine which strategy type is chosen
      set mates ( turtle-set mates
        ifelse-value ( random-float 1 < proba_B )
        [ one-of availa-males with [ strategy = 0] ]
          [ one-of availa-males with [ strategy = 1]] )

      ; count the current mates to break the while loop once
      ; the maximum number of mates is reached
      set mate-count count mates
    ]     
  ; have the female's males add her to their own mates agentset
    ask mates [ set mates ( turtle-set mates myself ) ]

    if n-max < count mates [ print  "Fewer available males than mates" ]
end 

我的答复: enter image description here

如果我直接输入网址: enter image description here

1 个答案:

答案 0 :(得分:0)

尝试通过以下方式设置headers

let headers = new HttpHeaders();

headers = headers.append('Authorization', 'Basic ' + btoa(username + ':' + password));
headers = headers.append('Content-Type', 'application/json');
headers = headers.append('cache-control', 'no-cache');

多次使用.set()时,每次都会覆盖标头,并且仅发送最后一个标头。