【问题标题】:Handling token with Grails 3使用 Grails 3 处理令牌
【发布时间】:2017-02-09 11:02:14
【问题描述】:

我正在尝试使用 Grails 3.1.4 连接到 API。

实际上验证已经完成,我在发送电子邮件和密码后得到生成的令牌。

之后我正在尝试发送查询。但是,我无法在 http 查询的标头中发送令牌

String url="www.myurl.com"
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL))

connection = (HttpURLConnection) new URL(url).openConnection()
connection.setRequestMethod("GET")
connection.setDoOutput(true)
connection.setDoInput(true)
connection.setConnectTimeout(10000)
connection.setRequestProperty("content-Type","application/x-www-form-urlencoded")
connection.setRequestProperty("Set-Cookie","token/"+token)
OutputStream output = connection?.getOutputStream()
output.write(query.getBytes())

【问题讨论】:

    标签: java spring http grails http-headers


    【解决方案1】:

    这完全取决于所使用的身份验证机制。如果使用 JWT 令牌,则需要添加带有关键字“Authentication”和值为“Bearer”的 http 标头。请询问您的服务供应商使用哪种机制。通常 401 状态码表示您做错了什么。

    【讨论】:

    • 其实我使用的是REST web service,它是一个基于cookie的spring Authentication。我认为问题出在本节的 connection.setRequestProperty("Set-Cookie","JSESSIONID="+token+"; Path=/; HttpOnly")`
    猜你喜欢
    • 2017-10-08
    • 1970-01-01
    • 2022-07-19
    • 2019-08-06
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2021-10-06
    相关资源
    最近更新 更多