springsecurity自定义登录页面的实现示例
作者:shuair
本文主要介绍了springsecurity自定义登录页面的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
spring security框架使用
环境
jdk版本:jdk 17
依赖
<!-- spring boot 版本 3.2.0 -->
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
登录认证
依赖
spring-boot-starter-security后,服务启动时,控制台日志会输出自动生成的密码日志信息如下
Using generated security password: 2d9c8697-8ebd-4be7-afea-a6bc922a7d5b This generated password is for development use only. Your security configuration must be updated before running your application in production.
默认的用户名为:
user
依赖
spring-boot-starter-security后,调用接口时就会进行登录认证,会跳转到默认的登录页面:/login,该页面是框架自带的- 页面加载较慢,样式加载不成功,但不影响功能使用。原因:会加载一些无法访问的css数据
- 输入默认的用户名和密码,进行登录认证,登录认证成功即可访问接口数据
- 该页面可自定义
默认的登出页面:/logout,该页面是框架自带的
- 页面加载较慢,样式加载不成功
- 该页面有一个确认登出按钮,点击后即可退出登录,调用接口时会重新进行登录认证
- 该页面可自定义
spring security自定义登录页面
使用浏览器自带的登录页面
添加一个配置类WebSecurityConfig.java,后续内容也会在此类中进行配置
package xin.yangshuai.springsecurity03.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
// @EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// 开启授权保护
http.authorizeRequests(new Customizer<ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry>() {
@Override
public void customize(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry) {
expressionInterceptUrlRegistry
// 对所有请求开启授权保护
.anyRequest()
// 已经认证的请求会被自动授权
.authenticated();
}
});
// 使用基本授权方式(浏览器自带的登录页面,无默认登出页)
http.httpBasic(Customizer.withDefaults());
return http.build();
}
}
http.authorizeRequests授权配置- 此方式由浏览器弹出默认登录页面进行登录认证
@EnableWebSecurity此处不用开启,依赖时已经自动开启
自定义登录页面
调整配置类WebSecurityConfig.java
package xin.yangshuai.springsecurity03.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
// @EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// 开启授权保护
http.authorizeRequests(new Customizer<ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry>() {
@Override
public void customize(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry) {
expressionInterceptUrlRegistry
// 对所有请求开启授权保护
.anyRequest()
// 已经认证的请求会被自动授权
.authenticated();
}
});
// 自定义登录页面
http.formLogin(new Customizer<FormLoginConfigurer<HttpSecurity>>() {
@Override
public void customize(FormLoginConfigurer<HttpSecurity> httpSecurityFormLoginConfigurer) {
// 自定义登录页,并且设置无需授权允许访问
httpSecurityFormLoginConfigurer.loginPage("/login").permitAll();
// 配置自定义表单的用户名参数,默认值:username
httpSecurityFormLoginConfigurer.usernameParameter("myusername");
// 配置自定义表单的密码参数,默认值:password
httpSecurityFormLoginConfigurer.passwordParameter("mypassword");
// 校验失败时跳转的地址,默认值:/login?error
httpSecurityFormLoginConfigurer.failureUrl("/login?error");
}
});
return http.build();
}
}
loginPage("/login")表示未授权时,会跳转到GET /login接口,因此需要对GET /login接口响应自定义的登录页面usernameParameter("myusername")表示可以自定义提交表单参数,在登录页面,请求登录认证接口默认是POST /login,用户名、密码的参数名可以自定义failureUrl("/login?error")表示认证失败,会跳转到GET /login?error接口,可以携带参数用来信息展示,实际上也是跳转到了登录页面,只是携带了参数
配置登录页面接口
package xin.yangshuai.springsecurity03.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class LoginController {
@GetMapping("login")
public String login(){
return "login";
}
}
配置登录页面
根据thymeleaf模板引擎,页面位置:src/main/resources/templates/login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<div th:if="${param.error}">用户名或密码错误</div>
<!--
使用动态参数,th:action="@{/login}"
开启防止csrf攻击时,会自动生成_csrf隐藏字段
-->
<form th:action="@{/login}" method="post">
<div>
<input type="text" name="myusername" placeholder="用户名">
</div>
<div>
<input type="password" name="mypassword" placeholder="密码">
</div>
<input type="submit" value="登录">
</form>
</body>
</html>
- 提交表单会默认加上
_csrf参数,防止csrf攻击,这个与配置有关 - 表单参数
myusername、mypassword要与配置保持一致 - 默认账号是
user,密码在项目启动时,会输出到控制台上 - 认证失败后,会跳回到登录页,并且携带
error参数,可以根据是否存在error参数来控制信息显示
到此这篇关于springsecurity自定义登录页面的实现示例的文章就介绍到这了,更多相关springsecurity自定义登录页面内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- 解析SpringSecurity自定义登录验证成功与失败的结果处理问题
- SpringSecurity动态加载用户角色权限实现登录及鉴权功能
- SpringBoot + SpringSecurity 短信验证码登录功能实现
- springboot+jwt+springSecurity微信小程序授权登录问题
- SpringSecurity OAuth2单点登录和登出的实现
- SpringSecurity 默认表单登录页展示流程源码
- springsecurity实现用户登录认证快速使用示例代码(前后端分离项目)
- SpringSecurity多表多端账户登录的实现
- SpringBoot如何整合Springsecurity实现数据库登录及权限控制
- SpringSecurity6.x多种登录方式配置小结
- SpringSecurity表单配置之登录成功及页面跳转原理解析
