【问题标题】:switch user not working when connected to mysql 8 database?连接到 mysql 8 数据库时切换用户不起作用?
【发布时间】:2022-12-14 05:52:45
【问题描述】:

我正在尝试让 spring security 中的切换用户功能正常工作。我正在使用 grails 4.0.10 和 mysql 8。

我创建了一个示例 hello world grails 应用程序,并按照文档中的切换用户指南进行操作。 https://grails.github.io/grails-spring-security-core/4.0.x/index.html#switchUser

如果我使用默认的 h2 数据库那么它可以工作但是如果我切换到 mysql 8 数据库它会抛出页面未找到 404 错误并且它不会切换。

我已经在 github 上发布了代码。链接在这里。 https://github.com/sanjaygir/switching

我在安全控制器中创建了一个简单的页面。该页面是 index.gsp,它有一个可以切换到另一个用户的表单。登录用户应显示在此页面的顶部。在引导程序文件中,我创建了两个用户。一个管理员和另一个普通用户。

我有一个具有此配置的本地数据库

 dataSource:
            dbCreate: create
            url: jdbc:mysql://localhost:3307/switch?useUnicode=yes&characterEncoding=UTF-8
            username: root
            password: password

为了运行此应用程序,您需要运行 mysql 8 db。请在 application.yml 的上述部分中更改 mysql 数据库名称和用户名和密码。

应用程序启动后,请直接访问 http://localhost:8080/secure/index,然后在文本框中输入“user”并单击按钮开关。它将抛出一个未找到的错误页面,如果您返回 http://localhost:8080/secure/index,您将无法在顶部看到登录用户名。这意味着切换不成功。

这是 secure/index.gsp 的简单代码

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title></title>
</head>

<body>

<sec:ifLoggedIn>
    Logged in as <sec:username/>
</sec:ifLoggedIn>


<form action='${request.contextPath}/login/impersonate' method='POST'>
    Switch to user: <input type='text' name='username'/> <br/>
    <input type='submit' value='Switch'/>
</form>




</body>
</html>

我希望我已经说清楚了。这是一个简单的 hello world 应用程序,旨在查看切换用户功能的运行情况。我很困惑为什么切换用户使用默认的 h2 db 而不是连接到 mysql 8 时。如果有人有任何想法,我感谢你的帮助。谢谢

更新:

今天我将数据库切换到 mysql 版本 5 并且它可以工作。 我在 application.yml 中更改了以下配置

hibernate:
    cache:
        queries: false
        use_second_level_cache: false
        use_query_cache: false
dataSource:
    pooled: true
    jmxExport: true
    driverClassName: com.mysql.jdbc.Driver
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    username: root
    password: 'password'

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:mysql://localhost:3306/switch?useUnicode=yes&characterEncoding=UTF-8

在 build.gradle 我用过

runtime 'mysql:mysql-connector-java:5.1.19'

我仍然不确定为什么它在 mysql 8 中不起作用。

