我是 jhipster 的新手,对蓝图也有反应。我一直在查看 React 代码结构,发现它的结构非常好。但我找不到的一件事是:access_token、application/json、x-xsrf-token、host 等标头内容在哪里配置。
有拦截器的 axios 配置:
import axios from 'axios';
import { getBasePath, Storage } from 'react-jhipster';
import { SERVER_API_URL } from 'app/config/constants';
const TIMEOUT = 1 * 60 * 1000;
axios.defaults.timeout = TIMEOUT;
axios.defaults.baseURL = SERVER_API_URL;
const setupAxiosInterceptors = onUnauthenticated => {
const onRequestSuccess = config => {
return config;
};
const onResponseSuccess = response => response;
const onResponseError = err => {
const status = err.status || (err.response ? err.response.status : 0);
if (status === 403 || status === 401) {
onUnauthenticated();
}
return Promise.reject(err);
};
axios.interceptors.request.use(onRequestSuccess);
axios.interceptors.response.use(onResponseSuccess, onResponseError);
};
export default setupAxiosInterceptors;
这是一个示例 reducer 文件(authentication.ts):
export const getSession: () => void = () => async (dispatch, getState) => {
await dispatch({
type: ACTION_TYPES.GET_SESSION,
payload: axios.get('services/uaa/api/account'),
});
const { account } = getState().authentication;
if (account && account.langKey) {
const langKey = Storage.session.get('locale', account.langKey);
await dispatch(setLocale(langKey));
}
};
export const login: (username: string, password: string, rememberMe?: boolean) => void = (username, password, rememberMe = false) => async (
dispatch,
getState
) => {
const result = await dispatch({
type: ACTION_TYPES.LOGIN,
payload: axios.post('auth/login', { username, password }),
});
await dispatch(getSession());
};
export const logout: () => void = () => async dispatch => {
await dispatch({
type: ACTION_TYPES.LOGOUT,
payload: axios.post('auth/logout', {}),
});
// fetch new csrf token
dispatch(getSession());
};
export const clearAuthentication = messageKey => (dispatch, getState) => {
dispatch(displayAuthError(messageKey));
dispatch({
type: ACTION_TYPES.CLEAR_AUTH,
});
};
我没有看到任何内容axios.headers.access_token="my-token"
,但在发送请求标头值时已设置。你能帮我看看它是如何工作的吗?标题内容在哪里?