您的位置:首页 > 其它

JIRA REST API Example - Cookie-based Authentication

2016-04-20 11:30 429 查看
三种方式可以通过 JIRA REST API 授权:allow REST clients to authenticate themselves using
1. cookies

2. basic authentication.

3. OAuth

当然此次主要是面向 cookie-based——存在一定的风险:

*reference

Your session cookies can be hijacked if handled improperly.This document does not go into the security implications of cookies,but if you should make yourself aware of the risks, before considering this approach.



在使用 cookie 前你需要知道的:

*reference

The basics of using REST APIs, e.g. requests, responses, headers.

The basics of using and administering JIRA.



实际上通过 cooki-based 来授权就需要三步:



*reference
This is how cookie-based authentication works in JIRA at a high level:

The client creates a new session for the user, via the JIRA REST API .

JIRA returns a session object, which has information about the session including the session cookie. The client stores this session object.

The client can now set the cookie in the header for all subsequent requests to the JIRA REST API. 



1. 通过 JIRA REST API (实际上就是一个 url,不过是根据你所在的项目定制的)创建好一个session。

2. 将得到的 session 对象(session 对象里面包含 cookie 信息)返回。

- session 对象大致长成这样:

*reference

{

"session":

{

"name":"JSESSIONID",

"value":"6E3487971234567896704A9EB4AE501F"

},

"loginInfo":

{

"failedLoginCount":1,

"loginCount":2,

"lastFailedLoginTime":"2013-11-27T09:43:28.839+0000",

"previousLoginTime":"2013-12-04T07:54:59.824+0000"

}

}



3. 将 cookie 设置到 headers 里面。

具体的步骤:

第一步:通过 JIRA REST API 创建 session 

*reference

To do this, just POST the desired user credentials (as JSON) to the session resource:

Example resource http://jira.example.com:8090/jira/rest/auth/1/session
Example credentials { "username": "myuser", "password": "mypassword" }



使用 post 方式将 上述的 credentials 中的内容转换为 json (Python:使用 json.dumps()方法 )类型,然后将其 post 到需要的 resource (例如:http://hdc-tomcat-xxx.xxxxsoft.org/jira/rest/auth/1/session)里面。

第二步:将session对象的cookie 信息提取出来,当你需要发送请求的时候,从session 中提取出 cookie 的 name + value——session 关键字里面的 name/value 关键字。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: