zoukankan      html  css  js  c++  java
  • springsecurity基于数据库验证用户

    之前的springsecurity程序都是将数据存放在内存中的,通过

    1 <security:user-service>
    2             <security:user name="user" password="user" authorities="ROLE_USER"/>
    3             <security:user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN"/>
    4          </security:user-service>

    user-service指定用户信息,例如name,password和权限

    但是这样的验证在开发中是不科学的,所以我们要连接数据库,通过匹配数据库中的信息判断用户是否登陆

    首先我们需要添加依赖,需要继续添加的依赖有spring-jdbc,spring-core,spring-web,spring-webmvc,mysql-connector-java

    添加完依赖后我们需要加入数据库的配置文件

    在spring-security配置文件中配置datasource

    jdbc-user-service 元素,通过该元素我们可以定义一个从数据库获取 UserDetails 的 UserDetailsService, dataSource 是对应数据源配置的 bean 引用

    完整的spring-security.xml配置文件:

     1 <beans xmlns="http://www.springframework.org/schema/beans"
     2        xmlns:security="http://www.springframework.org/schema/security"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans
     6           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
     7           http://www.springframework.org/schema/security
     8           http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     9     <!--加载数据库的配置文件-->
    10     <context:property-placeholder location="classpath:db.properties"/>
    11      <!--配置数据源-->
    12     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    13         <property name="driverClassName" value="${jdbc.driverClass}"/>
    14         <property name="password" value="${jdbc.password}"/>
    15         <property name="url" value="${jdbc.jdbcUrl}"/>
    16         <property name="username" value="${jdbc.user}"/>
    17     </bean>
    18         <!--
    19         自定义表单,通过form-login标签
    20         authentication-failure-url指定登陆失败以后应该跳转的页面
    21         default-target-url指定登陆成功以后跳转的页面,默认的是index.jsp页面
    22         -->
    23     <security:http auto-config="true">
    24         <security:form-login login-page="/login.jsp"
    25                              default-target-url="/successful.jsp"
    26                              login-processing-url="/login.do"
    27                              authentication-failure-url="/login_failure.jsp"
    28                              always-use-default-target="true"
    29                              username-parameter="username"
    30                              password-parameter="password"
    31         />
    32         <!-- 表示匿名用户可以访问 -->
    33         <security:intercept-url pattern="/login*.jsp*"
    34                                 access="IS_AUTHENTICATED_ANONYMOUSLY" />
    35         <security:intercept-url pattern="/**" access="ROLE_USER" />
    36     </security:http>
    37 
    38 
    39     <security:authentication-manager>
    40         <security:authentication-provider>
    41             <security:jdbc-user-service data-source-ref="dataSource"/>
    42         </security:authentication-provider>
    43     </security:authentication-manager>
    44 
    45 </beans>

    默认情况下 jdbc-user-service 将使用 SQL 语句 “select username, password, enabled from users where username = ?” 来获取用户信息;使用 SQL 语句 “select username, authority from authorities where username = ?” 来获取用户对应的权限;

    若数据库表名不是系统默认的,我们可以通过属性 users-by-username-query 来指定查询用户信息的时候是从自定义用户信息表中查询,通过authorities-by-username-query来查询用户权限

    另外还有一种连接数据库的方法

    1 <security:authentication-manager>
    2         <security:authentication-provider user-service-ref="userDetailsService"/>
    3     </security:authentication-manager>
    4 
    5     <bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
    6         <property name="dataSource" ref="dataSource"/>
    7     </bean>

    JdbcDaoImpl是支持用户组查询的

  • 相关阅读:
    122. Best Time to Buy and Sell Stock II
    121. Best Time to Buy and Sell Stock
    72. Edit Distance
    583. Delete Operation for Two Strings
    582. Kill Process
    indexDB基本用法
    浏览器的渲染原理
    js实现txt/excel文件下载
    git 常用命令
    nginx进入 配置目录时
  • 原文地址:https://www.cnblogs.com/Hdaydayup/p/6833022.html
Copyright © 2011-2022 走看看