Spring Security 连接到mysql 8数据库时交换机用户不工作?

fdbelqdn  于 2022-12-13  发布在  Spring
关注(0)|答案(1)|浏览(122)

我正在尝试让Spring Security中的Switch用户功能工作。我正在使用Grails 4. 0. 10和MySQL 8。
我创建了一个示例hello world grails应用程序,并按照文档中的switch用户指南进行操作。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数据库运行。请更改mysql数据库名称和用户名和密码在上述部分中的application. yml。
应用程序启动后,请直接进入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应用程序,创建来查看switch user特性的操作。我很困惑为什么switch user可以使用默认的h2 db,而在连接到mysql 8时却不行。如果有人有任何想法,我感谢你的帮助。谢谢

更新日期:

今天我把数据库切换到mysql版本5,它工作了。

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

在建筑中,我使用了Gradle

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

我还是不知道为什么它在mysql 8中不起作用。

qrjkbowd

qrjkbowd1#

我终于找到了bug。我不敢相信是什么原因导致404找不到issue.it是配置文件中的一行。
之前的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会有这个大问题。不管怎样,经过几天的努力搜索解决了!

相关问题