SpringSecurity 新版2.7以上 快速入门

SpringSecurity

快速入门

1、导入依赖


    org.springframework.boot
    spring-boot-starter-security

2、测试三种权限

2.7以前的版本

package com.mhy.security.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;

@EnableWebSecurity
public class OldSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //请求的规则
        http.authorizeHttpRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        // 没有权限跳到login页
        http.formLogin();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("shuisanya").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1")
                .and()
                .withUser("haha").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3");

    }
}

2.7以后的版本

package com.mhy.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@EnableWebSecurity
@Configuration
public class NewSecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        // @formatter:off
//        http
//                .authorizeHttpRequests()
//                .antMatchers("/").permitAll()
//                .antMatchers("/level1/**").hasRole("vip1")
//                .antMatchers("/level2/**").hasRole("vip2")
//                .antMatchers("/level3/**").hasRole("vip3");

        http.authorizeHttpRequests((authorize) -> {
            authorize.antMatchers("/").permitAll()
                    .antMatchers("/level1/**").hasRole("vip1")
                    .antMatchers("/level2/**").hasRole("vip2")
                    .antMatchers("/level3/**").hasRole("vip3");
        });

        http.formLogin();
        // @formatter:on
        return http.build();
    }

    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails user1 = User.withDefaultPasswordEncoder()
                                .username("shuisanya")
                                .password("123456")
                                .roles("vip1","vip2","vip3")
                                .build();
        UserDetails user2 = User.withDefaultPasswordEncoder()
                                .username("root")
                                .password("123456")
                                .roles("vip2","vip3")
                                .build();
        UserDetails user3 = User.withDefaultPasswordEncoder()
                                .username("haha")
                                .password("123456")
                                .roles("vip1")
                                .build();
        return new InMemoryUserDetailsManager(user1,user2,user3);
    }
}

3、测试自己设置加密

@Bean
public UserDetailsService userDetailsService() {
    UserDetails userDetails = User.withUsername("123456")
        .passwordEncoder(new Pbkdf2PasswordEncoder()::encode)
        .password("123456")
        .roles("vip2")
        .build();
    UserDetails user1 = User.withUsername("shuisanya")
        .passwordEncoder(new Pbkdf2PasswordEncoder()::encode)
        .password("123456")
        .roles("vip1","vip2","vip3")
        .build();
    UserDetails user2 = User.withUsername("root")
        .passwordEncoder(new Pbkdf2PasswordEncoder()::encode)
        .password("123456")
        .roles("vip2","vip3")
        .build();
    UserDetails user3 = User.withUsername("haha")
        .passwordEncoder(new Pbkdf2PasswordEncoder()::encode)
        .password("123456")
        .roles("vip1")
        .build();
    return new InMemoryUserDetailsManager(userDetails,user1,user2,user3);
}

@Bean
public PasswordEncoder passwordEncoder(){
    return new Pbkdf2PasswordEncoder();
}

连接数据库使用

导入依赖,这里使用mybatisPlus


     mysql
     mysql-connector-java

     com.baomidou
     mybatis-plus-boot-starter
     3.5.2

编写配置文件

spring.thymeleaf.cache=false

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
开启逻辑删除
mybatis-plus.global-config.db-config.logic-delete-field=deleted
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

实现UserDetailsService接口的编写

@Service("userDetailsServiceImpl")
public class UserDetailsServiceImpl implements UserDetailsService {

    private UserMapper userMapper;
    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        com.mhy.security.pojo.User user = userMapper.selectOne(wrapper);

        if (user == null){
            throw new UsernameNotFoundException("该用户不存在!");
        }

        List vip2 = AuthorityUtils.commaSeparatedStringToAuthorityList("vip2"); //权限 一般是从数据库来实现

        return new User(username,PasswordEncoderUtils.encode(user.getPassword()),vip2);
}

三种授权的配置

三种授权的配置

首页

SpringSecurity 新版2.7以上 快速入门

第一种 hasAuthority

这个参数只能配置一种授权

