SpringSecurity 自定义表单登录

SpringSecurity 自定义表单登录

本篇主要讲解 在SpringSecurity中 如何 自定义表单登录 , SpringSecurity默认提供了一个表单登录,但是实际项目里肯定无法使用的,本篇就主要讲解如何自定义表单登录

?1.创建SpringSecurity项目

??1.1 使用IDEA

??先通过IDEA 创建一个SpringBoot项目 并且依赖SpringSecurity,Web依赖

??此时pom.xml会自动添加

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
</dependency>

?2.扩展 WebSecurityConfigurerAdapter

?WebSecurityConfigurerAdapter 是SpringSecurity 提供的用于我们扩展自己的配置

?实现WebSecurityConfigurerAdapter经常需要重写的:

1、configure(AuthenticationManagerBuilder auth);

2、configure(WebSecurity web);

3、configure(HttpSecurity http);

??2.1 默认 WebSecurityConfigurerAdapter 为我们提供了一些基础配置如下

protected void configure(HttpSecurity http) throws Exception {
    logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");

    http
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .formLogin().and()
        .httpBasic();
}

??2.2 创建自定义的 WebSecurityConfigurer

??1.formLogin() 开启表单登录,该方法会应用 FormLoginConfigurer 到HttpSecurity上,后续会被转换为对应的Filter
??2.loginPage() 配置自定义的表单页面
??3.authorizeRequests().anyRequest().authenticated(); 表示任何请求接口都要认证

@Configuration
@Slf4j
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable()
            .formLogin()
            .loginPage("/mylogin.html")
            .and()
            .authorizeRequests().anyRequest().authenticated();

    }
}   

??2.3 mylogin.html

<!DOCTYPE html>
<html lang="en">
  <head>
     <meta charset="UTF-8">
     <title>Title</title>
  </head>
  <body>
    <h1>标准登录页面</h1>
    <h3>表单登录</h3>

 <form action="/login" 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 colspan="2">
            <button type="submit">登录</button>
        </td>
    </tr>
   </table>
  </form>
</body>
</html>

?3.访问自定义登录页面(注意有重定向过多问题)

?启动项目 并且直接访问

http://localhost:8080

?会发现浏览器报 重定向次数过多,这是什么原因呢?

?这是因为 我们上面配置了 loginPage("/mylogin.html") ,但是这个路径它没有被允许访问,也就是当重定向到/mylogin.html路径后,还是会因为需要认证 被重定向道 /mylogin.html 导致该错误

?4.允许登录页面路径访问 antMatchers("/mylogin.html").permitAll()

?只需要在配置的地方 添加 .antMatchers("/mylogin.html").permitAll() 允许这个路径

    http.csrf().disable()
            .formLogin()
            .loginPage("/mylogin.html")
            .and()
            .authorizeRequests()
            .antMatchers("/mylogin.html").permitAll()
            .anyRequest().authenticated();

?再次访问,我们自定义的表单就显示出来了(忽略样式。。。)

?此时我们输入用户名 user 密码 : 控制台打印

Using generated security password: 6bf253eb-c785-42b6-b147-b0fe2971586e

?发现又跳转到 /mylogin.html页面,这是因为 当我们配置了 loginPage("/mylogin.html")之后 处理表单登录的过滤器它所拦截的请求就不再是 /login (默认是 /login) ,拦截的登录请求地址变成了 和 loginPage一样的 mylogin.html

?此时如果将 action地址改成 /mylogin.html ,那么再登录 就能成功

<form action="/mylogin.html" method="post">

?5.配置自定义登录接口路径 loginProcessingUrl

?由于我们上面配置了 loginPage ,则对应登录接口路径也变成了 loginPage所配置的 mylogin.html,但是当我们不想使用这个作为接口路径的时候,可以通过以下配置来修改

?通过 loginProcessingUrl 类配置处理登录请求的路径

 http.csrf().disable()
            .formLogin()
            .loginPage("/mylogin.html")
            .loginProcessingUrl("/auth/login")
            .and()
            .authorizeRequests()
            .antMatchers("/mylogin.html").permitAll()
            .anyRequest().authenticated();

?记得和action 对应

<form action="/auth/login" method="post">

?至此SpringSecurity自定义登录页面的配置 以及注意事项全部说完

6. 总结

本篇主要讲解 在SpringSecurity中 如何 自定义表单登, 是不是非常简单 ,但是也有一些要注意的点
1.扩展WebSecurityConfigurerAdapter
2.配置loginPage 页面路径
3.允许loginPage 页面路径 访问
4.配置登录请求的路径 loginProcessingUrl

个人博客地址: https://www.askajohnny.com 欢迎访问!
本文由博客一文多发平台 OpenWrite 发布!

