R Shiny:单个app.R的用户身份验证

时间:2016-12-15 16:04:48

标签: r authentication shiny

我正在开发一个R Shiny App并希望添加用户名和登录。我检查了RStudio演示但是只使用了ShinyServer Pro,我使用mongolite软件包将formData备份到Mongodb。

在生成应用UI之前,有没有办法添加用户登录?

7 个答案:

答案 0 :(得分:9)

以下是如何使用Cookie进行身份验证的示例。更多信息可以在我的博客here中找到。

首先将cookie js下载到www /文件夹:

if (!dir.exists('www/')) {
    dir.create('www')
}

download.file(
  url = 'https://raw.githubusercontent.com/js-cookie/js-cookie/master/src/js.cookie.js',
  destfile = 'www/js.cookies.js'
)

安装必要的软件包:

install.packages(c('shiny', 'shinyjs', 'bcrypt'))

将以下代码保存为app.R,然后单击“运行应用程序”按钮:

library(shiny)
library(shinyjs)
library(bcrypt)


# This would usually come from your user database.

# Never store passwords as clear text
password_hash <- hashpw('secret123') 

# Our not so random sessionid
# sessionid <- paste(
#   collapse = '', 
#   sample(x = c(letters, LETTERS, 0:9), size = 64, replace = TRUE)
# )
sessionid <- "OQGYIrpOvV3KnOpBSPgOhqGxz2dE5A9IpKhP6Dy2kd7xIQhLjwYzskn9mIhRAVHo" 


jsCode <- '
  shinyjs.getcookie = function(params) {
    var cookie = Cookies.get("id");
    if (typeof cookie !== "undefined") {
      Shiny.onInputChange("jscookie", cookie);
    } else {
      var cookie = "";
      Shiny.onInputChange("jscookie", cookie);
    }
  }
  shinyjs.setcookie = function(params) {
    Cookies.set("id", escape(params), { expires: 0.5 });  
    Shiny.onInputChange("jscookie", params);
  }
  shinyjs.rmcookie = function(params) {
    Cookies.remove("id");
    Shiny.onInputChange("jscookie", "");
  }
'

server <- function(input, output) {

  status <- reactiveVal(value = NULL)
  # check if a cookie is present and matching our super random sessionid  
  observe({
    js$getcookie()
    if (!is.null(input$jscookie) && 
        input$jscookie == sessionid) {
          status(paste0('in with sessionid ', input$jscookie))
    }
    else {
      status('out')
    }
  })

  observeEvent(input$login, {
    if (input$username == 'admin' & 
        checkpw(input$password, hash = password_hash)) {
      # generate a sessionid and store it in your database,
      # sessionid <- paste(
      #   collapse = '', 
      #   sample(x = c(letters, LETTERS, 0:9), size = 64, replace = TRUE)
      # )
      # but we keep it simple in this example...
      js$setcookie(sessionid)
    } else {
      status('out, cause you don\'t know the password secret123 for user admin.')
    }
  })

  observeEvent(input$logout, {
    status('out')
    js$rmcookie()
  })

  output$output <- renderText({
    paste0('You are logged ', status())}
  )
}

ui <- fluidPage(
  tags$head(
    tags$script(src = "js.cookies.js")
  ),
  useShinyjs(),
  extendShinyjs(text = jsCode),
  sidebarLayout(
    sidebarPanel(
      textInput('username', 'User', placeholder = 'admin'),
      passwordInput('password', 'Password', placeholder = 'secret123'),
      actionButton('login', 'Login'),
      actionButton('logout', 'Logout')
    ),
    mainPanel(
      verbatimTextOutput('output')
    )
  )
)

shinyApp(ui = ui, server = server)

答案 1 :(得分:9)

ShinyProxy是一个基于开源的Docker和基于Spring Java的Shiny服务器,旨在解决这个问题。它允许您对应用程序配置文件中的用户进行硬编码,连接到LDAP服务器,使用SSO / Keycloak或社交网络登录。

答案 2 :(得分:6)

嗯,您可以通过代码使用renderUI并动态更改UI来实现。以下是如何执行此操作的示例:

library(shiny)
library(ggplot2)

u <- shinyUI(fluidPage(
  titlePanel("Shiny Password"),

  sidebarLayout(position = "left",
                sidebarPanel( h3("sidebar panel"),
                              uiOutput("in.pss"),
                              uiOutput("in.clr"),
                              uiOutput("in.titl"),
                              uiOutput("in.cnt"),
                              uiOutput("in.seed")

                ),
                mainPanel(h3("main panel"),
                          textOutput('echo'),
                          plotOutput('stdplot')
                )
  )
))

pok <- F

