java - 与 Redis 数据库共享 Spring Session
问题描述
以下场景:
描述:
我用 Spring 开发了两个微服务。一项服务是身份验证服务并生成会话。另一个服务是 ui-service,它需要知道请求的客户端是否被授权。经过一番阅读,我发现与 redis 共享会话似乎是一个很好的解决方案。(如果有更好的方法,请纠正我)对于编码,我遵循了这里的示例
现状: 我的身份验证服务工作并在redis中生成一个会话:
例如:
127.0.0.1:6379> keys *
1) "spring:session:sessions:a4c11990-94a4-4b99-bc77-33f2084e5e8f"
2) "spring:session:sessions:expires:a4c11990-94a4-4b99-bc77-33f2084e5e8f"
3) "spring:session:sessions:26f24541-74dd-4410-84ac-d051a64d1263"
4) "spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:userA"
5) "spring:session:expirations:1557314160000"
6) "spring:session:sessions:expires:26f24541-74dd-4410-84ac-d051a64d1263"
现在我想调用我的 ui 服务的测试端点。如果会话有效,则此调用应该成功,并且当没有有效会话时访问被拒绝。Atm 我总是在我的 ui 服务中收到拒绝访问错误。
代码: 我的身份验证服务:
应用程序属性
spring.session.store-type=redis
spring.session.redis.flush-mode=on-save
spring.session.redis.namespace=spring:session
spring.redis.host=localhost
spring.redis.port=6379
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
会话配置
@Configuration
@EnableRedisHttpSession
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public HttpSessionIdResolver httpSessionIdResolver() {
CookieHttpSessionIdResolver resolver = new CookieHttpSessionIdResolver();
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
cookieSerializer.setUseBase64Encoding(false);
resolver.setCookieSerializer(cookieSerializer);
return resolver;
}
在我的主要课程中,我做到了
@EnableRedisHttpSession
在我的 UI 服务中:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>Redistest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Redistest</name>
<description>Microservice for Data Management</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主要的:
@EnableWebSecurity
@SpringBootApplication
public class RedistestApplication {
public static void main(String[] args) {
SpringApplication.run(RedistestApplication.class, args);
}
会话配置:
@Configuration
@EnableRedisHttpSession
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public HttpSessionIdResolver httpSessionIdResolver() {
return HeaderHttpSessionIdResolver.xAuthToken();
}
网络安全:
@Configuration
public class WebSecurity extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated();
}
}
}
控制器
@RestController
public class TestController {
@GetMapping("/name")
public String getName() {
return "Hallo From Test";
}
}
问题: 我的服务没有任何错误。看来我只是无法使用 reddis 的会话。在教程中,作者说 cookie 中 session-id 的 base64 编码可能存在问题。我想这可能是问题所在。您有什么建议或在我的实施中看到任何错误吗?我必须明确禁用 HttpSession 吗?感谢您的帮助/提示。
编辑: 我刚刚认识到在我的浏览器检查(Firefox)中我没有任何“x-auth”字段。我的意图是将身份验证 ID(会话 ID)保存在标头中,而不是作为 cookie。所以我希望我的身份验证服务应该将 sessionid 设置为标头中的 x-auth 字段。那是对的吗?所以错误似乎来自身份验证服务?
解决方案
尝试在 SessionConfig 类中将 cookieSerializer.setUseBase64Encoding(false) 设置为 true。在 spring-boot 应用程序中的 spring-boot.2.0 会话之后必须进行编码。
推荐阅读
- c# - Visual Studio 2017 开始有/没有调试性能差异
- c# - XAML 视觉层似乎无法为 Size 目标属性设置动画
- oracle - 编译函数时出现“PLS-00049:绑定变量错误”
- unit-testing - 连接被远程主机在接收时强行关闭
- javascript - 太长的字符串不能用?
- python - 过滤后将新列添加到熊猫 df
- javascript - 将表值从其他表移动并返回
- html - 'string[]' 类型不存在属性 'featured'
- swift - 在 swift 4 中转换 Objective C 代码
- python - 如何用 sphinx 记录可链接常量