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

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

SpringSecurity提供了自定义AuthenticationProvider和AuthenticationFilter的功能。在Spring Security中,AuthenticationProvider是一个接口,用于对用户进行身份验证。默认的实现是DaoAuthenticationProvider。你可以通过实现该接口来创建自定义的身份验证提供者,以适应特定的需求。自定义的AuthenticationProvider可以通过在配置文件中指定来替换默认的Provider。例如,在配置文件中添加以下代码可以引用自定义的Provider: ```xml <authentication-manager> <authentication-provider ref="customProvider" /> </authentication-manager> ``` 此处的`customProvider`是指自定义的AuthenticationProvider的bean的ID,你可以根据实际情况进行修改。 另外,AuthenticationFilter是用于处理身份验证请求的过滤器。它负责从请求中提取用户凭证并使用AuthenticationProvider进行身份验证。Spring Security提供了多个不同类型的AuthenticationFilter,如UsernamePasswordAuthenticationFilter、BasicAuthenticationFilter等。你可以根据需要选择合适的AuthenticationFilter,并将其配置到Spring Security的过滤器链中。 关于Spring Security的源码,你可以在GitHub上找到它的源码存储库。在这个存储库中,你可以查看和学习Spring Security的实现细节。 总结起来,你可以通过自定义AuthenticationProvider来实现特定需求的身份验证,同时可以选择合适的AuthenticationFilter来处理身份验证请求。你可以参考Spring Security的源码来了解更多细节和实现方式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringSecurity自定义AuthenticationProvider和AuthenticationFilter](https://blog.csdn.net/weixin_34248849/article/details/93984642)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Spring Security笔记自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken](https://blog.csdn.net/weixin_33907511/article/details/85647330)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值