1.問題
請求后臺驗證碼接口,獲取驗證碼和驗證碼校驗的接口吻贿,校驗時總是獲取不到數(shù)值串结,就是兩次的SESSIONID不一樣
導致前端驗證碼數(shù)據(jù)傳到后臺,卻取不到后臺存儲的驗證碼舅列,無法進行校驗
2.解決
注意請求驗證碼圖片的域名要和請求校驗的域名一樣,比如肌割,
localhost
和127.0.0.1
是不一樣的
1)前端
在請求中加入一個withCredentials: true
就行了,意思就是跨域帶cookie請求
Angularjs請求示例
$scope.firstnext = function() {
$http({
method: "get",
url: 'http://localhost:8083/contrastcode?verification=' + $scope.identycode,
headers: {},
withCredentials: true //!跨域帶cookies
}).then(function(req) {
$log.log(req.data)
if(req.data.valid == true){
//do somethings
}
})
}
2)后臺跨域解決
@Configuration
public class CustomCORSConfiguration {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}