尚硅谷springSecurity笔记

1.简介

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

spring是非常流行和成功的框架,springSecurity也是spring家族中的成员,springSecurity基于spring框架,提供了一套Web应用安全性的完整解决方案

关于安全方面的两个主要区域是“认证”和“授权”,一般来说,web应用的安全性包括用户认证和用户授权两个部分,这两点也是spring security重要核心功能

  1. 用户认证指的是:验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统,用户认证一般要求用户提供用户名和密码,系统通过校验用户名和密码来完成认证过程
  2. 用户授权值得是验证某个用户是否有权限执行某个操作。在一个系统中,不同给用户所具有的的权限是不同的。比如对一个文件来说,有的用户只能进行读取,而有的用户可以进行修改,一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。

2.同款框架

Shiro:Apache旗下的安全框架

springsecurity特点:

  • 和Spring无缝整合
  • 全面的权限控制
  • 专门为Web开发而设计
    • 旧版本不能脱离Web环境使用
    • 新版本对整个框架进行了分层抽取,分成了核心模块和Web模块,单独引入核心模块就可以脱离Web环境
  • 重量级(缺点)

Shiro特点:

  • 轻量级 Shiro主张的理念是把复杂的事情变简单。针对性能有更高要求的互联网应用有更好表现
  • 通用性
    • 好处:不局限于Web环境,可以脱离Web环境使用
    • 缺陷:在web环境下一些特定的需求需要手动编写代码定制

3.入门案例

  1. 创建一个springboot的项目,并将版本修改为2.6.5版本。创建过程中不需要导入任何的组件

  2. 导入依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <!--注意这里需要改为web的依赖-->
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    
        <!--springSecurity相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    
    </dependencies>
    
  3. 编写controller进行测试

    @RestController
    @RequestMapping(value = "/test")
    public class TestController {
        @GetMapping(value = "/hello")
        public String hello() {
            return "hello spring security";
        }
    }
    

导入springSecurity之后,不需要任何的配置直接在浏览器地址访问这个服务就会弹出以下画面

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VtRAX7Pe-1655117009769)(%E5%B0%9A%E7%A1%85%E8%B0%B7springgSecurity%E7%AC%94%E8%AE%B0.assets/image-20220610181136247.png)]

默认的用户名是user密码是在idea运行窗口中生成的

Using generated security password: 9f8c9a84-f714-4583-97ab-4509711ca1ba

输入之后点击按钮就可以成功跳转到我们需要的页面了

4.基本原理

springsecurity本质上是一个过滤器链,有很多的过滤器,启动是可以获取到过滤器链

查看源码

FilterSecurityInterceptor:是一个方法级的权限过滤器,基本位于过滤链的最底部

ExceptionTranslationFilter:是一个异常过滤器,用来处理在认证过程中抛出的异常

UsernamePasswordAuthenticationFilter:对/login的POST请求做拦截,校验表单中用户名,密码

5.更改用户认证的密码

需要了解两个接口

  1. UserDetailsService
  2. PasswordEncoder

5.1.UserDetailsService接口

当什么都没有配置的时候,账号和密码是由springsecurity定义生成的。而在实际项目中账号和密码都是从数据库中查询出来的,所以我们需要通过自定义逻辑控制认证逻辑

查询数据库用户名和密码的过程就写在这个接口的实现类中

步骤

  1. 创建一个类继承UsernamePasswordAuthenticationFilter,重写三个方法attemptAuthentication,successfulAuthentication,unsuccessfulAuthentication
  2. 创建类实现UserDetailsService接口,编写查询数据库过程,返回User对象,这个User对象是springsecurity提供的User对象

5.2.PasswordEncoder接口

这是一个对密码进行加密的接口,用于对User对象里面密码加密

接口的API

//表示把参数按照特定的解析规则进行解析
String encode(CharSequence rawPassword);

//表示验证从存储中获取的编码密码与编码后提交的原始密码会否匹配,如果密码匹配,返回true;如果不匹配,则返回false,第一个参数表示需要被解析的密码 第二个参数表示存储的密码
boolean matches(CharSequence rawPassword,String encodedPassword);

//表示如果解析的密码能够再次进行解析且达到更安全的结果则返回true,否则返回false,默认返回false
default boolean upgradeEncoding(String encodedPassword){
    return false;
} 

BCryptPasswordEncoder是spring官方推荐的密码解析器

