Spring Security学习笔记自定义登录页面(二)

来源:互联网 发布:程序员赚钱吗 编辑:程序博客网 时间:2024/04/30 20:02

在入门(一)是有问题的:

  1. 在做项目的时候,登录页面都是需要自己写的,并且登录页面是不需要验证直接可以访问的。
  2. 用户名和密码直接写在配置文件中,而实际项目中我们是放在数据库中的。

在之前 Spring Security 框架生成的登录页面中直接输入用户名和密码提交后,Spring Security框架替我们进行验证的
由于验证过程是 Spring Security 框架自动完成的,所以在我们的登录页面中表单元素的 name都是固定的

现在自己新建一个登录页面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>登录页面</title></head>    <body>            <form name='f' action='/springSecurity/j_spring_security_check'            method='POST'>            <table>                <tr>                    <td>用户名:</td>                    <td><input type='text' name='j_username' value='user'></td>                </tr>                <tr>                    <td>密码:</td>                    <td><input type='password' name='j_password' /></td>                </tr>                <tr>                    <td ><input name="submit" type="submit" value="登录"/></td>                    <td ><input name="reset" type="reset" value="重置"/></td>                </tr>            </table>        </form>    </body></html>

写完登录页面后,需要指定spring Security跳转到我们写的登录页面
springSecurity.xml配置

<?xml version="1.0" encoding="UTF-8"?><b:beans xmlns="http://www.springframework.org/schema/security"    xmlns:b="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-3.0.xsd                        http://www.springframework.org/schema/security                         http://www.springframework.org/schema/security/spring-security-3.0.xsd">    <!-- 这表示,我们要保护应用程序中的所有 URL,只有拥有  ROLE_USER 角色的用户才能访问 -->    <http auto-config="true">        <intercept-url pattern="/*" access="ROLE_USER"/>        <!-- login-page指定登录页面             /login.jsp* 加个*号是防止请求时后面带了参数             filters="none" 表示不拦截        -->        <form-login login-page="/login.jsp"/>        <intercept-url pattern="/login.jsp*" filters="none"/>    </http>    <!-- 配置认证管理器 -->    <authentication-manager>        <authentication-provider>            <user-service>                <user name="user" password="user" authorities="ROLE_USER"/>            </user-service>        </authentication-provider>    </authentication-manager></b:beans>

访问我们的项目,发现已经可以跳转到我们自己写的登录页面了
这里写图片描述