SpringSecurity学习笔记(三)——自定义登录界面读取数据库用户以及权限
来源:互联网 发布:点对点网速测试软件 编辑:程序博客网 时间:2024/05/29 16:29
在SpringSecurity学习笔记(一)——入门篇中,在文末我们介绍了SpringSecurity如何使用自定义的登录界面,因为用户和权限都是写死的,所以相对本篇而言,简单很多,本文将介绍如何通过自定义界面来读取数据库中的用户以及相应的权限。
一.login.jsp
注意:这边url和SpringSecurity学习笔记(一)——入门篇中所述的差别很大,这里的url不仅可以为j_spring_security_check,还可以为任何非被SpringMVC中定义过的,不过有一点务必注意,它必须与spring-security.xml中定义的login-processing-url="/XXXX" 一致。
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*" isELIgnored="false" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="<c:url value='haha'/>" method="POST"><table><tr><td>用户名</td><td><input type="text" name="username"></td></tr><tr><td>密 码</td><td><input type="password" name="password"></td></tr><tr><td><input type="reset" value="重新登录"></td><td><input type="submit" value="登录"/></td></tr></table></form></body></html>
二.spring-security.xml配置
注意:
1.不能缺少 <csrf disabled="true"/>,否则会报错:缺少下面这个会报Could not verify the provided CSRF token because your session was not found.这是因为spring security为了防止跨站请求,除了配置<csrf disabled="true"/>,也可以在登录的表单中添加如下验证信息
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
2.除了上面的注意外还需要注意,这边的username-parameter="username" 和 password-parameter="password"必须和登录界面中的name一致
<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"><http pattern="/login.jsp" security="none"/> <http auto-config='true'> <!-- <intercept-url pattern="/login.jsp" access="hasRole('ROLE_ADMIN')" /> --> <intercept-url pattern="/admin.jsp" access="hasRole('ROLE_ADMIN')" /> <!-- 配置所有资源的访问权限 --> <!-- <intercept-url pattern="/**" access="hasRole('ROLE_USER')" /> --> <!-- 缺少下面这个会报Could not verify the provided CSRF token because your session was not found. --> <csrf disabled="true"/> <form-login login-page="/login.jsp" login-processing-url="/haha" authentication-failure-url="/login.jsp?error=true" username-parameter="username" password-parameter="password" default-target-url="/admin.jsp" /> </http><!-- <authentication-manager> <authentication-provider> <jdbc-user-service data-source-ref="dataSource" users-by-username-query="SELECT USERNAME,PASSWORD,status as enabled FROM USERS WHERE USERNAME=?" authorities-by-username-query="select u.username,r.role_name as authority from USERS u join USER_ROLE ur on u.id=ur.user_id join ROLE r on r.role_id=ur.role_id where u.username=?"/> </authentication-provider> </authentication-manager> <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/> <beans:property name="url" value="jdbc:mysql://123.207.179.XXXX:3306/XXXXX"/> <beans:property name="username" value="XXXXX"/> <beans:property name="password" value="XXXXX"/></beans:bean> --> <authentication-manager alias="authenticationManager"> <authentication-provider ref="authenticationProvider" /> </authentication-manager> <beans:bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> <beans:property name="userDetailsService" ref="myUserDetailsService" /> <beans:property name="hideUserNotFoundExceptions" value="false" /> </beans:bean> </beans:beans>
三.mybatis中的配置以及UserDetailsService的实现类这边就不再赘述了
可以参考点击打开链接
阅读全文
0 0
- SpringSecurity学习笔记(三)——自定义登录界面读取数据库用户以及权限
- springsecurity自定义登录界面
- SpringSecurity学习笔记之三:配置用户存储
- SpringSecurity学习笔记(二)——访问数据库(二)
- SpringSecurity【用户+权限的数据库持久化】
- SpringSecurity学习笔记(四)——更改SpringSecurity加密方式
- 工作学习笔记——用SQL语句新建SQL SERVER数据库、数据库用户以及登录名
- SpringSecurity权限管理——自定义表结构!!!
- SpringSecurity权限管理——自定义表结构2!!!
- SpringSecurity学习笔记之五:认证用户
- springSecurity自定义用户登录页面时,常见的问题
- springBoot+springSecurity 数据库动态管理用户、角色、权限(二)
- springBoot+springSecurity 数据库动态管理用户、角色、权限(二)
- Oracle学习笔记(三)——Oracle用户的管理、权限与角色
- SpringSecurity学习三-当前所有用户的信息
- Android学习笔记<初级>—自定义权限
- servlet学习笔记4——用户登录网站(数据库验证)
- SpringSecurity———单点登录CAS
- 静态
- TimeAndMath
- WebHubBot爬虫分享(每天可抓取500万以上)
- 问题:Objective-C 中 C 类型函数不识别 foundation 中的类
- 设计模式
- SpringSecurity学习笔记(三)——自定义登录界面读取数据库用户以及权限
- Active Job
- swift 网络----利用URLSession的Block模式下载多张图片
- 11.ar, nm, objdump
- 华北水利水电大学社会实践--国情社情调研团走进社区
- 慕课HTML基础知识复习
- fabric 开发环境
- 葵花宝典 六
- jsp session