简体   繁体   English

获取401访问http:// localhost:8080 / oauth / token

[英]getting 401 to access http://localhost:8080/oauth/token

I am hitting one end point from my angularjs client app to login when I am doing that one I am getting the following ERROR in browser console 我正在从我的angularjs客户端应用程序中击中一个端点进行登录时,在浏览器控制台中出现以下错误

OPTIONS http://localhost:8080/oauth/token XMLHttpRequest cannot load http://localhost:8080/oauth/token . 选项http:// localhost:8080 / oauth / token XMLHttpRequest无法加载http:// localhost:8080 / oauth / token Invalid HTTP status code 401 无效的HTTP状态代码401

It's server side code to accept CORS from the client. 服务器端代码可以接受来自客户端的CORS。

 @Component public class SimpleCORSFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with"); chain.doFilter(req, res); } public void init(FilterConfig filterConfig) {} public void destroy() {} } 

It's client side code which calling the http://localhost:8080/oauth/token 客户端代码调用了http:// localhost:8080 / oauth / token

 angular.module('frontendApp') .factory('AuthServerProvider', function loginService($http, localStorageService, Base64, API_SERVER) { return { login: function (credentials) { var data = "username=" + credentials.username + "&password=" + credentials.password + "&grant_type=password&scope=read%20write&" + "client_secret=123456&client_id=clientapp"; return $http.post(API_SERVER + 'oauth/token', data, { headers: { "Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json", "Access-Control-Allow-Origin": "*", "Authorization": "Basic " + Base64.encode("clientapp" + ':' + "123456") } }).success(function (response) { var expiredAt = new Date(); expiredAt.setSeconds(expiredAt.getSeconds() + response.expires_in); response.expires_at = expiredAt.getTime(); localStorageService.set('token', response); return response; }); }, logout: function () { // logout from the server $http.post('api/logout').then(function () { localStorageService.clearAll(); }); }, getToken: function () { return localStorageService.get('token'); }, hasValidToken: function () { var token = this.getToken(); return token && token.expires_at && token.expires_at > new Date().getTime(); } }; }); 

In case of OPTIONS request, you should not do further processing, ie skip the call to chain.doFilter(req, res) , eg: 如果有OPTIONS请求,则不应进行进一步处理,即跳过对chain.doFilter(req, res)的调用,例如:

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;

    response.addHeader("Access-Control-Allow-Origin", "*");

    if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "content-type,access-control-request-headers,access-control-request-method,accept,origin,authorization,x-requested-with");
        response.setStatus(HttpServletResponse.SC_OK);
    } else {
        chain.doFilter(req, resp);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot2 Oauth2隐式流 - http:// localhost:8080 / oauth / authorize获取访问被拒绝 - Spring Boot2 Oauth2 Implicit Flow - http://localhost:8080/oauth/authorize getting Access Denied OAuth:尝试获取对LinkedIn的访问令牌时的HTTP 401状态 - OAuth: HTTP 401 Status when trying to get access token to LinkedIn Spring Boot 2.0.4 + OAuth2 + JWT-无法获取访问令牌,返回405或仅被映射到localhost:8080 / - Spring Boot 2.0.4 + OAuth2 + JWT - Cannot get Access Token, returns 405 or just gets mapped into localhost:8080/ 尝试从Neteller获取OAuth访问令牌会产生错误:“服务器返回的HTTP响应代码:URL 401” - Attempt to get OAuth access token from Neteller produces error: “Server returned HTTP response code: 401 for URL” Spring Boot 2.0.3 Oauth2安全性:即使在标头中使用访问令牌也会出现401错误 - Spring Boot 2.0.3 Oauth2 Security: Getting 401 error even when using access token in header 获取服务帐户的访问令牌时出错:401 Unauthorized\nPOST https://oauth2.googleapis.com/token; 谷歌日历 API - Error getting access token for service account: 401 Unauthorized\nPOST https://oauth2.googleapis.com/token; Google Calendar API Spring Boot和OAuth2:获取ResourceAccessException:“ http:// localhost:5555 / oauth / token”的POST请求出现I / O错误:拒绝连接:connect - Spring Boot & OAuth2: Getting ResourceAccessException: I/O error on POST request for “http://localhost:5555/oauth/token”: Connection refused: connect 对于预检,响应具有无效的HTTP状态代码401,用于oauth / token - Response for preflight has invalid HTTP status code 401 for oauth/token Spring 5,401返回后带有Google刷新访问令牌的OAuth2 - Spring 5, OAuth2 with Google refresh access token after 401 return 错误401请求Google使用Oauth2访问令牌 - Error 401 requesting access token from Google with Oauth2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM