1
Hello, I have a backend made with Spring and I Gero a file . jar and 'raise' it via Vscode to the front made in Angular. When I try to access my page (everything is localhost), I get this return:
ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":401,"statusText":"OK","url":"http://localhost:8080/lancamento?resumo","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://localhost:8080/lancamento?resumo: 401 OK","error":{"timestamp":"2019-01-10","status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/lancamento"}}
My code in Angular is that way:
constructor(private http: HttpClient) { }
pesquisar(): Promise<any> {
const headers = new HttpHeaders();
headers.append('Authorization', 'Basic YWRtaW5AYWxnYW1vbmV5LmNvbTphZG1pbg==');
return this.http.get(`${this.lancamentoUrl}?resumo`, { headers}).toPromise()
.then(response => {
console.log(response);
});
}
My CORS code is like this:
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
response.setHeader("Access-Control-Allow-Origin", ApiProperty.getOriginPermitida());
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT, OPTIONS");
response.setHeader("Access-Control-Allow-Headers", "Authorization, Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
response.setHeader("Access-Control-Max-Age", "3600");
// response.setStatus(HttpServletResponse.SC_OK);
//
if ("OPTIONS".equals(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, resp);
}
}
My origin Ermitida is like this:
private String originPermitida = "*";
My Httpsecurity code looks like this:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.and()
.httpBasic()
.and()
.antMatcher("/**")
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable();
}
In Postman, it opens which is a beauty, the error happens when I open the page via Rowse.
Where am I going wrong? Could you give me a north?
Thank you!
What kind of security is using, Http Basic, Oauth ?
– Viktor Hugo
I’m using Basic anyway.
– Victor Freidinger
I was able to perform a debug now and checked that at the moment OPTIONS is done in CORS (This bit here: if ("OPTIONS".equals(request.getMethod())), the return of this is coming as GET. From what I got of explanation, OPTIONS would be a 'question to Browse' if you can send some things, even before sending. Would be skipping some stage ? The strange thing is that via Postman does not give the problem.
– Victor Freidinger
I couldn’t evolve in this problem. @Viktorhugo can help?
– Victor Freidinger