Skip to content

六 3、Session 配置

ZeroOrInfinity edited this page Dec 17, 2020 · 4 revisions

所属模块: core 模块

简单配置:

spring:
  session:
    # session 存储模式设置, 要导入相应的 spring-session 类的依赖, 默认为 none, 分布式服务应用把 session 放入 redis 等中间件
    store-type: none
    # session 过期时间
    timeout: PT300s

详细配置: demo 模块 -> session-detail-example

spring:
  profiles:
    active: dev
  # mysql
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/ums?useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
  # thymeleaf
  thymeleaf:
    encoding: utf-8
    prefix: classpath:/templates/
    suffix: .htm
    servlet:
      content-type: text/html;charset=UTF-8
  # jackson
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

# ===============start session 详细设置 ===============
  redis:
    host: 192.168.88.88
    port: 6379
    password:
    database: 0
    # 连接超时的时间
    timeout: 10000
    # redis-lettuce-pool
    lettuce:
      # 会影响应用关闭是时间, dev 模式设置为 0
      shutdown-timeout: PT500S
      pool:
        max-active: 8
        max-wait: PT10S
        max-idle: 8
        min-idle: 1
  session:
    # session 存储模式设置, 要导入相应的 spring-session 类的依赖, 默认为 none, 分布式服务应用把 session 放入 redis 等中间件
    store-type: redis
    # session 过期时间
    timeout: PT600s
    # session redis 缓存设置
    redis:
      # redis 刷新模式
      flush-mode: on_save
      # redis 命名空间
      namespace: spring:session
      # Cron expression for expired session cleanup job
      cleanup-cron: 5 * * * * *


server:
  port: 9090
  servlet:
    # tomcat session 设置
    session:
      timeout: PT600S
      cookie:
        max-age: PT600S
        # session 的 cookie name, 默认为: JSESSIONID
        name: SID
# ===============end session 详细设置 ===============

