【问题标题】:Username and password have been treated as anonymous in Spring-security-oauth2 password mode在 Spring-security-oauth2 密码模式下,用户名和密码已被视为匿名
【发布时间】:2019-03-18 00:07:20
【问题描述】:

我正在使用 Spring Boot 和 Spring Security OAuth2 向前端发出令牌。

邮递员
当我使用邮递员进行测试时,一切正常。

.

浏览器
但是当我使用 vue.js 和 axios 在浏览器上发送相同的请求时,它没有按预期工作。状态码是 401。

Gerneral:
Request URL: http://localhost:8080/oauth/token
Request Method: POST
Status Code: 401 
Remote Address: [::1]:8080
Referrer Policy: no-referrer-when-downgrade

Response Headers:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:8081
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Sun, 17 Mar 2019 02:20:54 GMT
Expires: 0
Pragma: no-cache
Transfer-Encoding: chunked
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
WWW-Authenticate: Basic realm="oauth2/client"
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

Request Headers:
Provisional headers are shown
Accept: application/json, text/plain, */*
Content-Type: application/x-www-form-urlencoded
Origin: http://localhost:8081
Referer: http://localhost:8081/login
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
Form Data:
{"grant_type":"password","scope":"all","username":"admin","password":"888","client_id":"wellcell","client_secret":"wellcell"}: 

服务器控制台日志的差异
我拍了一张并排的照片:

左侧是邮递员请求的服务器控制台日志。 右侧是浏览器请求的服务器控制台日志。

在“ClientCredentialsTokenEndpointFilter”之后,邮递员请求到“DaoAuthenticationProvider”进行身份验证。 但是浏览器请求转到“BasicAuthencationFilter”,“用户名”和“密码”被忽略,并返回了一个匿名用户。然后,匿名用户的访问被拒绝。

以前有人遇到过这种问题吗?

【问题讨论】:

    标签: spring-boot vue.js axios postman spring-security-oauth2


    【解决方案1】:

    我认为Content-Type: application/x-www-form-urlencoded 有问题。如果发送json,则需要使用Content-Type: application/json。 简单使用 axios 和 post 的 json:

    axios.post("http://localhost:8080/oauth/token", { 
      "grant_type": "password",
      "scope": "all",
      "username": "admin",
      "password": "888",
      "client_id": "wellcell",
      "client_secret": "wellcell"
    }).then((response) => {
      console.log(response.data);
    });
    

    【讨论】:

      猜你喜欢
      • 2016-03-19
      • 2014-12-21
      • 2015-03-09
      • 2015-07-25
      • 2011-12-12
      • 2012-02-08
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多