0
我有一个在端口8080
上运行的angularjs前端,端口3000
上有后端。 后端当前使用POSTMAN工作正常。在localhost上以angularjs身份获取未经授权的401:3000为后端
但是,当我将它们结合起来,我得到了后端以下
我的设置是:
//Global CORS configuration
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}
安全性设定为(不登录页面,但HTTP基本):
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/**").hasAnyRole("ADMIN", "USER")
.and()
.httpBasic()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Bean
public CustomBasicAuthenticationEntryPoint getBasicAuthEntryPoint(){
return new CustomBasicAuthenticationEntryPoint();
}
前端角度:
app.factory('SurveysService', function($q, $http) {
var encodedUserNameAndPassword = btoa('admin:letme1');
// $http.defaults.headers.common.Authorization = 'Basic ' +
// Base64.encode($scope.username + ':' + $scope.password);
$http.defaults.headers.common['Authorization'] = 'Basic ' + encodedUserNameAndPassword;
var ss = 'http://localhost:3000';
return {
getSurveys : function() {
return $q(function(resolve, reject) {
$http.get(ss+'/surveys?start=1&limit=10').then(function(response) {
resolve(response.data)
},
function(response){
reject(response)
})
})
}}
})
任何人都知道可能是什么问题?
401不是CORS问题 –
这是授权问题,请再次查看spring安全性。 请将'header'传递给'ADMIN'而不是'admin'。 –
@ user3145373我的用户'admin'是一个内存中的角色为'ADMIN'的用户。我已经考虑到春季安全无法解决问题。你是什么意思看标题? – jemrug