You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用shinymanager需两次登录的问题求助

shinymanager基础应用二次登录需重复提交密码才能验证成功

问题详情

使用shinymanager官方文档中的基础示例应用时出现异常:

  • 首次运行新创建的应用文件,用户名和密码验证正常通过
  • 后续再次登录时,第一次提交会提示“用户名/密码无效”,无需修改任何内容,再次提交即可成功登录
  • 已尝试调整服务器/UI逻辑、创建全新的基础应用测试,问题仍未解决

以下是使用的基础应用代码:

credentials <- data.frame(
  user = c("shiny", "shinymanager"), # mandatory
  password = c("azerty", "12345"), # mandatory
  start = c("2019-04-15"), # optinal (all others)
  expire = c(NA, "2019-12-31"),
  admin = c(FALSE, TRUE),
  comment = "Simple and secure authentification mechanism 
  for single ‘Shiny’ applications.",
  stringsAsFactors = FALSE
)

library(shiny)
library(shinymanager)

ui <- fluidPage(
  tags$h2("My secure application"),
  verbatimTextOutput("auth_output")
)

# Wrap your UI with secure_app
ui <- secure_app(ui)


server <- function(input, output, session) {
  
  # call the server part
  # check_credentials returns a function to authenticate users
  res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )
  
  output$auth_output <- renderPrint({
    reactiveValuesToList(res_auth)
  })
  
  # your classic server logic
  
}

shinyApp(ui, server)

可能的解决方案

1. 更新shinymanager到最新版本

该问题可能是旧版本的已知bug,先检查当前版本:

packageVersion("shinymanager")

若版本不是最新,执行更新:

install.packages("shinymanager")

2. 调整登录会话缓存设置

在secure_app中禁用持久化Cookie,强制每次登录重新验证:

ui <- secure_app(ui, cookie_expiry = 0)

3. 重构验证逻辑的调用方式

避免直接在secure_server中传入静态凭证,改用reactive对象传递:

server <- function(input, output, session) {
  # 用reactive包裹凭证数据
  reactive_creds <- reactive({
    credentials
  })
  
  res_auth <- secure_server(
    check_credentials = check_credentials(reactive_creds())
  )
  
  output$auth_output <- renderPrint({
    reactiveValuesToList(res_auth)
  })
}

4. 清除浏览器缓存与Cookie

浏览器保存的旧会话数据可能干扰验证流程,手动清除当前应用域名下的缓存和Cookie后重新测试。

5. 启用验证日志排查

添加日志输出,查看两次登录请求的验证细节:

res_auth <- secure_server(
  check_credentials = check_credentials(credentials, log_outdir = tempdir())
)

前往临时目录查看生成的日志文件,分析验证失败的具体原因。

内容的提问来源于stack exchange,提问作者Matt Simonson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 17:26:00