一: springboot配置
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") //配置允许跨域访问的路径
.exposedHeaders("Authorization") // 配置允许被访问的请求头
.allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins("*")
// 浏览器是否应发送凭据,例如cookie
.allowCredentials(false);
}
}
二:angualr配置:
以后任何请求都会自动携带该token信息
this.http
.post('http://localhost:8080/auth/login?_allow_anonymous=true', {
username: this.userName.value,
password: this.password.value,
rememberMe: 1
}, '', { observe: 'response'})
.subscribe((res) => {
console.log(res);
// 获取token
const token = res.headers.get('Authorization').split(' ')[1];
// 清空路由复用信息
this.reuseTabService.clear();
// 设置用户Token信息
this.tokenService.set({ token : token});
// 重新获取 StartupService 内容,我们始终认为应用信息一般都会受当前用户授权范围而影响
this.startupSrv.load().then(() => {
let url = this.tokenService.referrer.url || '/';
if (url.includes('/passport')) url = '/';
this.router.navigateByUrl(url);
});
});
}