如何实现SpringSecurity基于自定义表单的登录功能?

2026-05-28 09:471阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1265个文字,预计阅读时间需要6分钟。

如何实现SpringSecurity基于自定义表单的登录功能?

本篇主要讲解在SpringSecurity中,如何自定义表单登录。

SpringSecurity默认提供了一个表单登录功能,但在实际项目中,可能无法直接使用。本篇将主要讲解如何自定义表单登录。

1. 创建SpringSecurity项目

首先,我们需要创建一个SpringSecurity项目。以下是创建SpringSecurity项目的步骤:

- 创建一个新的SpringBoot项目;- 添加SpringSecurity依赖;- 配置SpringSecurity。

2. 自定义表单登录

自定义表单登录主要包括以下步骤:

- 创建一个自定义的登录页面;- 创建一个自定义的登录控制器;- 配置SpringSecurity,使其使用自定义的登录页面和控制器。

以下是一个简单的示例:

java@Controllerpublic class CustomLoginController {

@GetMapping(/login) public String showLoginForm() { return customLogin; }

@PostMapping(/login) public String handleLogin(@RequestParam(username) String username, @RequestParam(password) String password) { // 处理登录逻辑 return redirect:/home; }}

在上面的示例中,我们创建了一个名为`CustomLoginController`的控制器,其中包含了两个方法:`showLoginForm`和`handleLogin`。`showLoginForm`方法用于展示自定义的登录页面,而`handleLogin`方法用于处理登录逻辑。

3. 配置SpringSecurity

最后,我们需要配置SpringSecurity,使其使用自定义的登录页面和控制器。以下是配置SpringSecurity的步骤:

- 创建一个配置类,继承`WebSecurityConfigurerAdapter`;- 重写`configure(HttpSecurity http)`方法,配置登录页面和登录成功后的跳转路径。

以下是一个简单的示例:

java@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .loginPage(/login) .successForwardUrl(/home) .permitAll(); }}

在上面的示例中,我们创建了一个名为`SecurityConfig`的配置类,其中重写了`configure(HttpSecurity http)`方法。我们配置了登录页面为`/login`,登录成功后的跳转路径为`/home`。

本篇主要讲解 在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

如何实现SpringSecurity基于自定义表单的登录功能?

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

 实现WebSecurityConfigurerAdapter经常需要重写的:

1、configure(AuthenticationManagerBuilder auth);

2、configure(WebSecurity web);

3、configure(HttpSecurity 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**

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

标签:实现

本文共计1265个文字,预计阅读时间需要6分钟。

如何实现SpringSecurity基于自定义表单的登录功能?

本篇主要讲解在SpringSecurity中,如何自定义表单登录。

SpringSecurity默认提供了一个表单登录功能,但在实际项目中,可能无法直接使用。本篇将主要讲解如何自定义表单登录。

1. 创建SpringSecurity项目

首先,我们需要创建一个SpringSecurity项目。以下是创建SpringSecurity项目的步骤:

- 创建一个新的SpringBoot项目;- 添加SpringSecurity依赖;- 配置SpringSecurity。

2. 自定义表单登录

自定义表单登录主要包括以下步骤:

- 创建一个自定义的登录页面;- 创建一个自定义的登录控制器;- 配置SpringSecurity,使其使用自定义的登录页面和控制器。

以下是一个简单的示例:

java@Controllerpublic class CustomLoginController {

@GetMapping(/login) public String showLoginForm() { return customLogin; }

@PostMapping(/login) public String handleLogin(@RequestParam(username) String username, @RequestParam(password) String password) { // 处理登录逻辑 return redirect:/home; }}

在上面的示例中,我们创建了一个名为`CustomLoginController`的控制器,其中包含了两个方法:`showLoginForm`和`handleLogin`。`showLoginForm`方法用于展示自定义的登录页面,而`handleLogin`方法用于处理登录逻辑。

3. 配置SpringSecurity

最后,我们需要配置SpringSecurity,使其使用自定义的登录页面和控制器。以下是配置SpringSecurity的步骤:

- 创建一个配置类,继承`WebSecurityConfigurerAdapter`;- 重写`configure(HttpSecurity http)`方法,配置登录页面和登录成功后的跳转路径。

以下是一个简单的示例:

java@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .loginPage(/login) .successForwardUrl(/home) .permitAll(); }}

在上面的示例中,我们创建了一个名为`SecurityConfig`的配置类,其中重写了`configure(HttpSecurity http)`方法。我们配置了登录页面为`/login`,登录成功后的跳转路径为`/home`。

本篇主要讲解 在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

如何实现SpringSecurity基于自定义表单的登录功能?

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

 实现WebSecurityConfigurerAdapter经常需要重写的:

1、configure(AuthenticationManagerBuilder auth);

2、configure(WebSecurity web);

3、configure(HttpSecurity 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**

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

标签:实现