1.概述
Spring Security 是一个功能强大且高度可定制的身份验证和访问控制框架,专注于为 Java 应用程序提供
身份验证和授权的框架。
具体使用:
我们仅需要引入spring-boot-starter-security模块,进行少量配置,即可实现强大的安全管理。
特殊的类:
WebSecurityConfigurerAdapter:自定义Security策略
AuthenticationManagerBuilder:自定义认证策略
Spring Security的两个主要目标是"认证"和"授权"。
"认证"(Authentication)
"授权"(Authorization)
2.用户认证和授权
引入POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--thymeleaf-springsecurity5整合包 可以在thymeleaf进行security操作 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
package com.ldd.springbootsecurity.config;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* @author 小李同学
* @pageage com.ldd.springbootsecurity.config
* @date 2021/8/20
* @week 星期五
* @action
*/
/**
* 开启WebSecurity
*/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
/**
* 请求授权规则配置
* .antMatchers("/").permitAll() 所有请求都可通过
*/
http.authorizeRequests()
.antMatchers("/").permitAll() /*所有请求都可以通过*/
.antMatchers("/vip1/**").hasRole("vip1")/*访问/vip1/**需要有vip1权限*/
.antMatchers("/vip2/**").hasRole("vip2")/*访问/vip2/**需要有vip2权限*/
.antMatchers("/vip3/**").hasRole("vip3");/*访问/vip3/**需要有vip3权限*/
/**
* 没有访问权限默认会到登录页面
* formLogin(开启登录的页面)
* loginPage定制登录页面(不使用security提供的登录页面,而使用自定义)
* loginProcessingUrl登录页面点击提交的后面URL路径,不需要自己实现后台逻辑,使用security
* usernameParameter登录页面传递后台用户名属性,默认username,也可以在此处自定义(例如:uname)
* passwordParameter登录页面传递后台密码属性,默认password,也可以在此处自定义(例如:pwd)
*/
http.formLogin().loginPage("/toLogin").usernameParameter("username").passwordParameter("password").loginProcessingUrl("/login");
/**
* 退出
* logoutSuccessUrl 退出登录后跳转URL地址
*/
http.logout().logoutSuccessUrl("/");
/**
* 使用security记住我功能
* rememberMeParameter
*/
http.rememberMe().rememberMeParameter("remember");
}
/**
* 认证用户
*
* @param auth
* @throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
/**
* 认证用户可以选择从内存或者数据库获取
* inMemoryAuthentication内存认证
* passwordEncoder选择密文加密
*/
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("ldd").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1")
.and()
.withUser("sz").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2");
/**
* jdbc模板
*/
/*auth.jdbcAuthentication()
.dataSource() *//* DataSource数据源*//*
*//*
默认的schema 加载的是org/springframework/security/core/userdetails/jdbc/users.ddl
create table users 创建users
create table authorities 创建authorities
*//*
.withDefaultSchema()
.withUser("user").password("password").roles("vp1")
.and()
.withUser("user").password("password").roles("vp2");*/
}
}
3.源码分析
首先是经过用户名密码认证过滤器(UsernamePasswordAuthenticationFilter),而且请求方式必须是POST
从request获取用户名和密码。 最终认证成功后,会处理一些与session相关的方法(比如将认证信息存到session等操作)。
转载如下:
https://mp.csdn.net/mp_blog/creation/editor/119823202