在浏览器中直接URL命中的Jwt令牌认证

时间:2016-06-22 12:36:20

标签: angularjs rest authentication spring-security jwt

我有Angular + Spring应用程序,我尝试使用JWT令牌验证服务。

在Jwt身份验证中,我将令牌存储在localStorage中,并在每个请求的Authorization HTTP头中附加令牌。这可以通过在AngularJs中编写拦截器来完成。我已经关注了this教程。

现在我的问题是:
如何通过在浏览器中点击URL来直接验证请求?

当您点击Url时,没有加载任何页面,也没有将URL从localStorage绑定到Authentication头的脚本。因此,即使用户之前已经过身份验证,服务器也会拒绝该请求并发送401。

1 个答案:

答案 0 :(得分:1)

对于需要由浏览器直接加载的视频,图片或文档等网络资源,请使用JWT(无法设置标题)将参数添加到网址

  path/to/resource?jwt=...

在服务器端,如果身份验证标头不存在,则解码寻找jwt的URL

对于您尚未生成的传入链接,例如来自搜索引擎,

  1) accept the link and redirect user to login page with a cookie/field set to the destination page
  2) automatically, login user with JWT from local storage set in header in the usual way
  3) if it is succesful, redirect to the destination 
相关问题