ums:
  oauth:
    # 是否支持第三方授权登录功能, 默认: 空, 必须明确配置是否支持
    enabled: false
  # RBAC 权限访问控制
  rbac:
    # ========= 放行权限 ==========
    # 权限表达式, 当 enableRestfulApi=true 且没有 @EnableGlobalMethodSecurity 注释时生效, 默认为 hasPermission(request, authentication).
    # hasPermission 表达式默认实现为 UriAuthoritiesPermissionEvaluator, 想自定义逻辑, 实现 PermissionEvaluator 即可替换.
    # String accessExp = "hasPermission(request, authentication)";
    # // 配置等效与
    # httpSecurity.authorizeRequests().anyRequest().access(hasPermission(request, authentication));
    restful-access-exp: permitAll()
    # 是否支持 restful Api (前后端交互接口的风格; 如: 查询(GET),添加(POST),修改(PUT),删除(DELETE)), 默认: true.
    # 当 {@code enableRestfulApi=false} 时 {@code accessExp} 权限表达式生效,
    # 当 {@code enableRestfulApi=true} 时 {@code restfulAccessExp} 权限表达式生效.
    enable-restful-api: true
  client:
    # 一级域名(不包括二级域名) 例如:
    #       domain: www.example.com -> topDomain: example.com
    #       domain: www.example.com.cn -> topDomain: example.com.cn
    #       domain: aaa.bbb.example.net -> topDomain: example.net
    # 测试时用的 IP 或 localhost 直接原样设置就行.
    # 在应用启动时通过 SecurityAutoConfiguration 自动注入 MvcUtil 字段 topDomain 中. 如在设置跨域 cookie 时可以通过 MvcUtil.getTopDomain() 方法获取.
    topDomain: localhost
    # 登录页
    login-page: /login
    # 登录失败页
    failure-url: /login
    # 设置登录后返回格式(REDIRECT 与 JSON): 默认 JSON
    login-process-type: redirect
    # 设置处理登录表单的 uri,不需要用户实现此 uri,由 Spring security 自动实现, 默认为 /authentication/form
    login-processing-url: /authentication/form
    # 登录成功页
    success-url: /
    # 设置登出 url, 默认为 /logout
    logout-url: /logout
    # 设置登出后跳转的 url, 默认为 /login
    logout-success-url: /login
    # 设置登录时用户名的 request 参数名称, 默认为 username
    usernameParameter: username
    # 设置登录时用户密码的 request 参数名称, 默认为 password
    passwordParameter: password
    # 登录后是否利用 Referer 进行跳转, 默认为: true
    useReferer: true
    # 设置由客户端决定认证成功要跳转的 url 的 request 参数名称, 默认为 redirectTargetUrl
    targetUrlParameter: redirectTargetUrl
    # 不需要认证的静态资源 urls, 例如: /resources/**, /static/**
    ignoring-urls:
      - /static/**
    # 不需要认证的 uri(可以带 HttpMethod 后缀; 用:隔开), 例如: /user/** 或 /user/**:post, 默认为 空 Set.
    permit-urls:
      - /**/*.html:GET
      - /login2:GET

    # =============== login routing 功能: 解决跳转登录成功后不能跳转原始请求的问题 ===============

    # 是否开启根据不同的uri跳转到相对应的登录页, 默认为: false, 当为 true 时还需要配置 loginUnAuthenticationRoutingUrl 和 authRedirectSuffixCondition
    open-authentication-redirect: true
    # 当请求需要身份认证时,默认跳转的url 会根据 authJumpSuffixCondition 条件判断的认证处理类型的 url,默认实现 /authentication/require,
    # 当 openAuthenticationRedirect = true 时生效.
    login-un-authentication-routing-url: /authentication/require
    # 设置 uri 相对应的跳转登录页, 例如:key=/**: value=/login.html, 用等号隔开key与value, 如: /**=/login.html, 默认为空.
    # 当 openAuthenticationRedirect = true 时生效.
    # 支持通配符, 匹配规则: /user/aa/bb/cc.html 匹配 pattern:/us?r/**/*.html, /user/**, /user/*/bb/c?.html, /user/**/*.*.
    # 规则具体看 AntPathMatcher.match(pattern, path)
    auth-redirect-suffix-condition:
      - '/hello=/login2'
      - '/user/**=/login'
      - '/order/**=/login'
      - '/file/**=/login'
      - '/social/**=/signIn.html'

    # =============== session 详细设置 ===============
    # session 设置
    session:
      session-creation-policy: always
      # 当为 false 时允许单个用户拥有任意数量的 session(不同设备或不同浏览器),默认为 false。 当设置 true 时,同时请设置一下选项:maximumSessions 和 maxSessionsPreventsLogin
      session-number-control: false
      # 当设置为 1 时,maxSessionsPreventsLogin 为 false 时,同个用户登录会自动踢掉上一次的登录状态。 当设置为 1 时,maxSessionsPreventsLogin 为 true 时,同个用户登录会自动自动拒绝用户再登录。 默认为 1。 如要此选项生效,sessionNumberControl 必须为 true
      maximum-sessions: 1
      # 同个用户达到最大 maximumSession 后,当为 true 时自动拒绝用户再登录,当为 false 时自动踢掉上一次的登录状态, 默认为 false。 如要此选项生效,sessionNumberControl 必须为 true
      max-sessions-prevents-login: false
      # 如果设置为true,则允许HTTP会话在网址中使用HttpServletResponse.encodeRedirectURL(String)或HttpServletResponse.encodeURL(字符串)时被改写,被包含在URL,
      # 否则不允许HTTP会话。 这可以防止信息泄漏到外部域, 默认为: false
      enable-session-url-rewriting: false
      # concurrent session 失效后跳转地址, login-process-type=redirect 时有效. 默认: /
      invalid-session-of-concurrent-url: /concurrentSession.html
      # session 失效后跳转地址, login-process-type=redirect 时有效. 默认: /session/invalid,
      invalid-session-url: /session/invalid
      # session 的 cookie name, 默认为: JSESSIONID, 需要与 server.servlet.session.cookie.name 同时设置
      session-cookie-name: SID



  # 验证码配置
  # 同一个 uri 由多种验证码同时配置, **优先级**如下:
  #  `SMS > CUSTOMIZE > SELECTION > TRACK > SLIDER > IMAGE`
  codes:
    # 图片验证码
    image:
      # 设置需要图片验证码认证的 uri(必须是非 GET 请求),多个 uri 用 “-” 或 ","号分开支持通配符,如:/hello,/user/*;默认为 /authentication/form
      auth-urls:
        - /authentication/form
      request-param-image-code-name: imageCode
    # 短信验证码
    sms:
      # 设置需要短信验证码认证的 uri(必须是非 GET 请求),多个 uri 用 “,”号分开支持通配符,如:/hello,/user/*;默认为 /authentication/form
      auth-urls:
        - /authentication/mobile
      length: 6
      expire: 120
      request-param-mobile-name: mobile
      request-param-sms-code-name: smsCode

  # ================ 手机登录配置 ================
  mobile:
    login:
      # 手机验证码登录是否开启, 默认 false,
      # 手机验证码登录开启后 必须配置 ums.codes.sms.auth-urls=/authentication/mobile
      sms-code-login-is-open: true
      # 手机验证码登录请求处理url, 默认 /authentication/mobile
      login-processing-url-mobile: /authentication/mobile


---
spring:
  profiles: dev
  # redis
  redis:
    # redis-lettuce-pool
    lettuce:
      # 会影响应用关闭是时间, dev 模式设置为 0
      shutdown-timeout: PT0S
  mvc:
    throw-exception-if-no-handler-found: true

server:
  servlet:
    context-path: /demo

logging:
  config: classpath:logback-spring.xml

#debug: true
  • redis 依赖
  <!-- SpringSession Redis依赖 -->
  <dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session-data-redis</artifactId>
  </dependency>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
  </dependency>
  <!-- 为了解决 ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig -->
  <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.8.0</version>
  </dependency>