Spring Security怎么看数据库的密码
时间: 2024-02-22 12:52:05 浏览: 107
Spring Security可以通过配置数据源和查询语句来实现基于数据库的账户密码认证。具体步骤如下:
1.在Spring Security配置文件中配置数据源,例如:
```xml
<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/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
```
2.配置查询语句,例如:
```xml
<security:authentication-manager>
<security:authentication-provider>
<security:jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username,password,enabled from users where username=?"
authorities-by-username-query="select username,authority from authorities where username=?"/>
</security:authentication-provider>
</security:authentication-manager>
```
其中,users-by-username-query用于查询用户信息,authorities-by-username-query用于查询用户权限信息。
3.在查询语句中,密码需要以加密后的形式存储在数据库中。Spring Security提供了多种加密方式,例如MD5、SHA等。可以通过配置PasswordEncoder来指定加密方式,例如:
```xml
<security:authentication-manager>
<security:authentication-provider>
<security:jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username,password,enabled from users where username=?"
authorities-by-username-query="select username,authority from authorities where username=?"
password-encoder="md5"/>
</security:authentication-provider>
</security:authentication-manager>
```
其中,password-encoder用于指定加密方式。
阅读全文