BCryptPasswordEncoder是对bcrypt强散列方法的具体实现,是基于Hash算法实现的单向加密,可以通过strength控制加密强度,默认为10

6.使用配置文件对用户名和密码进行修改

在springboot的配置文件中进行修改

spring.security.user.name=root
spring.security.user.password=123456

修改之后进行测试

7.使用配置类对用户名和密码进行修改

创建一个配置类SpringSecurityConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //对密码进行加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode("123456");
        //设置用户名和密码
        auth.inMemoryAuthentication()
                .withUser("root").password(password).roles("admin");
    }

    //需要BCryptPasswordEncoder对象,所以我们需要将其加入到容器中
    @Bean
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }
}

8.自定义编写实现类对用户名和密码进行修改(常用)

需要编写一个实现类,实现WebSecurityConfigurerAdapter接口,之后编写一个实现类,实现UserDetailsService,返回User对象

第一步

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }

    @Bean
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }
}
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
        return new User("root",new BCryptPasswordEncoder().encode("123"),auths);
    }
}

9.连接数据库完成认证功能

sql

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xwuv99m3-1655117009771)(%E5%B0%9A%E7%A1%85%E8%B0%B7springSecurity%E7%AC%94%E8%AE%B0.assets/image-20220612130921069.png)]

创建实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@Data
@NoArgsConstructor
public class Users {
    private Integer id;
    private String username;
    private String password;
}

整合mybatis-plus

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hty.springsecurity.eneity.Users;
@Repository
public interface UsersMapper extends BaseMapper<Users> {

}

编写MySecurityConfig类判断账号和密码

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hty.springsecurity.eneity.Users;
import com.hty.springsecurity.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Users user = usersMapper.selectOne(wrapper);

        //判断
        if(user == null){
            //抛出异常
            throw new UsernameNotFoundException("用户名不存在!");
        }

        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
        //设置的值为查询数据库返回users对象
        return new User(user.getUsername(),new BCryptPasswordEncoder().encode(user.getPassword()),auths);
    }
}

10.自定义用户登陆页面

  1. 在配置类中重写另一个configure方法

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()  //自定义自己编写的登录页面
            .loginPage("/login.html")//登陆页面的地址
            .loginProcessingUrl("/login")//登陆页面的请求地址
            .defaultSuccessUrl("/test/hello").permitAll()//登陆成功后跳转的页面
            .and()
            .authorizeRequests()//配置权限 哪些地址需要认证,哪些不需要
            .antMatchers("/","/test/hello","/login").permitAll()//设置哪些路径可以直接访问 不需要认证
            .anyRequest().authenticated()
            .and().csrf().disable();//关闭csrf防护
    }
    
  2. 创建相关的页面和controller 注:表单提交使用post controller中使用GetMapping

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>登陆</title>
    </head>
    <body>
    <form action="/user/login" method="POST">
        用户名:<input type="text" name="username"/><br/>
        密码:<input type="password" name="password"/><br/>
        <input type="submit" value="登陆"/>
    </form>
    </body>
    </html>
    
    @GetMapping("/index")
    public String index() {
        return "hello index";
    }
    
  3. 测试

11.基于角色或权限进行访问控制

11.1.hasAuthority方法-单个用户权限

如果当前的主体具有指定的权限,则返回true,否则返回false

  1. 在配置类设置当前访问地址有哪些权限

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()  //自定义自己编写的登录页面
            .loginPage("/login.html")//登陆页面的地址
            .loginProcessingUrl("/user/login")//登陆页面的请求地址
            .defaultSuccessUrl("/test/index").permitAll()//登陆成功后跳转的页面
            .and()
            .authorizeRequests()//配置权限 哪些地址需要认证,哪些不需要
            .antMatchers("/","/test/hello","/login").permitAll()//设置哪些路径可以直接访问 不需要认证
            .antMatchers("/test/index").hasAuthority("admins")//当前登陆用户只有具有admins这个权限才可以访问这个路径
            .anyRequest().authenticated()
            .and().csrf().disable();//关闭csrf防护
    }
    
  2. 在UserDetailsService中,对权限进行修改

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Users user = usersMapper.selectOne(wrapper);
    
        //判断
        if(user == null){
            //抛出异常
            throw new UsernameNotFoundException("用户名不存在!");
        }
    
        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("admins");
        //设置的值为查询数据库返回users对象
        return new User(user.getUsername(),new BCryptPasswordEncoder().encode(user.getPassword()),auths);
    }
    