原文地址:https://www.cnblogs.com/askajohnny/p/12229190.html

时间: 2024-08-01 18:09:42

SpringSecurity 自定义表单登录的相关文章

SpringBoot集成Spring Security(4)——自定义表单登录

通过前面三篇文章,你应该大致了解了 Spring Security 的流程.你应该发现了,真正的 login 请求是由 Spring Security 帮我们处理的,那么我们如何实现自定义表单登录呢,比如添加一个验证码- 源码地址:https://github.com/jitwxs/blog_sample 文章目录 一.添加验证码 1.1 验证码 Servlet 1.2 修改 login.html 1.3 添加匿名访问 Url二.AJAX 验证三.过滤器验证 3.1 编写验证码过滤器 3.2 注

SpringSecurity 默认表单登录页展示流程源码

SpringSecurity 默认表单登录页展示流程源码 本篇主要讲解 SpringSecurity提供的默认表单登录页 它是如何展示的的流程, 涉及 1.FilterSecurityInterceptor, 2.ExceptionTranslationFilter , 3.DefaultLoginPageGeneratingFilter 过滤器, 并且简单介绍了 AccessDecisionManager 投票机制 ?1.准备工作(体验SpringSecurity默认表单认证) ??1.1 创

sharepoint 2013基于AD的Form表单登录(二)——form登录页面自定义

配置好了sharepoint 2013基于AD的Form登录,只是成功了第一步,如何自定义登录页呢?特别是不要出现sharepoint2013自带登录页面,每次登录前还需要选择是否是form或者windows验证. 打开vs2012新建sharepoint 2013 project,在layouts目录下添加application page,页面命名为CustomLogin.aspx. 前台页面:为避免母版页中其他控件影响,注意继承的是simple.master         后台页面:继承F

sharepoint 2013基于AD的Form表单登录(四)——开发自定义登录过程需要引用文件路径。

1.Microsoft.IdentityModel.dll 位置 %ProgramFiles%\ReferenceAssemblies\Windows Identity Foundation\v3.5 2.Microsoft.SharePoint.IdentityModel.dll位置 C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.IdentityModel\v4.0_15.0.0.0__71e9bce111e94

如何使用自定义表单和自定义流程

以自定义一个请假流程作为示例. 1.首先,创建几个表单,这些自定义表单,是在请假流程中使用的. 表单管理->新建表单 我们新建了5个表单.vacation-request.vacation-department.vacation-hr.vacation-modify.vacation-finish 2.新建模型 模型列表->新建模型 流程设计完成后,点击保存.名称设置为vocation.返回模型列表,选中vocation,点击发布,可以看到发布Id显示有值,模型发布成功! 3.新建一个流程,跟

Angular19 自定义表单控件

1 需求 当开发者需要一个特定的表单控件时就需要自己开发一个和默认提供的表单控件用法相似的控件来作为表单控件:自定义的表单控件必须考虑模型和视图之间的数据怎么进行交互 2 官方文档 -> 点击前往 Angular为开发者提供了ControlValueAccessor接口来辅助开发者构建自定义的表单控件,开发者只需要在自定义表单控件类中实现ControlValueAccessor接口中的方法就可以实现模型和视图之间的数据交互 interface ControlValueAccessor { wri

Angular5+ 自定义表单验证器

Angular5+ 自定义表单验证器 Custom Validators 标签(空格分隔): Angular 首先阐述一下遇到的问题: 怎样实现"再次输入密码"的验证(两个controller值相等)(equalTo) 怎样反向监听(先输入"再次输入密码",后输入设置密码) 解决思路: 第一个问题,可以通过[AbstractControl].root.get([targetName])来取得指定的controller,然后比较他们的值. 第二个,可以通过[targe

织梦自定义表单发送邮件超简单版(支持QQ邮箱163邮箱)

环境要求 主机465端口是开启和放行的 php扩展openssl是开启的 php扩展sockets是开启的 1.QQ邮箱 或者 163邮箱 126邮箱 开启SMTP服务,拿到授权码,根据自己的来 QQ邮箱开启SMTP服务 和 获取授权码 163邮箱开启SMTP服务和 获取授权码 126邮箱开启SMTP服务,跟163一样. 2.网站后台 - 系统 - 系统基本参数 - 核心设置 是否启用SMTP方式发送邮件:是 SMTP服务器:ssl://smtp.163.com 或者 ssl://smtp.qq

Drupal创建自定义表单,上传文件代码

Drupal中创建自定义表单,用来上传文件,对上传文件做一些操作.以下是放在Module中的代码: 一.菜单建立表单路径 /** Implementation of hook_menu(). */ function moduleName_menu () { $items = array(); $items['admin/import'] = array( 'title' => 'title', 'page callback' => 'drupal_get_form', 'page argume