【发布时间】:2016-02-27 06:38:06
【问题描述】:
当我尝试向调用“oauth/token”端点的 Spring 服务器进行身份验证时遇到 CORS 问题。 服务器以 401 响应响应
XMLHttpRequest cannot load http://localhost:8080/oauth/token.
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8000' is therefore not allowed access.
The response had HTTP status code 401.
这是我调用服务器的方式:
login: function(credentials) {
var data = "username=" + encodeURIComponent(credentials.username) + "&password="
+ encodeURIComponent(credentials.password) + "&grant_type=password&scope=read%20write&" +
"client_secret=mySecretOAuthSecret&client_id=awhyapp";
return $http.post('http://localhost:8080/oauth/token', data, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"Access-Control-Allow-Origin": "*",
"Authorization": "Basic " + Base64.encode("awhyapp" + ':' + "mySecretOAuthSecret")
}
}).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;
});
Oauth 配置:
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll()
.and()
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.logout()
.logoutUrl("/api/logout")
.logoutSuccessHandler(ajaxLogoutSuccessHandler)
.and()
.csrf()
.requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
.disable()
.headers()
.frameOptions().disable()
.and()
.authorizeRequests()
.antMatchers("/api/authenticate").permitAll()
.antMatchers("/api/register").permitAll()
.antMatchers("...").permitAll()
}
}
我遵循了这个问题 (Add headers to oauth/token response (spring-security)) 中的答案,但我的问题是,无论我如何配置我的 Filter 类,即使我使用 @Order 注释设置了最高优先级,这些函数也永远不会被调用。
【问题讨论】:
-
我遇到了完全相同的问题...您找到解决方法了吗?
标签: angularjs oauth spring-boot cors jhipster