Skip to content

六 7、验证码功能配置

ZeroOrInfinity edited this page Jan 5, 2021 · 14 revisions

所属模块: core 模块

简单配置: demo 模块 -> basic-example

ums:
  # 验证码配置
  codes:
    # 图片验证码
    image:
      # 设置需要图片验证码认证的 uri(必须是非 GET 请求),多个 uri 用 “-” 或 ","号分开支持通配符,如:/hello,/user/*;默认为 /authentication/form
      auth-urls:
        - /authentication/form
        - /authentication/social          
    # 短信验证码
    sms:
      # 设置需要短信验证码认证的 uri(必须是非 GET 请求),多个 uri 用 “,”号分开支持通配符,如:/hello,/user/*;默认为 空
      auth-urls:
        - /authentication/mobile

详细配置: demo 模块 -> validate-code-example

server:
  port: 9090

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

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

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'

  # 验证码配置
  codes:
    # 验证码缓存类型, 默认: SESSION, 可选: REDIS/SESSION
    validate-code-cache-type: session
    # 获取验证码的 url 的前缀, 默认: /code
    validate-code-url-prefix: /code
    # ===== 刷新图片验证码与滑块验证码缓存 定时任务 属性 =====
    # A cron-like expression.
    # 0 * 4 * * ? 分别对应: second/minute/hour/day of month/month/day of week
    # 默认为: "0 * 4 * * ?", 凌晨四点
    refresh-validate-code-job-cron: 0 * 4 * * ?
    # 是否支持定时刷新 validateCodeJob 定时任务, 考虑到很多应用都有自己的定时任务应用, 默认: false. RefreshValidateCodeCacheJob 接口的实现已注入 IOC 容器, 方便自定义定时任务接口时调用.
    enable-refresh-validate-code-job: true
    # 定时刷新 validateCodeJob 任务时, 需要缓存的验证码图片数. 默认: 100;
    # 注意: 这里为了方便开发而设置的值, 生产环境根据情况直接设置
    total-images: 100
    # 图片验证码
    image:
      # 设置需要图片验证码认证的 uri(必须是非 GET 请求),多个 uri 用 “-” 或 ","号分开支持通配符,如:/hello,/user/*;默认为 /authentication/form
      auth-urls:
        - /authentication/form
        - /authentication/social
      # 验证码长度
      length: 4
      # 图片验证码高
      height: 60
      # 图片验证码宽
      width: 270
      # 验证码过期时间
      expire: 180
      # 验证码 request 参数名称
      request-param-image-code-name: imageCode
      # 缓存图片验证码目录,默认 classpath:static/image/code,
      # 不以 "classpath:" 开头时即认为是绝对路径, 以 "classpath:" 开头时即认为是基于 classpath 的相对路径.
      image-cache-directory: classpath:static/image/code

    # 滑块验证码
    slider:
      # 设置需要验证码认证的 uri(必须是非 GET 请求),多个 uri 用 “-” 或 ","号分开支持通配符,如:/hello,/user/*;默认为 空
      auth-urls:
        - /authentication/form
        - /slider/check
      # 设置滑块验证码校验证的 uri, 默认: /slider/check, 默认: /slider/check
      slider-check-url: /slider/check
      # 提交验证码请求时,请求中带的验证码变量名,默认 sliderCode.
      # 与 tokenRequestParamName, xRequestParamName, yRequestParamName 互斥关系;
      # 当使用此参数时, 把另外三个参数(kv键值对形式, 键值对之间用逗号风隔) 组装到此参数.
      # 注意: 默认传递参数是用另外的三个参数实现验证码校验, 如要使用此参数, 请重新实现 ValidateCodeProcessor#validate(ServletWebRequest)
      request-param-name: sliderCode
      # request token param name, 默认: sliderToken. 与 requestParamName 互斥关系.
      token-request-param-name: sliderToken
      # request X param name, 默认: x. 与 requestParamName 互斥关系.
      x-request-param-name: x
      # request Y param name, 默认: y. 与 requestParamName 互斥关系.
      y-request-param-name: y
      # 在模板上抠图区灰阶等级: 4-10, 数值越高, 灰色越深, 默认: 5
      grayscale: 5
      # 模板图片与原始图片的图片后缀, 默认: png
      imageSuffix: png
      # 滑块验证码默认过期时间, 180 秒
      expire: 180
      # 校验 SliderCode.getLocationX() 时的允许的差异值: 默认: 3 PX
      redundancy-value: 3
      # 原始图片目录,默认 classpath:static/image/slider/original
      # 不以 "classpath:" 开头时即认为是绝对路径, 以 "classpath:" 开头时即认为是基于 classpath 的相对路径.
      original-image-directory: classpath:static/image/slider/original
      # 模板图片目录, 模板图片必须符合: 模板图片的宽 * 3 < 原始图片的宽, 且 模板图片的高 *2 < 原始图片的高; 背景必须是白色, 默认 classpath:static/image/slider/template,
      # 不以 "classpath:" 开头时即认为是绝对路径, 以 "classpath:" 开头时即认为是基于 classpath 的相对路径.
      template-image-directory: classpath:static/image/slider/template
      # 根据原始图片生成的滑块图片目录,用于自定义缓存滑块图片的存储目录, 默认 classpath:static/image/slider/code,
      # 不以 "classpath:" 开头时即认为是绝对路径, 以 "classpath:" 开头时即认为是基于 classpath 的相对路径.
      code-image-directory: classpath:static/image/slider/code


    # 短信验证码
    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
  mvc:
    throw-exception-if-no-handler-found: true
  thymeleaf:
    cache: false
#  redis:
#    host: 192.168.88.88
#    port: 6379
#    password:
#    database: 0
#    # 连接超时的时间
#    timeout: 10000
#    # redis-lettuce-pool
#    lettuce:
#      # 会影响应用关闭是时间, dev 模式设置为 0
#      shutdown-timeout: PT0S
#      pool:
#        max-active: 8
#        max-wait: PT10S
#        max-idle: 8
#        min-idle: 1
#debug: true

server:
  servlet:
    context-path: /demo

logging:
  config: classpath:logback-spring.xml