Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导-如何使用cors和Spring安全性修复会话bean_Java_Spring_Spring Boot_Spring Security_Cors - Fatal编程技术网

Java Spring引导-如何使用cors和Spring安全性修复会话bean

Java Spring引导-如何使用cors和Spring安全性修复会话bean,java,spring,spring-boot,spring-security,cors,Java,Spring,Spring Boot,Spring Security,Cors,TL;博士 每次localhost:4200通过cors筛选器向localhost:8080发出http请求时,它都会丢失sessionscope bean,该bean保存身份验证,这基本上使它无法通过403的所有调用。不包括第一个http请求,该请求不在spring security之后 我有一个spring引导应用程序,它在localhost:8080中运行良好。 我们正在其内部创建一个角度iframe,当部署在localhost:8080上时,它也能很好地工作 但当我们在本地主机上执行此操

TL;博士 每次localhost:4200通过cors筛选器向localhost:8080发出http请求时,它都会丢失sessionscope bean,该bean保存身份验证,这基本上使它无法通过403的所有调用。不包括第一个http请求,该请求不在spring security之后

我有一个spring引导应用程序,它在localhost:8080中运行良好。 我们正在其内部创建一个角度iframe,当部署在localhost:8080上时,它也能很好地工作

但当我们在本地主机上执行此操作时:4200 ng serve 这行不通

它开始抱怨cors,所以我有以下配置,除了我添加的关于cors的所有配置

@Configuration
@Profile({"dev"})
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class springDevConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception{
    http.csrf().disable();
    http.headers().frameOptions().sameOrigin();
    http.headers().cacheControl().disable();
    http.cors().configurationSource(corsConfigurationSource())
    .and().authorizeRequests().anyRequest().permitAll();
  }

  @Bean
  public CorsConfigurationSource corsConfigurationSource(){
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

    CorsConfiguration configuration = new CorsConfiguration();

    configuration.setAllowedOrigins(
    ImmutableList.of("http://localhost:4200"));

    configuration.setAllowedMethods(Arrays.asList(
    RequestMethod.GET.name(),
    RequestMethod.POST.name(),
    RequestMethod.OPTIONS.name(),
    RequestMethod.DELETE.name(),
    RequestMethod.PUT.name()));

    source.registerCorsConfiguration("/**", configuration);

    return source;
  }
}
我的会话bean相当简单

@Component
@SessionScope
public class UserSession {
   //Holds the Authorities for the prePostEnabled
   User user; 
   ...
}
为了初始化用户,我向某个未受保护的端点发出请求,并在代码中执行类似的操作

...
User user = new User(id, "", authorities);

Authentication auth = new UsernamePasswordAuthenticationToken(
user, null,authorities));

SecurityContextHolder.getContext().setAuthentication(auth);

Usersession.setUser(user);
...
当我在localhost:8080上发出http请求时,后续的http请求具有相同的会话

但是,当我尝试从localhost:4200向localhost:8080发出请求时,每个请求似乎都会获取不同的UserSession/或者打开一个新会话?在所有受保护的端点上给我403

到底发生了什么?为什么localhost:4200在向localhost:8080发出请求时,每次调用都要进行新会话?为解决此问题,应更改哪些配置

附录1:如果我发表评论

@EnableGlobalMethodSecurity(prePostEnabled = true)
代码在localhost:4200上运行良好,我的意思是他不再有403个代码,但很可能在每个请求中仍在使用另一个会话范围bean

附录2: 现在可以用了
我所要做的就是将ssl放在ng serve配置中,该配置在localhost:8080上,而不是4200上,JSessionId开始工作

你能再展示一下你是如何嵌入iframe的,以及什么服务于原始页面的吗

似乎正在发生的是,您实际上是在未意识到的情况下提出跨域请求。8080和4200是不同的域,如果页面的一部分从一个域加载,而另一部分(即iframe)从另一个域加载,则构成跨域请求,并且一个域发送的cookie不会应用于另一个域的请求,因此会话范围不共享。此外,如果您向加载原始页面的域以外的域发出Ajax请求,则默认情况下,除非您设置CORS,否则这些请求将被拒绝。这就解释了你为什么要这么做

您必须确保包含的页面iFrame和Ajax请求的所有部分都是从同一个域加载的,或者您有其他方法将会话附加到请求。通常,JSESSIONID cookie用于此目的。此cookie是否在初始响应中发送


您通常有一个应用程序服务于前端,包括初始页面,例如4200上的应用程序,以及一个仅响应API调用的应用程序,例如8080上配置CORS的应用程序。这样,所有对后端的调用都是通过同一个域完成的,Cookie可以正常应用。

Hello Kaqqao,据我所知,我们既不使用localhost:8080,也不使用localhost:80804200@ioliano修改了答案,卡卡,你好像在做什么!在localhost:8080中,jsessionid不知何故存在于iframe调用中,但当我从localhost:4200发出请求时,似乎缺少这个cookie!但是我还没有做任何配置来在localhost:8080上设置这个cookie。所以我需要找到一种方法来获取JSSessionID,或者在我不知道如何获取的基础上构建它,你有什么建议吗?@ioliano JSSessionID是自动生成的,除非你明确禁用它。不要让事情变得不必要的复杂。如果要共享会话,请从同一域提供页面和iframe。后端可以位于不同的域上,没有问题,只需配置CORS即可。在您帮助我查找问题时,给您接受的答案