I have implemented Filter for CORS in spring boot.The code is as follow:-
@SpringBootApplication
@Component
public class Application implements Filter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
To get the access_token from oauth2 I have created following object from angularjs:-
{"url":"http://localhost:8080/oauth/token?grant_type=client_credentials&client_id=clientId&client_secret=clientSecret","headers":{"Authorization":"Basic token_value"},"withCredentials":true,"method":"POST"}
I am getting following error when I hit the server:-
OPTIONS url... 401() Response for preflight has invalid HTTP status code 401
I have looked for the solutions for similar problems in stack overflow but none of them fixed my problem.Could someone please help on this?