首页 > 其他分享 >SpringSecurity 新版2.7以上 快速入门

SpringSecurity 新版2.7以上 快速入门

时间:2022-08-19 14:44:49浏览次数:60  
标签:http 入门 vip1 antMatchers SpringSecurity security 2.7 public vip2

SpringSecurity

快速入门

1、导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

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

 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.5.2</version>
</dependency>

编写配置文件

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<com.mhy.security.pojo.User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        com.mhy.security.pojo.User user = userMapper.selectOne(wrapper);
        
        if (user == null){
            throw new UsernameNotFoundException("该用户不存在!");
        }
        
        List<GrantedAuthority> vip2 = AuthorityUtils.commaSeparatedStringToAuthorityList("vip2"); //权限 一般是从数据库来实现
        
        return new User(username,PasswordEncoderUtils.encode(user.getPassword()),vip2);
}

三种授权的配置

三种授权的配置

首页

第一种 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 <T> AuthorityAuthorizationManager<T> 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<com.mhy.security.pojo.User> wrapper = new QueryWrapper<>();
wrapper.eq("username",username);
com.mhy.security.pojo.User user = userMapper.selectOne(wrapper);
if (user == null){
throw new UsernameNotFoundException("该用户不存在!");
}

List<GrantedAuthority> 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();
    }
}

标签:http,入门,vip1,antMatchers,SpringSecurity,security,2.7,public,vip2
From: https://www.cnblogs.com/shuisanya/p/16601911.html

相关文章

  • Jmeter(四十九) - 从入门到精通高级篇 - jmeter使用监视器结果监控tomcat性能(详解教程
    ------------------------------------------------------------------- 转载自:北京-宏哥https://www.cnblogs.com/du-hong/p/13667219.html -------------------------......
  • Docker快速入门--刘清政
    转载:https://www.cnblogs.com/liuqingzheng/p/16315254.html1Docker简介1.1什么是虚拟化​在计算机中,虚拟化(英语:Virtualization)是一种资源管理技术,是将计算机的各......
  • JQuery概念和JQuery快速入门
    JQuery概念jQuery是一个快速、简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(框架)于2006年1月由JohnResig发布。jQuery设计的宗旨是“writeL......
  • JQuery概念以及快速入门
    JQuery概念概念:一个JavaScript框架,简化js开发jQuery是一个快速、简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(框架)。jQuery设计的宗......
  • 小白入门-VS-C#如何新建一个项目
    启动VisualStudio。(中英版)在菜单栏上,选择File->New->Project。从模板中选择VisualC#,然后选择Windows。选择ConsoleApplication。为您的项目制定一个名称,......
  • Pytest系列(1-1)-快速入门
    前言目前有两种纯测试的测试框架,pytest和unittestunittest应该是广为人知,而且也是老框架了,很多人都用来做自动化,无论是UI还是接口pytest是基于unittest开发的另一款更......
  • Python入门系列(二)语法风格
    python缩进Python使用缩进来表示代码块,例如if5>2:print("Fiveisgreaterthantwo!")如果跳过缩进,Python将给您一个错误。#下边的写法将会报错if5>2:pri......
  • 内容审核入门逻辑
    在不同公司,对审核类职位的划分标准不同,比如有内容安全审核、内容推荐审核、内容质量审核等类似职位。虽然在侧重点和具体操作上有所不同,但有许多逻辑都是共通的。为了避免......
  • JavaWeb 开发入门
    原文:JavaWeb学习总结(一)——JavaWeb开发入门一、基本概念1.1、Web开发的相关知识Web,在英语中Web即表示网页的意思,它用于表示Internet主机上供外界访问的资源......
  • 7、Python语法入门之流程控制
    7、Python语法入门之流程控制  目录:引子分支结构什么是分支结构为什么要用分支结构如何使用分支结构if语法if应用案例循环结构什么是循......