问题 : 已阻止跨源请求:同源策略禁止读取位于 http://192.168.1.61:9090/user/test 的远程资源。(原因:来自 CORS 预检通道的 CORS 头 'Access-Control-Allow-Headers' 的令牌 'authorization' 无效)。
前端用的是angularJS (主要的代码):
$scope.username = "admin@casnetvi.com";
$scope.password = "12345678";
$scope.login = function(){
var headers = {
authorization: "Basic "+ btoa($scope.username + ":"+ $scope.password)
};
$http.get("http://192.168.1.61:9090/user/test", {headers: headers}).success(function (data) {
console.info(data)
}).error(function () {
alert("无效");
})
}
控制器代码 :
@RequestMapping(value = "/test", method = RequestMethod.GET)
public Object g(String username) {
return "-----------";
}
javaconfig配置:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "OPTIONS", "PUT")
.allowedHeaders("Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method",
"Access-Control-Request-Headers")
.exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials")
.allowCredentials(true).maxAge(3600);
}
security配置 :
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().permitAll()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.httpBasic()
.realmName("ch-cloud-backend development realm")
.and()
.csrf().disable();
}
然后用火狐测试 。
已阻止跨源请求:同源策略禁止读取位于 http://192.168.1.61:9090/user/test 的远程资源。(原因:来自 CORS 预检通道的 CORS 头 'Access-Control-Allow-Headers' 的令牌 'authorization' 无效)。
这是请求头 :
Accept :application/json, text/plain, */*
Accept-Encoding :gzip, deflate
Accept-Language :zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Authorization :Basic YWRtaW5AY2FzbmV0dmkuY29tOjEyMzQ1Njc4
Connection :keep-alive
Host :192.168.1.61:9090
Origin :http://localhost:63343
Referer :http://localhost:63343/ch-cloud-ui/app/views/login.html
User-Agent :Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0
X-Requested-With :XMLHttpRequest
具体是什么原因呢 ? ps: 我用的是spring-boot -1.3 。 所以是没有配置文件的
白猪掌柜的
暮色呼如
相关分类