我正在尝试运行此Spring会话示例,但是,我使用Maven而不是Gradle.我在以下情况下得到以下错误:a)运行WAR文件和b)在WildFly8.2服务器中部署WAR
快速搜索表明问题可能是pom.xml中引用的各种依赖项之间存在冲突的版本.
这个错误的根本原因和解决方案是什么?
运行或部署WAR时出错
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.boot.SpringApplicationRunListener : org.springframework.boot.context.event.EventPublishingRunListener
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:381)
at org.springframework.boot.SpringApplication.getRunListeners(SpringApplication.java:352)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:274)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at hello.Application.main(Application.java:25)
... 6 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:377)
... 11 more
Caused by: java.lang.NoSuchFieldError: INSTANCE
at org.springframework.boot.SpringApplication.asUnmodifiableOrderedSet(SpringApplication.java:1031)
at org.springframework.boot.SpringApplication.getListeners(SpringApplication.java:930)
at org.springframework.boot.context.event.EventPublishingRunListener.<init>(EventPublishingRunListener.java:47)
... 16 more
Run Code Online (Sandbox Code Playgroud)
这是我完整的POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" …Run Code Online (Sandbox Code Playgroud) 我们用我们自己的 User 对象扩展了主体。这样,该对象对于每个请求都是可用的。当用户更新其信息时,主体需要使用这些新数据进行更新。当不使用 spring-session 时,此方法有效。然而,对于 spring-session 来说,情况并非如此。
我检查了 spring-session 代码,以及RedisOperationsSessionRepository:save(RedisSession session)唯一的调用session.saveDelta(),它仅保存更改的属性。那么,我们如何在会话中更新主体呢?
注意 - 更新主体的地方位于服务层,因此我们无权访问SessionAuthenticationStrategy.
我想在没有 Redis 的情况下使用 spring-boot + spring-session 但使用 dynamodb 作为 sessionRepository 实现。
所有可用的示例都与 Redis 或 Hazelcast 紧密耦合,并且主要是自动配置,抽象出正在初始化的 bean。此外,我的 spring boot 配置明确定义了一个
@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory(Environment env) {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
return factory;
}
Run Code Online (Sandbox Code Playgroud)
我还禁用了SessionAutoConfiguration.classspring-session的 spring-boot 自动配置。
所以我有几个问题。
1. 如何配置具有明确定义的 TomcatEmbeddedServletContainerFactory bean 的 spring-boot 项目以使用 spring-session?
2. 我注意到 spring-session 与 Redis 和 Hazelcast 紧密结合(仅此而已)。对于会话存储库 impl 使用像 amazon dynamodb 这样的商店是否有任何反对意见?
让我了解如何配置我想要实现的目标,但我一直遇到初始化异常。如果有人能指出正确的方向,我将不胜感激。
使用 spring-session 版本:1.1.0.M1
我必须能够重命名默认的 Spring Session 表,并在spring 会话文档中找到以下配置选项。
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # 用于初始化数据库模式的 SQL 文件的路径。spring.session.jdbc.table-name=SPRING_SESSION #用于存储会话的数据库表的名称。
这是我试图在 application.properties 中设置的内容:
spring.session.store-type=jdbc
spring.session.jdbc.table-name: APP_SPR_SESSION
spring.session.jdbc.schema: src/main/resources/appSpringSession.sql
Run Code Online (Sandbox Code Playgroud)
以下是 appSpringSession.sql 的内容,它是schema-oracle.sql的修改/重命名版本
CREATE TABLE app_app.APP_SPR_SESSION (
PRIMARY_ID CHAR(36) NOT NULL,
SESSION_ID CHAR(36) NOT NULL,
CREATION_TIME NUMBER(19,0) NOT NULL,
LAST_ACCESS_TIME NUMBER(19,0) NOT NULL,
MAX_INACTIVE_INTERVAL NUMBER(10,0) NOT NULL,
EXPIRY_TIME NUMBER(19,0) NOT NULL,
PRINCIPAL_NAME VARCHAR2(100 CHAR),
CONSTRAINT APP_SPR_SESSION_PK PRIMARY KEY (PRIMARY_ID)
);
CREATE UNIQUE INDEX APP_SPR_SESSION_IX1 ON APP_SPR_SESSION (SESSION_ID);
CREATE INDEX APP_SPR_SESSION_IX2 ON APP_SPR_SESSION (EXPIRY_TIME);
CREATE INDEX APP_SPR_SESSION_IX3 ON APP_SPR_SESSION …Run Code Online (Sandbox Code Playgroud) 我想使用 Redis 来操作会话。但是我在运行 spring boot 应用程序时失败了。所以我猜这个错误来自 maven 依赖项,尤其是版本冲突。
这是我的 Maven 依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
我试图通过在 pom.xml 中集成嵌入式 redis 依赖项来避免通知键空间事件错误,但没有成功。
注意,我在上面的 pom.xml 中添加了两个依赖,其中 artifactId 是 spring-session-data-redis 和 …
我精确地说,我是Java Developper一年级的法国学生。
我正在使用以下程序开发一个多模块应用程序:Spring Boot,Spring安全性,Hibernate,Spring Data,Spring MVC和Thymeleaf。
我想在登录时在会话中设置User,或者至少在userId中设置。这样,我不必每次需要时都将其手动放入会话或模型中。
但是当我使用默认的Spring Security登录和身份验证配置时,我真的不知道如何或在何处调用这种方法:
void putUserInHttpSession( HttpSession httpSession ) {
httpSession.setAttribute( "user" , getManagerFactory().getUserManager().findByUserName( SecurityContextHolder.getContext().getAuthentication().getName()) );
}
Run Code Online (Sandbox Code Playgroud)
我可以在需要的时候做到这一点,但我发现很难做到不只是在登录时这样做!
这是我认为您可能需要帮助的内容(那真是太棒了!!! :)
我的WebSecurityConfig类:
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Autowired
private DataSource dataSource;
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// Setting Service to find User in the database.
// And Setting PassswordEncoder
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure( HttpSecurity http ) …Run Code Online (Sandbox Code Playgroud) 我的应用程序在没有 spring boot 的情况下使用 spring web mvc 框架运行。现在我想使用 spring session JDBC 将会话存储到应用程序使用的数据库中。我在网上找到的所有例子都是使用spring boot的,如果不使用spring boot,他们使用的数据源配置是EmbeddedDatabase这样的:
@Bean
public EmbeddedDatabase dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("org/springframework/session/jdbc/schema-h2.sql").build();
}
Run Code Online (Sandbox Code Playgroud)
我有使用 HikariCP 的数据源配置,我希望 spring 会话使用此数据源配置。
@Bean
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(env.getRequiredProperty("jdbc.driver"));
config.setJdbcUrl(env.getRequiredProperty("jdbc.url"));
config.setUsername(env.getRequiredProperty("jdbc.username"));
config.setPassword(env.getRequiredProperty("jdbc.password"));
config.setMinimumIdle(env.getRequiredProperty("jdbc.pool.minimumIdle", Integer.class));
config.setMaximumPoolSize(env.getRequiredProperty("jdbc.pool.maximumPoolSize", Integer.class));
config.addDataSourceProperty("cachePrepStmts", env.getRequiredProperty("jdbc.prop.cachePrepStmts"));
config.addDataSourceProperty("prepStmtCacheSize", env.getRequiredProperty("jdbc.prop.prepStmtCacheSize"));
config.addDataSourceProperty("prepStmtCacheSqlLimit", env.getRequiredProperty("jdbc.prop.prepStmtCacheSqlLimit"));
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
Run Code Online (Sandbox Code Playgroud)
如何使用我当前的配置与 spring 会话集成?
Redis是Java的Spring会话存储的默认实现。但是Redis是内存中的值键存储,迟早它会耗尽内存。一旦完成,它将执行以下操作之一:
maxmemory-policy)中设置的策略释放内存。我看到Redis中有一些称为“ 虚拟内存”的功能,但是该功能已经停止使用,并且从2.4版开始,该功能不可用。
当Redis内存不足时,Redis能够使用硬盘作为备份存储吗,或者对于会话存储来说,这确实是一个糟糕的选择吗?还是Redis依赖OS将swap作为最后的手段?
我正在使用Redis 3和Spring Session 1.1.1
我正在开发一个使用 Spring Session JDBC 的应用程序。我也将 Spring JPA 用于其他实体。我的问题是,如何配置 Spring Boot 应用程序以允许将单独的数据库用于会话存储?
我已经引用了这个问题,但答案中提到的 JdbcHttpSessionConfiguration 构造函数似乎不再有效(我使用的是 Spring Boot 2.1.1)。除此之外,我找不到有关该主题的任何文档。我找到了有关如何使用 JDBC 支持配置 Spring Session 以及如何在 Spring 中使用多个数据源的信息,但没有找到有关如何将两者结合的信息。我认为它可能涉及扩展JdbcHttpSessionConfiguration,但不幸的是,我无法弄清楚如何正确地这样做。
这是我迄今为止的全部内容:
@Configuration
class SessionConfig extends JdbcHttpSessionConfiguration {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,上述尝试也在 H2 存储中创建我的所有实体表。
我的主数据源 (PostgreSQL) 在我的application.properties.
spring.session.store-type=jdbc
spring.datasource.url=jdbc:postgresql://localhost/auth
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
Run Code Online (Sandbox Code Playgroud)
感谢您的任何指导。
我正在尝试使用 Rest API 和 React 作为前端来实现 Spring Security,因为这是我的第一个全栈开发项目,我对如何实现正确的身份验证机制一无所知。
我搜索了很多,找到了关于 Spring Security with Basic Auth 的文章,但我无法弄清楚如何将该身份验证转换为 Rest api,然后通过会话/cookie 进行相同的管理。即使我得到的任何 github 参考资料都非常旧,或者它们还没有完全迁移到 Spring Security 5。
因此无法找出保护 REST API 安全的正确方法。(只是spring security,spring security + jwt,spring security + jwt + spring session + cookie)
编辑
来自数据库的用户名验证
@Component
CustomUserDetailsService -> loadUserByUsername -> Mongo Db
Run Code Online (Sandbox Code Playgroud)
通行加密
@Bean
public PasswordEncoder passwordEncoder() { ... }
Run Code Online (Sandbox Code Playgroud)
跨源
@Bean
public WebMvcConfigurer corsConfigurer() { ... }
Run Code Online (Sandbox Code Playgroud)
注册控制器
@RestController
public class RegistrationController {
@PostMapping("/registration")
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public ResponseEntity registerUserAccount(... ) { ... } …Run Code Online (Sandbox Code Playgroud) spring-session ×10
spring-boot ×6
java ×5
spring ×4
maven ×2
redis ×2
oracle ×1
spring-jdbc ×1
spring-mvc ×1
spring-rest ×1