【问题讨论】:

    标签: mysql grails spring-security grails-4


    【解决方案1】:

    我终于找到了错误。我无法相信是什么导致了 404 not found 问题。它是配置文件中的一行。

    在 application.yml 看起来像这样之前

    ---
    grails:
        profile: web
        codegen:
            defaultPackage: rcroadraceweb4
        gorm:
            reactor:
                # Whether to translate GORM events into Reactor events
                # Disabled by default for performance reasons
                events: false
    info:
        app:
            name: '@info.app.name@'
            version: '@info.app.version@'
            grailsVersion: '@info.app.grailsVersion@'
    spring:
        jmx:
            unique-names: true
        main:
            banner-mode: "off"
        groovy:
            template:
                check-template-location: false
        devtools:
            restart:
                additional-exclude:
                    - '*.gsp'
                    - '**/*.gsp'
                    - '*.gson'
                    - '**/*.gson'
                    - 'logback.groovy'
                    - '*.properties'
    management:
        endpoints:
            enabled-by-default: false
    
    
    server:
        servlet:
            context-path: '/roadrace'
    
    
    ---
    
    hibernate:
        cache:
            queries: false
            use_second_level_cache: false
            use_query_cache: false
    
    
    grails:
        plugin:
            databasemigration:
                updateOnStart: true
                updateOnStartFileName: changelog.groovy
    
        controllers:
            upload:
                maxFileSize: 2000000
                maxRequestSize: 2000000
    
        mail:
            host: "localhost"
            port: 25
            default:
                to: 'root@localhost'
                from: 'noreply@runnercard.com'
    
    dataSource:
        type: com.zaxxer.hikari.HikariDataSource
        pooled: true
        driverClassName: com.mysql.cj.jdbc.Driver
        dialect: org.hibernate.dialect.MySQL8Dialect
        dbCreate: none
        properties:
            minimumIdle: 5
            maximumPoolSize: 10
            poolName: main-db
            cachePrepStmts: true
            prepStmtCacheSize: 250
            prepStmtCacheSqlLimit: 2048
            useServerPrepStmts: true
            useLocalSessionState: true
            rewriteBatchedStatements: true
            cacheResultSetMetadata: true
            cacheServerConfiguration: true
            elideSetAutoCommits: true
            maintainTimeStats: false
    dataSources:
        logging:
            # This is not used unless `useJdbcAccessLogger` or `useJdbcLogger` is set to `true`
            # This does not need to be setup unless it is in use.
            type: com.zaxxer.hikari.HikariDataSource
            pooled: true
            driverClassName: com.mysql.cj.jdbc.Driver
            properties:
                minimumIdle: 2
                maximumPoolSize: 5
                poolName: logging-db
                cachePrepStmts: true
                prepStmtCacheSize: 250
                prepStmtCacheSqlLimit: 2048
                useServerPrepStmts: true
                useLocalSessionState: true
                rewriteBatchedStatements: true
                cacheResultSetMetadata: true
                cacheServerConfiguration: true
                elideSetAutoCommits: true
                maintainTimeStats: false
    
    environments:
        development:
            dataSource:
                dbCreate: none
                url: jdbc:mysql://localhost:3307/dev2?useUnicode=yes&characterEncoding=UTF-8
                username: root
                password: password
            grails:
    #            mail:
    #                host: "smtp.gmail.com"
    #                port: 465
    #                username: "justforstackoverflow123@gmail.com"
    #                password: "1asdfqwef1"
    #                props:
    #                    "mail.smtp.auth": "true"
    #                    "mail.smtp.socketFactory.port": "465"
    #                    "mail.smtp.socketFactory.class": "javax.net.ssl.SSLSocketFactory"
    #                    "mail.smtp.socketFactory.fallback": "false"
    
        test:
            dataSource:
    #            dialect: org.hibernate.dialect.MySQL5InnoDBDialect
                dbCreate: none
                url: jdbc:mysql://localhost:3307/test?useUnicode=yes&characterEncoding=UTF-8
                username: root
                password: password
    
    
        production:
    ---
    logging:
        level:
            root: INFO
            org.springframework: WARN
            grails.plugin.springsecurity.web.access.intercept.AnnotationFilterInvocationDefinition: ERROR
            grails.plugins.DefaultGrailsPluginManager: WARN
            org.hibernate: ERROR # TODO: we need to lower this, and fix the warnings this is talking about.
            rcroadraceweb4: DEBUG
            com.runnercard: DEBUG
            liquibase.ext.hibernate.snapshot.HibernateSnapshotGenerator: ERROR
    
    ---
    #debug: true
    #useJdbcSessionStore: true
    ---
    environments:
        nateDeploy:
            behindLoadBalancer: true
            grails:
                insecureServerURL: 'https://nate-dev.nate-palmer.com/roadrace'
                serverURL: 'https://nate-dev.nate-palmer.com/roadrace'
            dataSource:
                url: 'jdbc:mysql://10.1.10.240:3306/rcroadwebDEV?serverTimezone=America/Denver'
    

    修复后它看起来像这样

    ---
    grails:
        profile: web
        codegen:
            defaultPackage: rcroadraceweb4
        gorm:
            reactor:
                # Whether to translate GORM events into Reactor events
                # Disabled by default for performance reasons
                events: false
    info:
        app:
            name: '@info.app.name@'
            version: '@info.app.version@'
            grailsVersion: '@info.app.grailsVersion@'
    spring:
        jmx:
            unique-names: true
        main:
            banner-mode: "off"
        groovy:
            template:
                check-template-location: false
        devtools:
            restart:
                additional-exclude:
                    - '*.gsp'
                    - '**/*.gsp'
                    - '*.gson'
                    - '**/*.gson'
                    - 'logback.groovy'
                    - '*.properties'
    management:
        endpoints:
            enabled-by-default: false
    
    
    server:
        servlet:
            context-path: '/roadrace'
    
    
    ---
    
    hibernate:
        cache:
            queries: false
            use_second_level_cache: false
            use_query_cache: false
    
    
    grails:
        plugin:
            databasemigration:
                updateOnStart: true
                updateOnStartFileName: changelog.groovy
    
        controllers:
            upload:
                maxFileSize: 2000000
                maxRequestSize: 2000000
    
        mail:
            host: "localhost"
            port: 25
            default:
                to: 'root@localhost'
                from: 'noreply@runnercard.com'
    
    dataSource:
        type: com.zaxxer.hikari.HikariDataSource
        pooled: true
        driverClassName: com.mysql.cj.jdbc.Driver
        dialect: org.hibernate.dialect.MySQL8Dialect
        dbCreate: none
        properties:
            minimumIdle: 5
            maximumPoolSize: 10
            poolName: main-db
            cachePrepStmts: true
            prepStmtCacheSize: 250
            prepStmtCacheSqlLimit: 2048
            useServerPrepStmts: true
            useLocalSessionState: true
            rewriteBatchedStatements: true
            cacheResultSetMetadata: true
            cacheServerConfiguration: true
            elideSetAutoCommits: true
            maintainTimeStats: false
    dataSources:
        logging:
            # This is not used unless `useJdbcAccessLogger` or `useJdbcLogger` is set to `true`
            # This does not need to be setup unless it is in use.
            type: com.zaxxer.hikari.HikariDataSource
            pooled: true
            driverClassName: com.mysql.cj.jdbc.Driver
            properties:
                minimumIdle: 2
                maximumPoolSize: 5
                poolName: logging-db
                cachePrepStmts: true
                prepStmtCacheSize: 250
                prepStmtCacheSqlLimit: 2048
                useServerPrepStmts: true
                useLocalSessionState: true
                rewriteBatchedStatements: true
                cacheResultSetMetadata: true
                cacheServerConfiguration: true
                elideSetAutoCommits: true
                maintainTimeStats: false
    
    environments:
        development:
            dataSource:
                dbCreate: none
                url: jdbc:mysql://localhost:3307/dev2?useUnicode=yes&characterEncoding=UTF-8
                username: root
                password: password
    #        grails:
    #            mail:
    #                host: "smtp.gmail.com"
    #                port: 465
    #                username: "justforstackoverflow123@gmail.com"
    #                password: "1asdfqwef1"
    #                props:
    #                    "mail.smtp.auth": "true"
    #                    "mail.smtp.socketFactory.port": "465"
    #                    "mail.smtp.socketFactory.class": "javax.net.ssl.SSLSocketFactory"
    #                    "mail.smtp.socketFactory.fallback": "false"
    
        test:
            dataSource:
    #            dialect: org.hibernate.dialect.MySQL5InnoDBDialect
                dbCreate: none
                url: jdbc:mysql://localhost:3307/test?useUnicode=yes&characterEncoding=UTF-8
                username: root
                password: password
    
    
        production:
    ---
    logging:
        level:
            root: INFO
            org.springframework: WARN
            grails.plugin.springsecurity.web.access.intercept.AnnotationFilterInvocationDefinition: ERROR
            grails.plugins.DefaultGrailsPluginManager: WARN
            org.hibernate: ERROR # TODO: we need to lower this, and fix the warnings this is talking about.
            rcroadraceweb4: DEBUG
            com.runnercard: DEBUG
            liquibase.ext.hibernate.snapshot.HibernateSnapshotGenerator: ERROR
    
    ---
    #debug: true
    #useJdbcSessionStore: true
    ---
    environments:
        nateDeploy:
            behindLoadBalancer: true
            grails:
                insecureServerURL: 'https://nate-dev.nate-palmer.com/roadrace'
                serverURL: 'https://nate-dev.nate-palmer.com/roadrace'
            dataSource:
                url: 'jdbc:mysql://10.1.10.240:3306/rcroadwebDEV?serverTimezone=America/Denver'
    

    这是环境>开发块中的这一行

     #        grails:
    

    它在评论 grails 行后起作用。

    但是 grails 块的所有内容都被注释了,所以我仍然很困惑为什么不注释 grails 会有这个大问题。无论如何经过几天的艰苦搜索终于解决了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-31
      • 2013-01-29
      • 2012-12-28
      • 2012-03-05
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多