场景:

  • 访问/level1/**这个下面的所有只能是vip1才可以访问
  • 访问/level2/**这个下面的所有只能是vip2才可以访问
  • 访问/level3/**这个下面的所有只能是vip3才可以访问
@EnableWebSecurity
@Configuration
public class NewSecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests((authorize) -> {
            authorize
                    .antMatchers("/").permitAll()
                    .antMatchers("/level1/**").hasAuthority("vip1")
                    .antMatchers("/level2/**").hasAuthority("vip2")
                    .antMatchers("/level3/**").hasAuthority("vip3");
        });

        http
                .formLogin() //开启登入功能
                .loginPage("/toLogin") //开启等去去的页面,去自己的页面
                .loginProcessingUrl("/login"); //登录请求的方法,这个是spring security帮你做

        http.csrf().disable(); //关闭csrf防火墙

        http.logout().logoutSuccessUrl("/"); //退出登录的页面

        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new Pbkdf2PasswordEncoder();
    }

}

第二种 hasRole

这个参数只能配置一种授权,但它会默认给你配置的授权名称前加一个 ROLE_

AuthorityAuthorizationManager类中的源码

private static final String ROLE_PREFIX = "ROLE_";
public static  AuthorityAuthorizationManager hasRole(String role) {
    Assert.notNull(role, "role cannot be null");
    return hasAuthority(ROLE_PREFIX + role);
}

场景:

  • 访问/level1/**这个下面的所有只能是vip1才可以访问
  • 访问/level2/**这个下面的所有只能是vip2才可以访问
  • 访问/level3/**这个下面的所有只能是vip3才可以访问

配置类

@EnableWebSecurity
@Configuration
public class NewSecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests((authorize) -> {
            authorize
                    .antMatchers("/").permitAll()
                    .antMatchers("/level1/**").hasRole("vip1") // 这里实际上是ROLE_vip1
                    .antMatchers("/level2/**").hasRole("vip2") // 这里实际上是ROLE_vip1
                    .antMatchers("/level3/**").hasRole("vip3");// 这里实际上是ROLE_vip3
        });
        http
                .formLogin() //开启登入功能
                .loginPage("/toLogin") //开启等去去的页面,去自己的页面
                .loginProcessingUrl("/login"); //登录请求的方法,这个是spring security帮你做

        http.csrf().disable(); //关闭csrf防火墙

        http.logout().logoutSuccessUrl("/"); //退出登录的页面

        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new Pbkdf2PasswordEncoder();
    }

}

使用在编写实现UserDetailsService时候需要注意

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("username",username);
com.mhy.security.pojo.User user = userMapper.selectOne(wrapper);
if (user == null){
throw new UsernameNotFoundException("该用户不存在!");
}

List vip2 = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_vip2");  //注意

return new User(username,PasswordEncoderUtils.encode(user.getPassword()),vip2);
}

第三种 hasAnyAuthority

配置多个参数

场景:

  • 访问/level1/**这个下面的所有是vip1才可以访问
  • 访问/level2/**这个下面的所有是vip1,vip2才可以访问
  • 访问/level3/**这个下面的所有是vip1,vip2,vip3才可以访问

配置类:

@EnableWebSecurity
@Configuration
public class NewSecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests((authorize) -> {
            authorize
                    .antMatchers("/").permitAll()
                    .antMatchers("/level1/**").hasAnyAuthority("vip1","vip2","vip3")
                    .antMatchers("/level2/**").hasAnyAuthority("vip2","vip3")
                    .antMatchers("/level3/**").hasAnyAuthority("vip3");
        });

        http
                .formLogin() //开启登入功能
                .loginPage("/toLogin") //开启等去去的页面,去自己的页面
                .loginProcessingUrl("/login"); //登录请求的方法,这个是spring security帮你做

        http.csrf().disable(); //关闭csrf防火墙
        http.logout().logoutSuccessUrl("/"); //退出登录的页面
        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new Pbkdf2PasswordEncoder();
    }
}

第四种 hasAnyAuthority

配置多个参数

这个参数只能配置一种授权,但它会默认给你配置的授权名称前加一个 ROLE_

场景:

  • 访问/level1/**这个下面的所有是vip1才可以访问
  • 访问/level2/**这个下面的所有是vip1,vip2才可以访问
  • 访问/level3/**这个下面的所有是vip1,vip2,vip3才可以访问

配置类

@EnableWebSecurity
@Configuration
public class NewSecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests((authorize) -> {
            authorize
                    .antMatchers("/").permitAll()
                    .antMatchers("/level1/**").hasAnyRole("vip1","vip2","vip3")
                    .antMatchers("/level2/**").hasAnyRole("vip2","vip3")
                    .antMatchers("/level3/**").hasAnyRole("vip3");
        });

        http
                .formLogin() //开启登入功能
                .loginPage("/toLogin") //开启等去去的页面,去自己的页面
                .loginProcessingUrl("/login"); //登录请求的方法,这个是spring security帮你做

        http.csrf().disable(); //关闭csrf防火墙

        http.logout().logoutSuccessUrl("/"); //退出登录的页面

        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new Pbkdf2PasswordEncoder();
    }
}

Original: https://www.cnblogs.com/shuisanya/p/16601911.html
Author: 水三丫
Title: SpringSecurity 新版2.7以上 快速入门

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/621642/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Springboot&&LinuX

    一、现学习规划 Springboot 概念介绍:全新框架,简化Spring应用的初始搭建以及开发过程 Springboot核心:自动装配 微服务阶段 javase:OOP mysq…

    Java 2023年6月8日
    068
  • 对象缓存服务的思考和实现

    写在前面 目前在很多业务中,存储都大量的依赖了云存储,比如阿里云的 oss、华为云的 obs 等。但是如果有大量的上传/下载任务,云存储上的网络 I/0 就变成了一个很大的瓶颈。 …

    Java 2023年6月7日
    082
  • Linux下用Docker部署接口安全的运行环境

    背景:MySQL 数据库运行在宿主机上(Linux) 需求:Redis、服务、页面分别运行在独立的docker中,并处于同一网络,容器内部重要目录要挂载在物理目录,保证数据安全 方…

    Java 2023年6月8日
    095
  • CSP_202206-2_寻宝!大冒险!

    CSP_202206-2_寻宝!大冒险 题目链接 思路 相当于判断两个有限集合AB之间是不是满射和单射,只需要保证以下两点 A和B元素个数相等 A中每个元素都能通过映射(\psi)…

    Java 2023年6月7日
    098
  • dataGrid放在tab里的时候不会发送请求原因

    dataGrid放在tab里的时候不会发送请求原因: 标签从里取出来放到内。 标签:页面被调用时执行(页面加载前),:页面加载后调用。 因为head比body先创建,所以你在bod…

    Java 2023年6月6日
    073
  • pyhon——进程线程、与协程基础概述

    一直以来写博客都是实用主义者,只写用法,没信心写原理,但是每一次写作业的过程都有一种掘地三尺的感觉,终于,写博客困难症重症患者经历了漫长的思想斗争,还是决定把从网上淘到的各种杂货和…

    Java 2023年5月30日
    080
  • 喜讯!云效度量能力获信通院先进级评估

    6月16日-17日,由中国信息通信研究院(以下简称”中国信通院”)主办的2022精益软件工程大会以线上形式成功举行。会上,中国信通院云大所所长何宝宏公布了软…

    Java 2023年6月8日
    088
  • 【Java面试】这也太卷了~面试竟然问,HTTP协议和RPC协议的区别

    “Http协议和RPC协议有什么区别?”最近很多人问我这个问题,他们都不知道怎么回答。今天我们就来了解一下这个问题的高手回答。另外,我把文字版本的内容整理到…

    Java 2023年6月16日
    086
  • Spring5 扩展篇之自定义xml标签

    本篇文章主要讲一下Spring 如何取自定义自己的XML标签: 1. 首先要自定义自己的XSD文件 说明: 首先这个文件最好建立在静态资源 resource文件夹下,我为了方便都将…

    Java 2023年6月7日
    077
  • /ConcurrencyThreadGroup,非预先启动线程

    1.文档 https://jmeter-plugins.org/wiki/ConcurrencyThreadGroup/ 优点:逐步启动线程减少内存占用,jmeter默认线程组是先…

    Java 2023年5月29日
    075
  • 【力扣】1720. 解码异或后的数组

    未知 整数数组 arr 由 n 个非负整数组成。 经编码后变为长度为 n – 1 的另一个整数数组 encoded ,其中 encoded[i] = arr[i] XO…

    Java 2023年6月8日
    076
  • easyUI 添加CheckBox选择到DataGrid

    @author YHC 这个教程向你展示如何放置一个checkbox 列到datagrid,这个复选框用户将可以选择 选中/取消选中 datagrid行数据. 添加一个checkb…

    Java 2023年5月29日
    067
  • 【转】cron表达式详解

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: (1) Seconds Minutes Hours Day…

    Java 2023年6月6日
    0108
  • 丰富Bean的配置

    关于Bean是如何配置的,大家并不陌生。毕竟前文曾有涉及,虽然比较粗浅。却也介绍了如何写份XML配置文件,告诉Spring容器怎样创建Bean,怎样注入依赖,等等。其中要点如下:1…

    Java 2023年6月5日
    071
  • 1开篇:应用系统源代码平台移植的理论指导思想

    一:因工作需要一直在做不同平台间的代码移植,从最开始的对照旧系统代码手动翻译成新系统代码,到借助简单工具构建的编码框架,再到手动编写代码转换工具。手动编写代码转换工具经历了从截取文…

    Java 2023年6月8日
    0102
  • Ubuntu 20.04 开启局域网唤醒(WoL)

    打开主板相关设置 创建 systemd 自启动设置文件 vim /etc/systemd/system/wol@.service 放入以下内容: [Unit] Descriptio…

    Java 2023年6月7日
    087
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球