Мой код на стороне сервера ..
public class SimpleCorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST,GET,DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("X-Frame-Options", "*");
response.setHeader("Access-Control-Allow-Headers", "content-type,access-control-request-headers,access-control-request-method,accept,origin,authorization,x-requested-with");
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, res);
}
}
@Override
public void destroy() {
}
}
Код моей клиентской стороны ...
viewUsers(): Observable<any> {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Access-Control-Allow-Origin', '*')
headers.append('Accept', 'application/json');
headers.append('Authorization', 'Basic dXNlcjpzdXJpeWFu');
let options = new RequestOptions({ headers: headers });
return this.http.get(myurl, options).map(getJson);
}
Пожалуйста, помогите мне решить эту проблему ...
Я получаю ошибку выше и ниже: не удалось загрузить http://10.100.8.58:8080/user/view: Ответ на предпечатную проверку имеет недопустимый код состояния HTTP 401. Я не знаю, где я допустил ошибку.
МОЙ сервер на стороне securityConfif CLass is ..
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("suriyan").roles("USER").and().withUser("admin")
.password("suriyans").roles("USER", "ADMIN");
}
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().authorizeRequests().antMatchers("/user/**").hasRole("USER").antMatchers("/**")
.hasRole("ADMIN").and().csrf().disable().headers().frameOptions().disable();
}
}
Мой почтовый индекс: ..
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://10.100.8.58:8080/user/view")
.get()
.addHeader("authorization", "Basic dXNlcjpzdXJpeWFu")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "48b3ca10-fe72-4a1e-ce00-44c78afb636a")
.build();
Response response = client.newCall(request).execute();