11.2.hasAnyAuthority方法-多个用户权限

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.formLogin()  //自定义自己编写的登录页面
        .loginPage("/login.html")//登陆页面的地址
        .loginProcessingUrl("/user/login")//登陆页面的请求地址
        .defaultSuccessUrl("/test/index").permitAll()//登陆成功后跳转的页面
        .and()
        .authorizeRequests()//配置权限 哪些地址需要认证,哪些不需要
        .antMatchers("/","/test/hello","/login").permitAll()//设置哪些路径可以直接访问 不需要认证
        .antMatchers("/test/index").hasAnyAuthority("admins","manager")
        .anyRequest().authenticated()
        .and().csrf().disable();//关闭csrf防护
}

11.3.hasRole方法

如果用户具备给定角色就允许访问,否则403

如果当前主题具有指定的角色,则返回true

  1. 配置类

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()  //自定义自己编写的登录页面
            .loginPage("/login.html")//登陆页面的地址
            .loginProcessingUrl("/user/login")//登陆页面的请求地址
            .defaultSuccessUrl("/test/index").permitAll()//登陆成功后跳转的页面
            .and()
            .authorizeRequests()//配置权限 哪些地址需要认证,哪些不需要
            .antMatchers("/","/test/hello","/login").permitAll()//设置哪些路径可以直接访问 不需要认证
            .antMatchers("/test/index").hasRole("sale")
            .anyRequest().authenticated()
            .and().csrf().disable();//关闭csrf防护
    }
    
  2. 修改service方法

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Users user = usersMapper.selectOne(wrapper);
    
        //判断
        if(user == null){
            //抛出异常
            throw new UsernameNotFoundException("用户名不存在!");
        }
    	
        //要注意这里使用的是ROLE_前缀加上角色的类型(看源码)
        List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_sale");
        //设置的值为查询数据库返回users对象
        return new User(user.getUsername(),new BCryptPasswordEncoder().encode(user.getPassword()),auths);
    }
    

11.4.hasAnyRole方法

用法同hasAnyAuthority方法

12.自定义403页面

我们需要在配置类中进行配置,在configure中加入

//配置403页面
http.exceptionHandling().accessDeniedPage("/403_error.html");

即可配置完成403页面

13.springsecurity中的注解

使用注解前需要开启注解功能

@EnableGlobalMethodSecurity(securedEnabled = true)

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;

