Angular2与Sping Boot 和Spring Security

ndh0cuux  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(177)

首先,我已经在this page上检查了这个问题,我尝试了他的解决方案,但最后,我仍然有同样的问题。
XMLHttpRequest无法加载http://localhost:8080/login。对预处理请求的响应未通过访问控制检查:请求的资源上不存在“Excell-Control-Allow-Origin”标头。因此不允许访问Origin http://localhost:3000。响应的HTTP状态代码为403。
但是,我把一个access-control到处所以我不明白为什么它是这样的。
我的代码看起来像这样(我希望我能为你写得足够多):
在Angular中,我的login.service.ts

  1. check(name: string, password: string): boolean {
  2. let headers = new Headers();
  3. headers.append('Content-Type', 'application/x-www-form-urlencoded');
  4. headers.append('Access-Control-Allow-Origin','*');
  5. let options = new RequestOptions({headers:headers,withCredentials:true});
  6. if(this.http.post(this.baseUrl,
  7. `username=${name}&password=${password}`,
  8. {headers:headers})
  9. .toPromise().then(response=> {
  10. return {}
  11. }))
  12. return true;
  13. return false;
  14. }

字符串
如果认证成功,我还想返回一个布尔值,但我真的不知道如何知道它是否有效,所以我现在这样做(它总是为真)。
在Java中,我有这样的代码:

  1. @Configuration
  2. @EnableWebMvc
  3. class WebConfig extends WebMvcConfigurerAdapter {
  4. }


为了安全起见,我买了这个

  1. @Configuration
  2. @EnableWebSecurity
  3. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  4. @Autowired
  5. private RESTLoginSuccessHandler loginSuccessHandler;
  6. @Autowired
  7. private RestLogoutSuccessHandler logoutSuccessHandler;
  8. @Override
  9. protected void configure(HttpSecurity httpSecurity) throws Exception {
  10. //deactivate CSRF and use custom impl for CORS
  11. httpSecurity
  12. .cors.and()
  13. .csrf().disable()
  14. .addFilterBefore(new CorsFilter(), ChannelProcessingFilter.class);
  15. //authorize, authenticate rest
  16. httpSecurity
  17. .authorizeRequests()
  18. .anyRequest().hasRole("USER")
  19. .and()
  20. .sessionManagement()
  21. .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
  22. .and()
  23. .formLogin()
  24. .usernameParameter("username")
  25. .passwordParameter("password")
  26. .loginPage("/login")
  27. .successHandler(loginSuccessHandler)
  28. .permitAll()
  29. .and()
  30. .logout()
  31. .logoutSuccessHandler(this.logoutSuccessHandler)
  32. .permitAll();
  33. }
  34. @Autowired
  35. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  36. auth.inMemoryAuthentication().withUser("rano").password("1234").roles("USER");
  37. auth.inMemoryAuthentication().withUser("admin").password("admin").roles("USER", "ADMIN");
  38. }
  39. }
  40. @Bean
  41. CorsConfigurationSource corsConfigurationSource() {
  42. CorsConfiguration configuration = new CorsConfiguration();
  43. configuration.setAllowedOrigins(Arrays.asList("http://localhost:8080","http://localhost:3000"));
  44. configuration.setAllowedMethods(Arrays.asList("PUT","DELETE","POST"));
  45. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  46. source.registerCorsConfiguration("/**", configuration);
  47. return source;
  48. }


在我的登录页面中:

  1. @Component
  2. public class RESTLoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
  3. private RequestCache requestCache = new HttpSessionRequestCache();
  4. @Override
  5. public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
  6. org.springframework.security.core.Authentication authentication) throws IOException, ServletException {
  7. SavedRequest savedRequest = requestCache.getRequest(request, response);
  8. if (savedRequest == null) {
  9. clearAuthenticationAttributes(request);
  10. return;
  11. }
  12. String targetUrlParam = getTargetUrlParameter();
  13. if (isAlwaysUseDefaultTargetUrl()
  14. || (targetUrlParam != null && StringUtils.hasText(request.getParameter(targetUrlParam)))) {
  15. requestCache.removeRequest(request, response);
  16. clearAuthenticationAttributes(request);
  17. return;
  18. }
  19. clearAuthenticationAttributes(request);
  20. }
  21. public void setRequestCache(RequestCache requestCache) {
  22. this.requestCache = requestCache;
  23. }
  24. }


那么,有什么问题吗?或者如何使用Sping Boot 和Spring Security制作Angular2应用程序?因为除了添加安全性之外,Sping Boot 和Angular之间的所有功能都可以正常工作。

des4xlb0

des4xlb01#

将以下内容添加到configure方法中

  1. .cors().and()

字符串

相关问题