计算机系统应用教程网站

网站首页 > 技术文章 正文

Spring-Security-认证认识篇 spring security app认证token

btikc 2024-09-25 15:18:06 技术文章 24 ℃ 0 评论

1.什么是

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.
SpringSecurity是一个功能强大且高度定制的身份验证和访问控制框架。这是保护基于Spring的应用程序的事实上的标准。

spring security 的核心功能主要包括:

认证: Authentication (你是谁)

认证是否为合法用户,简单的说是登录。一般为匹对用户名和密码,即认证成功

授权 Authorization (你能干什么)

授权管理,是在已认证的前提下。用户在认证后,根据用户的不同权限,开放不同的资源。

2.认证入门案例

2.1 搭建springboot项目

添加依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies>

2.2 Controller

2.3 启动类

@SpringBootApplication
public class BootApp {
public static void main(String[] args) {
SpringApplication.run(BootApp.class, args);
}
}

2.4 启动项目,访问

浏览器输入 http://127.0.0.1:8080/get

当我们项目依赖 spring-boot-starter-security 的启动stater,则认证默认生效,且springSecurity提供了默认的登录页面,只有输入正确的输入用户\密码后才可以访问系统资源.

用户名默认:user

密码:项目启动时在控制台输出随机密码

2.5 配置中设置登录名和密码

再次启动,就能使用 user 和密码 123 登录了

3 原理探索

3.1 概要流程

Spring Security核心就是一组过滤器链,内部包含了提供各种功能的过滤器,项目启动后将会自动配置。

上图只是列出了常见的过滤器


1.UsernamePasswordAuthenticationFilter 用来处理表单用户和密码登录。 
2FilterSecurityInterceptor 获取所配置资源访问的授权信息,决定其是否有权限。
3.ExceptionTranslationFilter 用来捕获FilterSecurityInterceptor 抛出的异常,比如,没有登录,则引导到登录页面

3.2 详细的认证的流程

Authentication:封装了用户登录的信息

AuthenticationManager:定义了认证Authentication的方法

userDetailService接口:定义了根据用户名查询用户信息的方法

UserDetail: 通过userDetailService的查询用户的方法后,封装成UserDetail 对象返回,然后将对象封装到Authentication中

4.自定义用户认证逻辑

4.1处理用户信息获取逻辑

spring把 用户的信息会被封装在UserDetailService,用户需要实现该接口,处理自己的业务逻辑

@Component
public class MyUserDetailService implements UserDetailsService {
@Autowired
PasswordEncoder passwordEncoder;//密码处理类
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
String pwd =passwordEncoder.encode("123456"); //password 应该从数据库获取
//第三个参数:该用户的权限。--正常从数据库获取
return new User(s,pwd, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
}
}

根据用户输入的用户名,获取用户的信息(比如从数据库中获取),进行校验,如果通过则封装到session中,如果没有该用户获取校验不通过,侧抛出异常。

4.2 注册密码处理类

@Configuration
public class MySecurityConfig {
//注册PasswordEncoder 
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}

5用户自定义登录页面

5.1 创建登录页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<center>
<h3>用户登录</h3>
<form action="/user/auth" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<button type="submit">登录</button>
</form>
</center>
</body>
</html>

注意:name和password 必须是 username 和 password

5.2 配置类继承WebSecurityConfigurerAdapter

@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter{
//注册PasswordEncoder
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().loginPage("/login.html")//登录页面地址
.loginProcessingUrl("/user/auth")//登录页面提交的地址
.and()
.authorizeRequests()
.antMatchers("/login.html").permitAll()//放行的页面
.anyRequest().authenticated().and().csrf().disable();
}

}

5.3 效果

输入正确的密码,就可以访问系统的资源了,至此,我们应该对spring-security的认证应该有了一定的认识。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表