@SpringBootApplication
@MapperScan("com.hty.springsecurity.mapper")
//开启注解支持 这个注解也可以放在配置类上面
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SpringSecurityApplication {

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

13.1.@Secured

判断是否具有角色,需要注意:这里匹配字符串需要添加前缀"ROLE_"

  1. 在controller的方法上加上这个注解

    @Secured({"ROLE_sale","ROLE_manager"})
    @GetMapping("/update")
    public String update() {
        return "hello update";
    }
    
  2. 我们需要保证在UserDetailsService中含有这个角色才可以

  3. 测试

13.2.@PreAuthorize

适合进入方法前的权限验证,该注解可以将登陆用户的roles/premissions参数传到方法中

使用的位置:方法上

@PreAuthorize("hasAnyAuthority('admins')")
@GetMapping("/update")
public String update() {
    return "hello update";
}

13.3.@PostAuthorize

这个注解在方法执行之后再进行权限验证,适合验证带有返回值的权限

使用位置:方法上

@PostAuthorize("hasAnyAuthority('aaaa')")//这里的这个权限并不存在 所以会跳转到403页面
@GetMapping("/update")
public String update() {
    System.out.println("update 方法");//该输出会输出到控制台上
    return "hello update";
}

13.4.@PostFilter

权限验证之后对数据进行过滤,留下用户名是admin1的数据

表达式中的filterObject引用的是方法返回值List中的某一个元素

@GetMapping(value = "/getAll")
@PostAuthorize("hasAnyAuthority('admins')")
@PostFilter("filterObject.username == 'admin1'")
@ResponseBody
public List<Users> getAllUser(){
    ArrayList<Users> list = new ArrayList<>();
    list.add(new Users(1,"admin1","6666"));
    list.add(new Users(2,"admin2","8888"));

    return list;
}

13.5.@PreFilter

进入控制器前对数据进行过滤

14.用户注销

我们需要在配置类中加入一个退出的地址

//配置退出请求地址
http.logout().logoutUrl("/logout").logoutSuccessUrl("/index").permitAll();

15.基于数据库的记住我(自动登陆)

首先创建一个表

create table persistent_logins (
    username varchar(64) not null, 
    series varchar(64) primary key, 
    token varchar(64) not null, 
    last_used timestamp not null
);

然后在配置类中注入数据源,配置操作数据库对象

//注入数据源
@Autowired
private DataSource dataSource;

//操作数据库的对象
@Bean
public PersistentTokenRepository persistentTokenRepository(){
    JdbcTokenRepositoryImpl tokenRepositoryImpl = new JdbcTokenRepositoryImpl();
    tokenRepositoryImpl.setDataSource(dataSource);
    //        tokenRepositoryImpl.setCreateTableOnStartup(true);//自动创建表
    return tokenRepositoryImpl;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    //配置403页面
    http.exceptionHandling().accessDeniedPage("/403_error.html");

    //配置退出请求地址
    http.logout().logoutUrl("/logout").logoutSuccessUrl("/test/hello").permitAll();

    http.formLogin()  //自定义自己编写的登录页面
        .loginPage("/login.html")//登陆页面的地址
        .loginProcessingUrl("/user/login")//登陆页面的请求地址
        .defaultSuccessUrl("/success.html").permitAll()//登陆成功后跳转的页面
        .and()
        .authorizeRequests()//配置权限 哪些地址需要认证,哪些不需要
        .antMatchers("/","/test/hello","/login").permitAll()//设置哪些路径可以直接访问 不需要认证
        .antMatchers("/test/index").hasRole("sale")
        .anyRequest().authenticated()
        
        .and().rememberMe().tokenRepository(persistentTokenRepository())
        .tokenValiditySeconds(60)//以s为单位 设置有效时长
        .userDetailsService(userDetailsService)
        
        .and().csrf().disable();//关闭csrf防护
}

前端的表单信息

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陆</title>
</head>
<body>
<form action="/user/login" method="POST">
    用户名:<input type="text" name="username"/><br/>
    密码:<input type="password" name="password"/><br/>
    <input type="checkbox" name="remember-me"> <!--自动登陆-->
    <input type="submit" value="登陆"/>
</form>
</body>
</html>

16.CSRF理解

跨站请求伪造(英语:Cross-site request forgery),也被称为 one-click attack 或者 session riding,通常缩写为 CSRF 或者 XSRF, 是一种挟制用户在当前已登录的Web应用程序上执行非本意的操作的攻击方法。跟跨网站脚本(XSS)相比,XSS 利用的是用户对指定网站的信任,CSRF 利用的是网站对用户网页浏览器的信任。

跨站请求攻击,简单的说,是攻击者通过一些技术手段欺骗用户的浏览器去访问一个自己曾经认证过的网站并运行一些操作(如发邮件,发消息,甚至财产操作如转账和购买商品)由于浏览器曾经认证过,所以被访问的网站会认为是真正的用户操作而去运行。这用了web中用户身份验证的一个漏洞,简单的身份验证只能保证请求发自某个用户的浏览器,却不能保证请求本身是用户自愿发出的

从spring security4.0开始,默认情况下会开启CSRF保护,以防止CSRF攻击应用程序,spring security csrf会针对patch,post,put,delete方法进行保护

使用方法就是在配置类中添加配置

.and().csrf().disable();//关闭csrf防护
将这个语句注释掉即可

在登陆页面的表单中加入一个隐藏域

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登陆</title>
</head>
<body>
<form action="/user/login" method="POST">
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
    用户名:<input type="text" name="username"/><br/>
    密码:<input type="password" name="password"/><br/>
    <input type="checkbox" name="remember-me">记住我 <br> <!--自动登陆-->
    <input type="submit" value="登陆"/>
</form>
</body>
</html>

,post,put,delete方法进行保护

使用方法就是在配置类中添加配置

.and().csrf().disable();//关闭csrf防护
将这个语句注释掉即可

在登陆页面的表单中加入一个隐藏域

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登陆</title>
</head>
<body>
<form action="/user/login" method="POST">
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
    用户名:<input type="text" name="username"/><br/>
    密码:<input type="password" name="password"/><br/>
    <input type="checkbox" name="remember-me">记住我 <br> <!--自动登陆-->
    <input type="submit" value="登陆"/>
</form>
</body>
</html>
  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值