vue axios request is used in the front end
token authentication of springboot oauth2 is used in the background
the cross-domain request that I have configured
now encounters the problem of logging in normally to get token and doing other requests
when the token expires and the browser status code is 401, how can the client get the error.response returned by the server as undefined. The problem is a cross-domain problem.
I think it should be to modify the filter of springboot oauth2, but I don"t know how to modify it
springboot my current cross-domain settings
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(-1)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Resource(name = "usersService")
private UserDetailsService userDetailsService;
@Autowired
private ClientDetailsService clientDetailsService;
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Autowired
public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(encoder()); //
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.anonymous().disable()
.requestMatchers()
.antMatchers(HttpMethod.OPTIONS, "/oauth/token");
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
@Bean
@Autowired
public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
handler.setTokenStore(tokenStore);
handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
handler.setClientDetailsService(clientDetailsService);
return handler;
}
@Bean
@Autowired
public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
TokenApprovalStore store = new TokenApprovalStore();
store.setTokenStore(tokenStore);
return store;
}
@Bean
public BCryptPasswordEncoder encoder(){
return new BCryptPasswordEncoder();
}
/**
*
* @return
*/
@Bean
public FilterRegistrationBean filterRegistrationBean() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(1);
return bean;
}
vue configuration
importaxiosfrom"axios"
importstorefrom"../store"
//axios
axios.defaults.timeout=5000
axios.defaults.baseURL="http://localhost:8888"
//
axios.interceptors.request.use(
config=>{
lettoken=store.state.token
if(token===""){
config.auth={username:"devglan-client",password:"devglan-secret"}
config.headers={"Content-type":"application/x-www-form-urlencoded;charset=utf-8"}
}else{
config.headers={"Content-type":"application/json;charset=utf-8"}
}
returnconfig
},
error=>{
console.log("-------request-----")
returnPromise.reject(error)
})
//
axios.interceptors.response.use(
response=>{
returnresponse
},
error=>{
if(error&&error.response){
console.log("*******errorerrorerrorerror***************")
console.log(error)
}else{
console.log("**********************")
console.log(error.response)
//debugger
console.log(error)
console.log(error.config)
console.log(error.code)//
console.log(error.status)
console.log("**********************")
//error.message=""
this.router.replace({
path:"login",
query:{redirect:this.router.currentRoute.fullPath}
})
}
returnPromise.reject(error)
}
)
exportdefault{
fetchGet(url,params={}){
lettoken=store.state.token
if(token!==""){
url=url+"?access_token="+token
}
returnnewPromise((resolve,reject)=>{
axios.get(url,params).then(res=>{
resolve(res.data)
}).catch(error=>{
console.log("------fetchGet-----error")
console.log(error)
reject(error)
})
})
},
fetchPost(url,params={}){
lettoken=store.state.token
if(token!==""){
url=url+"?access_token="+token
}
console.log("fetchPost----url---->"+url)
returnnewPromise((resolve,reject)=>{
axios.post(url,params).then(res=>{
resolve(res)
}).catch(error=>{
console.log("------fetchPost-----error")
console.log(error)
reject(error)
})
})
}
}
response
401
the problems found on the Internet are similar to the mistakes made by this big brother
https://codeshelper.com/q/10.
kneel down and beg all the great gods to watch