16 Ekim 2020 Cuma

SpringSession JDBC

Giriş
Eğer uygulamamızı ölçeklendirmek istiyorsak yani bir kaç instance daha eklemek istiyorsak karşımıza bazı problemler çıkıyor. Bu problemlerden birisi session. Açıklaması şöyle
What could prevent us from just launching a few more instances?
1. Using schedulers
2. Using WebSockets
3. User sessions stored in memory
4. Application cache — it can be either simple concurrent hashmaps in components or spring cache.

All of this can be easily adapted using the right tools.
Önce SpringSession vs SpringBoot Security Session arasındaki farka bakmak lazım. Açıklaması şöyle. Yani daha yeni bir proje olan SpringSession'ı tercih etmek lazım. SpringSession ile session bilgisi cluster'da saklanabilir. Bu cluster JDBC veya Redis olabilir
You can use "Spring Security" or "Spring Session" in order to have a Session Management in your boot project. Spring session is a rather new project ...
SpringSession Nedir?
Açıklaması şöyle. Bu projeyi kullanırsak yaparsak @EnableJdbcHttpSession anotasyonunu kullanmaya gerek yok. Sadece spring.session.store-type=jdbc yapmak yeterli.
Surprisingly, the only configuration property that we need to enable Spring Session backed by a relational database is in the application.properties:

spring.session.store-type=jdbc
Artık session bilgisi blob olarak saklanmaya başlar. Açıklaması şöyle
However, in this case, it is worth remembering that the session data itself will be stored as a blob, and sometimes, when updating Spring, you will have to clear sessions due to problems with deserializing old objects.

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.springframework.session</groupId>
  <artifactId>spring-session-jdbc</artifactId>
</dependency>
initialize-schema Alanı
Açıklaması şöyle
You are also instructing Spring Session to always create the schema with the option 
spring.session.jdbc.initialize-schema=always
Örnek
Şöyle yaparız.
spring.session.store-type=jdbc
# Database schema initialization mode.
spring.session.jdbc.initialize-schema=always
# Path to the SQL file to use to initialize the database schema.
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-mysql.sql
# Name of the database table used to store sessions.
spring.session.jdbc.table-name=SPRING_SESSION
spring.main.allow-bean-definition-overriding=true
Açıklaması şöyle
In MySQL, two tables are created Spring_session and Spring_session_attributes, upon successful authentication these tables will be filled automatically.
Django session bilgisini tek tabloda saklıyor. django_session tablosunun sütunları şöyle
session_key
session_data
expire_date

session_key is the key that clients are provided. Generally, clients making a request will include the session_key as part of a cookie. When the webserver receives the request, it finds session_key if it exists, then queries to see if the key is known. If it is, it will then look at the correlated session_data and retrieve metadata about the user and their session

Hiç yorum yok:

Yorum Gönder