【问题标题】:(Spring) perform authorization with a DB table(Spring) 使用数据库表执行授权
【发布时间】:2017-11-27 19:05:48
【问题描述】:

哟,我正在学习 Spring。 在这个新剧集中,我想使用 DB 进行登录。

所以我在 MySqlWorkbench 中创建了名为“demodb”的 SQL 架构,在这个架构中我有一个包含 3 列(用户名、密码、身份验证)的表

问题: 当我运行我的应用程序并出现登录表单时,即使我输入了正确的用户名和密码,Auth 列设置为“ROLE_USER”,我也无法继续。

3 次尝试后出现错误 401。

这是我的SecConfig.xml

<sec:http auto-config="true">
    <sec:intercept-url pattern="/**" access="ROLE_USER"/>
    <sec:http-basic/>
</sec:http>

<sec:authentication-manager>
   <sec:authentication-provider user-service-ref="myUserDeatilsService"/>
</sec:authentication-manager>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/demodb?useSSL=false" />
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>
<bean id="myUserDeatilsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
    <property name="dataSource" ref="dataSource"></property>
</bean>

信息:已加载 SQLErrorCodes:[DB2、Derby、H2、HSQL、Informix、MS-SQL、MySQL、Oracle、PostgreSQL、Sybase、Ha

【问题讨论】:

    标签: mysql spring authentication


    【解决方案1】:

    对于 Spring Security,您应该为用户和用户角色表定义查询。

    请参阅 spring 安全文档以获取示例:spring security doc

    另请参阅 Mkyong 的精彩指南:Spring Security form login using database

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 2021-12-10
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      相关资源
      最近更新 更多