s <- shinyServer(function(input, output) 
{
  output$in.pss   <- renderUI({ input$pss; if (pok) return(NULL) else return(textInput("pss","Password:","")) })
  output$in.clr   <- renderUI({ input$pss; if (pok) return(selectInput("clr","Color:",c("red","blue"))) else return(NULL) })
  output$in.titl  <- renderUI({ input$pss; if (pok) return(textInput("titl","Title:","Data")) else return(NULL) })
  output$in.cnt   <- renderUI({ input$pss; if (pok) return(sliderInput("cnt","Count:",100,1000,500,5)) else return(NULL) })
  output$in.seed  <- renderUI({ input$pss; if (pok) return(numericInput("seed","Seed:",1234,1,10000,1)) else return(NULL) })
  histdata <- reactive(
    {
      input$pss;
      validate(need(input$cnt,"Need count"),need(input$seed,"Need seed"))
      set.seed(input$seed)
      df <- data.frame(x=rnorm(input$cnt))
    }
  )
  observe({
     if (!pok) {
       password <- input$pss
       if (!is.null(password) && password == "pass") {
         pok <<- TRUE
       }
     }
   }
  )
  output$echo = renderText(
    {
      if (pok) {
        s <- sprintf("the %s is %s and has %d rows and uses the %d seed",
           input$ent,input$clr,nrow(histdata()),input$seed)
      } else {
        s <- ""
      }
      return(s)
    }
  )
  output$stdplot = renderPlot(
    {
      input$pss
      if (pok) {
        return(qplot(data = histdata(),x,fill = I(input$clr),binwidth = 0.2,main=input$titl))
      } else {
        return(NULL)
      }
    }
  )
}
)
shinyApp(ui=u,server=s)

产量

登录时:

enter image description here

这一旦你输入了硬编码密码&#34;传递&#34;。

enter image description here

当然,这种编程方式有点尴尬,但您可以使用制表符并使用类似的逻辑隐藏它们。

或者如果您使用的是shinyServer,您可能会在网站前面放置一个过滤器。但这就是我在Shiny中接近它的方式。

答案 3 :(得分:4)

我最近写了一个R包,其中提供了可以与任何引导UI框架集成的登录/注销模块。

Blogpost with example using shinydashboard

Package repo

软件包仓库中的inst/目录包含示例应用程序的代码。

答案 4 :(得分:3)

您可以在Shiny应用程序之前添加一个身份验证代理,如下所示:https://www.datascienceriot.com/add-authentication-to-shiny-server-with-nginx/kris/

这是一个骨架Nginx配置,可以从HTTPS端口443重定向到在端口8000上运行的Shiny Server。

server {
    listen       443;
    server_name  shinyservername;

    ssl                  on;
    ssl_certificate      ...
    ssl_certificate_key  ...
    ssl_dhparam ...

    location / {
        proxy_pass http://yourdestinationIP:8000;
        proxy_set_header        X-Forwarded-Proto $scheme;
        add_header              Front-End-Https   on;
        proxy_set_header        Accept-Encoding   "";
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/htpasswd;
    }
}

将主机防火墙设置为打开端口443,并且只允许本地主机连接到端口8000上的Shiny Server:

iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp -s localhost --dport 8000 -j ACCEPT
iptables -A INPUT -p tcp --dport 8000 -j DROP

将一个或多个用户的静态凭据添加到/etc/nginx/htpasswd

htpasswd –c /etc/nginx/htpasswd myshinyuser

一个缺点(很多)是这将验证&amp;授权,但它不会将经过身份验证的用户信息传递给您的应用程序。为此,您需要Shiny Server Pro的身份验证集成,在会话中将用户传递给您。

答案 5 :(得分:3)

polished R软件包将验证和用户管理添加到任何Shiny应用程序:https://github.com/Tychobra/polished

以下是您通过抛光获得的默认登录页面的屏幕截图: default polished sign in page
您可以在登录和注册页面上用自己的商标轻松替换占位符徽标和颜色。

Polished还随附一个仪表板来管理您的应用程序的用户:


更多详细信息:https://polished.tech/

答案 6 :(得分:2)

我使用shinyAppLogin而不是shinApp:

# R code
shinyAppLogin <- function( ui, server, title="Restricted Area", accounts = list(admin="admin"), ...) {
    ui_with_login <- bootstrapPage(theme = "login_style.css",
        div(class="login-page",
            div(class="form",
                h1(title), br(),
                tags$form(class="login-form",
                    textInput(inputId = "user", label = NULL, placeholder="Username"),
                    passwordInput(inputId = "pass", label = "", placeholder = "Password" ),
                    actionButton(inputId = "login", label = "Login")
            ) ) ) )

    server_with_login <- function(input, output, session) {

        observeEvent(input$login, ignoreNULL = T, {

        if ( input$user %in% names(accounts) && input$pass == accounts[[input$user]] ) {

            removeUI(selector = "body", multiple = T, immediate = T, session = session)
            insertUI(selector = "html", where = "beforeEnd", ui = ui, immediate = T, session = session )
            server(session$input, session$output, session = session)
        }
    } ) }

    shinyApp(ui = ui_with_login, server = server_with_login, ...)
}

然后我的代码变成: shinyAppLogin(my_ui,my_server)

Login screen when styles

然后我使用enter link description here中的css 只需将你的CSS保存在www / login_style.css