,同時撥打電話,我收到此錯誤「/的OAuth /令牌」時,我正在一個HTTP調用到我自己的系統上運行的服務器實例。春季安全的OAuth2:CORS預檢通道沒有成功
@Component
public class SimpleCORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
HttpServletRequest httpServletRequest = (HttpServletRequest) req;
if (httpServletRequest.getMethod() != "OPTIONS") {
chain.doFilter(req, res);
} else {
// In case of HTTP OPTIONS method, just return the response
}
}
我將它作爲WebConfigurer過濾器:
private void initCORSFilter(ServletContext servletContext, EnumSet<DispatcherType> disps) {
log.debug("Registering CORS Filter");
FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", new SimpleCORSFilter());
Map<String, String> parameters = new HashMap<>();
corsFilter.setInitParameters(parameters);
corsFilter.addMappingForUrlPatterns(disps, true, "/*");
corsFilter.setAsyncSupported(true);
}
我得到這個錯誤在Firefox:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://182.176.221.94:9091/ams/oauth/token. (Reason: CORS preflight channel did not succeed).
在我通過創建這樣一個過濾器修復了這個總之,我一直在確保預檢OPTIONS調用總是得到響應,繼續前進。這是在我自己的系統上工作,但現在服務器實例部署在Linux服務器上,我再次遇到這個問題。我只是在調用'oauth/token'時纔得到它,其他所有調用都正常工作。
我還能做些什麼來擺脫這一點。任何幫助?
我已經更新了這個問題。請看一下。我已經在WebConfigurer中添加了過濾器。刪除@Component屬性使其即使在我的本地機器上也停止工作。 –
工作對我來說沒有@Component所以什麼是錯在你的項目 –
我增加了大約不一致的詳細信息過濾器中的有關選項 –