Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. XMLHttpRequest cannot load http://localhost:8080/login.
  2. Response to preflight request doesn't pass access control check:
  3. No 'Access-Control-Allow-Origin' header is present on the requested resource.
  4. Origin 'http://localhost:3000' is therefore not allowed access.
  5. The response had HTTP status code 403.
  6.  
  7. check(name: string, password: string): boolean {
  8. let headers = new Headers();
  9. headers.append('Content-Type', 'application/x-www-form-urlencoded');
  10. headers.append('Access-Control-Allow-Origin','*');
  11. let options = new RequestOptions({headers:headers,withCredentials:true});
  12.  
  13. if(this.http.post(this.baseUrl,
  14. `username=${name}&password=${password}`,
  15. {headers:headers})
  16. .toPromise().then(response=> {
  17. return {}
  18. }))
  19. return true;
  20.  
  21. return false;
  22. }
  23.  
  24. @Configuration
  25. @EnableWebMvc
  26. class WebConfig extends WebMvcConfigurerAdapter {
  27.  
  28. @Override
  29. public void addCorsMappings(CorsRegistry registry) {
  30. registry.addMapping("/**")
  31. .allowedOrigins("http://localhost:8080","http://localhost:3000")
  32. .allowedMethods("PUT","DELETE","POST")
  33. .allowedHeaders("header1", "header2", "header3")
  34. .exposedHeaders("header1", "header2");
  35. }
  36.  
  37. @Override
  38. public void addViewControllers(ViewControllerRegistry registry) {
  39. registry.addViewController("/login").setViewName("login");
  40. }
  41. }
  42.  
  43. @Configuration
  44. @EnableWebSecurity
  45. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  46.  
  47.  
  48. @Autowired
  49. private RESTLoginSuccessHandler loginSuccessHandler;
  50.  
  51. @Autowired
  52. private RestLogoutSuccessHandler logoutSuccessHandler;
  53.  
  54. @Override
  55. protected void configure(HttpSecurity httpSecurity) throws Exception {
  56. //deactivate CSRF and use custom impl for CORS
  57. httpSecurity
  58. .csrf().disable()
  59. .addFilterBefore(new CorsFilter(), ChannelProcessingFilter.class);
  60. //authorize, authenticate rest
  61. httpSecurity
  62. .authorizeRequests()
  63. .anyRequest().hasRole("USER")
  64. .and()
  65. .sessionManagement()
  66. .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
  67. .and()
  68. .formLogin().usernameParameter("username").passwordParameter("password").loginPage("/login").successHandler(loginSuccessHandler).permitAll()
  69. .and().logout().logoutSuccessHandler(this.logoutSuccessHandler).permitAll();
  70. }
  71.  
  72. @Autowired
  73. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  74. auth.inMemoryAuthentication().withUser("rano").password("1234").roles("USER");
  75. auth.inMemoryAuthentication().withUser("admin").password("admin").roles("USER", "ADMIN");
  76. }
  77. }
  78.  
  79. @Override
  80. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
  81.  
  82. HttpServletRequest req = (HttpServletRequest) servletRequest;
  83. HttpServletResponse resp = (HttpServletResponse) servletResponse;
  84.  
  85. String origin = req.getHeader("Origin");
  86. if (origin != null && origin.matches(".*")) {
  87. resp.addHeader("Access-Control-Allow-Origin", origin);
  88. if ("options".equalsIgnoreCase(req.getMethod())) {
  89. resp.setHeader("Allow", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS");
  90. if (origin != null) {
  91. String headers = req.getHeader("Access-Control-Request-Headers");
  92. String method = req.getHeader("Access-Control-Request-Method");
  93. resp.addHeader("Access-Control-Allow-Methods", method);
  94. resp.addHeader("Access-Control-Allow-Headers", headers);
  95. // optional, only needed if you want to allow cookies.
  96. resp.addHeader("Access-Control-Allow-Credentials", "true");
  97. }
  98. resp.getWriter().flush();
  99. return;
  100. }
  101. else {
  102. resp.addHeader("Access-Control-Allow-Credentials", "true");
  103. resp.setContentType("application/json");
  104. }
  105. }
  106.  
  107. // Fix ios6 caching post requests
  108. if ("post".equalsIgnoreCase(req.getMethod())) {
  109. resp.addHeader("Cache-Control", "no-cache");
  110. }
  111.  
  112. if (chain != null) {
  113. chain.doFilter(req, resp);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement