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

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

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

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

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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="utf-8"%>

<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配置



<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">

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

        
        <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>

访问我们的项目,发现已经可以跳转到我们自己写的登录页面了
Spring Security学习笔记自定义登录页面(二)_第1张图片

你可能感兴趣的:(spring-security)