【发布时间】:2018-06-14 20:17:10
【问题描述】:
我目前正在开发一个以 Spring Boot 作为后端的 Angular 4 应用程序。
当我想向后端服务器请求令牌时,每个浏览器都会收到不同的错误消息。 1. Chrome 控制台消息:
无法加载 http://localhost:4002/oauth/token?username=wiko&password=shefvaas&grant_type=password:预检响应包含无效的 HTTP 状态代码 401
- Firefox 控制台消息:
跨域请求被阻止:同源策略不允许读取位于http://localhost:4002/oauth/token?username=wiko&password=shefvaas&grant_type=password 的远程资源。 (原因:CORS 预检通道未成功)
这是我的角度请求:
genHeaders(map?: ConfigurationMap) {
const httpHeaders = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': ('Basic ' + btoa('fayaqun:satria'))
});
return {
headers: httpHeaders,
withCredentials: true
};}
这是我的 Spring Boot AuthorizationServerConfig:
@Autowired
private AuthenticationManager authManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient("fayaqun" )
.authorizedGrantTypes("client_credentials", "password", "refresh_token")
.authorities("ROLE_SUPERUSER", "ROLE_VALIDATOR", "ROLE_CLIENT", "ROLE_GUEST")
.scopes("read", "write", "trust")
.resourceIds("oauth2-resource")
.accessTokenValiditySeconds(86400)
.secret("satria");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore()).authenticationManager(authManager);
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
有这方面的经验吗?
谢谢。
【问题讨论】:
标签: angular spring-boot