我打從我angularjs客戶端應用程序一個終點時,我做的是一個我收到在瀏覽器控制檯下面的錯誤登錄獲得401訪問http://本地主機:8080 /的OAuth /令牌
選項http://localhost:8080/oauth/token XMLHttpRequest無法加載http://localhost:8080/oauth/token。無效的HTTP狀態代碼401
它是接受來自客戶端的CORS的服務器端代碼。
@Component
public class SimpleCORSFilter implements Filter {
\t public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
\t \t HttpServletResponse response = (HttpServletResponse) res;
\t \t response.setHeader("Access-Control-Allow-Origin", "*");
\t \t response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
\t \t response.setHeader("Access-Control-Max-Age", "3600");
\t \t response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
\t \t chain.doFilter(req, res);
\t }
\t public void init(FilterConfig filterConfig) {}
\t public void destroy() {}
}
這是該調用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();
}
};
});
您將url oauth/auth添加到Spring中的匿名權限? –
將'Authorization'頭添加到** Access-Control-Allow-Headers **列表 – Michael
@Michael我已經在代碼中添加了** Access-Control-Allow-Headers列表**請看一次,我的憑證不是當我在網絡中的瀏覽器控制檯中看到時添加請求。 –