我们添加Spring Security到现有项目中.
从这一刻起,我们No 'Access-Control-Allow-Origin' header is present on the requested resource从服务器收到401 错误.
那是因为没有Access-Control-Allow-Origin标题附加到响应.为了解决这个问题,我们Filter在注销过滤器之前添加了我们自己的过滤器,但过滤器不适用于我们的请求.
我们的错误:
XMLHttpRequest无法加载
http://localhost:8080/getKunden.请求的资源上不存在"Access-Control-Allow-Origin"标头.http://localhost:3000因此不允许原点访问.响应具有HTTP状态代码401.
我们的安全配置:
@EnableWebSecurity
@Configuration
@ComponentScan("com.company.praktikant")
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private MyFilter filter;
@Override
public void configure(HttpSecurity http) throws Exception {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
source.registerCorsConfiguration("/**", config);
http.addFilterBefore(new MyFilter(), LogoutFilter.class).authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/*").permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws …Run Code Online (Sandbox Code Playgroud) 后端在 docker 容器中运行。它与弹簧靴和安全性一起运行并且工作正常。前端也在另一个容器中运行。一切正常,直到突然抛出此异常。我只是在闲置几分钟后才注意到这个异常(通常在 5 到 10 分钟之间)。
警告 1 --- [nio-8080-exec-2] ohengine.jdbc.spi.SqlExceptionHelper:SQL 错误:0,SQLState:08S01
错误 1 --- [nio-8080-exec-2] ohengine.jdbc.spi。 SqlExceptionHelper : I/O 错误: 连接重置
错误 1 --- [nio-8080-exec-2] oaccC[.[.[/].[dispatcherServlet] : Servlet [dispatcherServlet] 的 Servlet.service() 与路径上下文[] 抛出异常 [请求处理失败;嵌套异常是 org.springframework.dao.DataAccessResourceFailureException: could not extract ResultSet; 嵌套异常是 org.hibernate.exception.JDBCConnectionException: could not extract ResultSet] with root cause
java.net.SocketException:连接重置在 java.net.SocketInputStream.read(SocketInputStream.java:209) ~[na:1.8.0_111-internal] 在 java.net.SocketInputStream.read(SocketInputStream.java:141) ~[ na:1.8.0_111-internal] 在 java.io.DataInputStream.readFully(DataInputStream.java:195) ~[na:1.8.0_111-internal]
.....
与数据库的连接始终可用。奇怪的是,只有当我使用 docker 运行我的项目时才会抛出这个错误。如果我使用 eclipse 和 node.js 运行它,则不会发生错误。我唯一注意到的是,在我登录并闲置几分钟后,当我尝试做某事时会自动退出。我不知道这是否与原始错误有任何关系。
应用程序属性
spring.datasource.url=databaseURL
spring.datasource.username=SQLusername
spring.datasource.password=Password
spring.datasource.driver-class-name=net.sourceforge.jtds.jdbc.Driver
spring.datasource.initialSize=100
spring.datasource.minIdle=10
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.datasource.tomcat.max-active=250
spring.datasource.tomcat.max-wait=30000 …Run Code Online (Sandbox Code Playgroud)