• Spring Security笔记


    Spring SecuritySpring家族中的一个安全管理框架。

    一般来说中大型的项目都是使用 SpringSecurity 来做安全框架,小项目用相对简单的Shiro认证授权是 SpringSecurity 作为安全框架的核心功能。

    • 认证:通过用户名密码验证当前访问系统的是不是本系统的用户,并且要确认具体是哪个用户。
    • 授权:经过认证后判断当前用户是否有权限进行某个操作。

    本笔记只做代码编写,具体概念参考:
    📖 Spring Security 详解
    📖 B站视频链接:SpringSecurity框架教程-Spring Security+JWT实现项目级前端分离认证授权

    1. 入门

    📌 认证流程图:
    在这里插入图片描述

    • Authentication接口:它的实现类,表示当前访问系统的用户,封装了用户相关信息。
    • AuthenticationManager接口:定义了认证 Authentication 的方法 authenticate()
    • UserDetailsService接口:加载用户特定数据的核心接口。里面定义了一个根据用户名查询用户信息的方法loadUserByUsername()
    • UserDetails接口:提供核心用户信息。通过 UserDetailsService 根据用户名获取处理的用户信息要封装成 UserDetails 对象返回。然后将这些信息封装到 Authentication 对象中。

    1.1 实现UserDetailsService

    UserDetailsService接口,在这个实现类中去查询数据库。

    📅 1. pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.ymqx</groupId>
      <artifactId>SpringSecurityDemo</artifactId>
      <version>1.0-SNAPSHOT</version>
      <name>SpringSecurityDemo</name>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
    
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
      </parent>
    
      <dependencies>
        <!--web-->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <!--lombok-->
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
        </dependency>
    
        <!--security-->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    
        <!--mybatis-plus-->
        <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-boot-starter</artifactId>
          <version>3.4.3</version>
        </dependency>
        
        <!--mysql-->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
        </dependency>
      </dependencies>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    引入依赖:spring-boot-starter-security

    📅 2. service:

    package org.ymqx.service.impl;
    
    import ...
    
    import java.util.Objects;
    
    @Service
    public class UserDetailsServiceImpl implements UserDetailsService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            // 1、根据用户名查询用户信息
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getUserName, username);
            User user = userMapper.selectOne(wrapper);
            // 1.1、如果查询不到数据就通过抛出异常来给出提示
            if(Objects.isNull(user)){
                throw new RuntimeException("用户名或密码错误");
            }
    
            // 2、根据用户查询权限信息 添加到LoginUser中
    
            // 3、封装成UserDetails对象返回
            UserDetails loginUser = new LoginUser(user);
    
            return loginUser;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    ✨ 实现UserDetailsService接口,重写loadUserByUsername()方法,内部实现查询数据库逻辑,将获取的用户信息封装成UserDetails对象返回。

    1.2 实现UserDetails

    📅 3. domain.LoginUser:

    package org.ymqx.domain;
    
    import ...
    
    import java.util.Collection;
    
    @Data
    @NoArgsConstructor
    public class LoginUser implements UserDetails {
        private User user;
    
        public LoginUser(User user) {
            this.user = user;
        }
    
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return null;
        }
    
        @Override
        public String getPassword() {
            // 返回用户密码
            return user.getPassword();
        }
    
        @Override
        public String getUsername() {
            // 返回用户名
            return user.getUserName();
        }
    
        @Override
        public boolean isAccountNonExpired() {
            // 返回true
            return true;
        }
    
        @Override
        public boolean isAccountNonLocked() {
            // 返回true
            return true;
        }
    
        @Override
        public boolean isCredentialsNonExpired() {
            // 返回true
            return true;
        }
    
        @Override
        public boolean isEnabled() {
            // 返回true
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    ✨ 实现UserDetails接口,通过 UserDetailsService 根据用户名获取处理的用户信息要封装成 UserDetails 对象返回。然后将这些信息封装到 Authentication 对象中。

    1.3 MybatisPuls查询数据库

    sys_user 用户表建表语句:

    CREATE TABLE `sys_user` (
      `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `user_name` VARCHAR(64) NOT NULL DEFAULT 'NULL' COMMENT '用户名',
      `nick_name` VARCHAR(64) NOT NULL DEFAULT 'NULL' COMMENT '昵称',
      `password` VARCHAR(64) NOT NULL DEFAULT 'NULL' COMMENT '密码',
      `status` CHAR(1) DEFAULT '0' COMMENT '账号状态(0正常 1停用)',
      `email` VARCHAR(64) DEFAULT NULL COMMENT '邮箱',
      `phonenumber` VARCHAR(32) DEFAULT NULL COMMENT '手机号',
      `sex` CHAR(1) DEFAULT NULL COMMENT '用户性别(0男,1女,2未知)',
      `avatar` VARCHAR(128) DEFAULT NULL COMMENT '头像',
      `user_type` CHAR(1) NOT NULL DEFAULT '1' COMMENT '用户类型(0管理员,1普通用户)',
      `create_by` BIGINT(20) DEFAULT NULL COMMENT '创建人的用户id',
      `create_time` DATETIME DEFAULT NULL COMMENT '创建时间',
      `update_by` BIGINT(20) DEFAULT NULL COMMENT '更新人',
      `update_time` DATETIME DEFAULT NULL COMMENT '更新时间',
      `del_flag` INT(11) DEFAULT '0' COMMENT '删除标志(0代表未删除,1代表已删除)',
      PRIMARY KEY (`id`)
    ) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='用户表'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    📅 4. application.yml 配置数据库信息:

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/sg_security?characterEncoding=utf-8&serverTimezone=UTC
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    📅 5. domain.User:

    package org.ymqx.domain;
    
    import ...
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @TableName(value = "sys_user")
    public class User implements Serializable {
        private static final long serialVersionUID = -40356785423868312L;
    
        /**
         * 主键
         */
        @TableId
        private Long id;
        /**
         * 用户名
         */
        private String userName;
        /**
         * 昵称
         */
        private String nickName;
        /**
         * 密码
         */
        private String password;
        /**
         * 账号状态(0正常 1停用)
         */
        private String status;
        /**
         * 邮箱
         */
        private String email;
        /**
         * 手机号
         */
        private String phonenumber;
        /**
         * 用户性别(0男,1女,2未知)
         */
        private String sex;
        /**
         * 头像
         */
        private String avatar;
        /**
         * 用户类型(0管理员,1普通用户)
         */
        private String userType;
        /**
         * 创建人的用户id
         */
        private Long createBy;
        /**
         * 创建时间
         */
        private Date createTime;
        /**
         * 更新人
         */
        private Long updateBy;
        /**
         * 更新时间
         */
        private Date updateTime;
        /**
         * 删除标志(0代表未删除,1代表已删除)
         */
        private Integer delFlag;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    📅 6. mapper:

    package org.ymqx.mapper;
    
    import ...
    
    @Mapper
    public interface UserMapper extends BaseMapper<User> {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    1.4 配置类

    📅 7. config:

    package org.ymqx.config;
    
    import ...
    
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        // 指定加密类
        @Bean
        public PasswordEncoder passwordEncoder(){
            return new BCryptPasswordEncoder();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    实际项目中我们不会把密码明文存储在数据库中。

    • 默认使用的PasswordEncoder要求数据库中的密码格式为:{id}password 。它会根据id去判断密码的加密方式。但是我们一般不会采用这种方式。所以就需要替换PasswordEncoder
    • 我们一般使用SpringSecurity为我们提供的BCryptPasswordEncoder
    • 我们只需要使用把BCryptPasswordEncoder对象注入Spring容器中,SpringSecurity就会使用该PasswordEncoder来进行密码校验。
    • 我们可以定义一个 SpringSecurity 的配置类,SpringSecurity 要求这个配置类要继承WebSecurityConfigurerAdapter
    public class Test5 {
        public static void main(String[] args) {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encode = passwordEncoder.encode("123456");
            System.out.println(encode);
    
            boolean matches = passwordEncoder.matches("123456", encode);
            System.out.println(matches);
        }
    }
    
    输出:
    $2a$10$V.th3Vaeh9lzPJmtyM3A7eqL9YRZ1Cv4By4KmJN2F6t5bng6P4UFG
    true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    字符串"123456"生成的密文插入到用户表sys_user

    --密码原文存储
    insert into sys_user(user_name,nick_name,password,status,email,phonenumber,sex,avatar,user_type,create_by,create_time,update_by,update_time,del_flag) 
    values('sg','三更','$2a$10$V.th3Vaeh9lzPJmtyM3A7eqL9YRZ1Cv4By4KmJN2F6t5bng6P4UFG','0',null,'123456789','男','','0','0','2022-06-01','0','2022-06-01','0')
    
    • 1
    • 2
    • 3

    1.5 测试

    📅 8. controller:

    package org.ymqx.controller;
    
    import ...
    
    @RestController
    public class HelloController {
    
        @RequestMapping("/hello")
        public String hello(){
            return "hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    📅 9. 启动类:

    package org.ymqx;
    
    import ...
    
    @SpringBootApplication
    @MapperScan("org.ymqx.mapper")
    public class SecurityApplication
    {
        public static void main( String[] args ) {
            SpringApplication.run(SecurityApplication.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    📌 访问 http://localhost:8086/hello

    请添加图片描述

    📌 输入账号:sg、密码:123456

    请添加图片描述

    验证成功,返回响应。

    2. 整合JWT、Redis

    入门案例可见,每次访问都需要使用SpringSecurity默认的login页面登录认证,显然不符合前后端分离的开发模式。

    所以我们需要自定义登录接口。通过入门的认证流程图可知,通过AuthenticationManagerauthenticate()方法来进行用户认证,同时,需要将登录接口进行放行,让用户访问这个接口的时候不用登录也能访问。

    认证成功后,生成一个JWT,放入响应中返回给请求方。并且,将用户信息存入redis,可以把用户 id 作为 key。

    请求方下次请求时携带登录返回的JWT,服务方通过JWT识别出具体的是哪个用户,通过用户 id 查询redis,获取用户信息,并将用户信息存入SecurityContextHolder,方便 SpringSecurity 后续过滤器使用。

    在这里插入图片描述

    • 登录
    1. 自定义登录接口,调用 ProviderManager 的方法进行认证,如果认证通过生成JWT,把用户信息存入 redis 中。
    2. 自定义 UserDetailsService,在这个实现类中去查询数据库。(已经实现)
    • 校验:
    1. 定义 Jwt 认证过滤器,获取token,解析 token 获取其中的 userid,通过 userid 从 redis 中获取用户信息,存入SecurityContextHolder

    2.1 新增依赖

    📅 1. pom.xml:

        <!--redis依赖-->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- 引入redis连接池的依赖 -->
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-pool2</artifactId>
        </dependency>
    
        <!--fastjson依赖-->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.33</version>
        </dependency>
    
        <!--jwt依赖-->
        <dependency>
          <groupId>io.jsonwebtoken</groupId>
          <artifactId>jjwt</artifactId>
          <version>0.9.0</version>
        </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2.2 新增工具类

    📅 2. utils.FastJsonRedisSerializer:

    配置 redis 序列化

    package org.ymqx.utils;
    
    
    import ...;
    
    /**
     * Redis使用FastJson序列化
     *
     */
    public class FastJsonRedisSerializer<T> implements RedisSerializer<T>
    {
    
        public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    
        private Class<T> clazz;
    
        static
        {
            ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
        }
    
        public FastJsonRedisSerializer(Class<T> clazz)
        {
            super();
            this.clazz = clazz;
        }
    
        @Override
        public byte[] serialize(T t) throws SerializationException
        {
            if (t == null)
            {
                return new byte[0];
            }
            return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
        }
    
        @Override
        public T deserialize(byte[] bytes) throws SerializationException
        {
            if (bytes == null || bytes.length <= 0)
            {
                return null;
            }
            String str = new String(bytes, DEFAULT_CHARSET);
    
            return JSON.parseObject(str, clazz);
        }
    
    
        protected JavaType getJavaType(Class<?> clazz)
        {
            return TypeFactory.defaultInstance().constructType(clazz);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    📅 3. config.RedisConfig:

    配置 RedisTemplate

    package org.ymqx.config;
    
    
    import ...
    
    @Configuration
    public class RedisConfig {
    
        @Bean
        @SuppressWarnings(value = { "unchecked", "rawtypes" })
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
        {
            RedisTemplate<Object, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(connectionFactory);
    
            FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
    
            // 使用StringRedisSerializer来序列化和反序列化redis的key值
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(serializer);
    
            // Hash的key也采用StringRedisSerializer的序列化方式
            template.setHashKeySerializer(new StringRedisSerializer());
            template.setHashValueSerializer(serializer);
    
            template.afterPropertiesSet();
            return template;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    📅 4. utils.RedisCache:

    配置 redis 工具类

    package org.ymqx.utils;
    
    import ...
    
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    @Component
    public class RedisCache
    {
        @Autowired
        public RedisTemplate redisTemplate;
    
        /**
         * 缓存基本的对象,Integer、String、实体类等
         *
         * @param key 缓存的键值
         * @param value 缓存的值
         */
        public <T> void setCacheObject(final String key, final T value)
        {
            redisTemplate.opsForValue().set(key, value);
        }
    
        /**
         * 缓存基本的对象,Integer、String、实体类等
         *
         * @param key 缓存的键值
         * @param value 缓存的值
         * @param timeout 时间
         * @param timeUnit 时间颗粒度
         */
        public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
        {
            redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
        }
    
        /**
         * 设置有效时间
         *
         * @param key Redis键
         * @param timeout 超时时间
         * @return true=设置成功;false=设置失败
         */
        public boolean expire(final String key, final long timeout)
        {
            return expire(key, timeout, TimeUnit.SECONDS);
        }
    
        /**
         * 设置有效时间
         *
         * @param key Redis键
         * @param timeout 超时时间
         * @param unit 时间单位
         * @return true=设置成功;false=设置失败
         */
        public boolean expire(final String key, final long timeout, final TimeUnit unit)
        {
            return redisTemplate.expire(key, timeout, unit);
        }
    
        /**
         * 获得缓存的基本对象。
         *
         * @param key 缓存键值
         * @return 缓存键值对应的数据
         */
        public <T> T getCacheObject(final String key)
        {
            ValueOperations<String, T> operation = redisTemplate.opsForValue();
            return operation.get(key);
        }
    
        /**
         * 删除单个对象
         *
         * @param key
         */
        public boolean deleteObject(final String key)
        {
            return redisTemplate.delete(key);
        }
    
        /**
         * 删除集合对象
         *
         * @param collection 多个对象
         * @return
         */
        public long deleteObject(final Collection collection)
        {
            return redisTemplate.delete(collection);
        }
    
        /**
         * 缓存List数据
         *
         * @param key 缓存的键值
         * @param dataList 待缓存的List数据
         * @return 缓存的对象
         */
        public <T> long setCacheList(final String key, final List<T> dataList)
        {
            Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
            return count == null ? 0 : count;
        }
    
        /**
         * 获得缓存的list对象
         *
         * @param key 缓存的键值
         * @return 缓存键值对应的数据
         */
        public <T> List<T> getCacheList(final String key)
        {
            return redisTemplate.opsForList().range(key, 0, -1);
        }
    
        /**
         * 缓存Set
         *
         * @param key 缓存键值
         * @param dataSet 缓存的数据
         * @return 缓存数据的对象
         */
        public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
        {
            BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
            Iterator<T> it = dataSet.iterator();
            while (it.hasNext())
            {
                setOperation.add(it.next());
            }
            return setOperation;
        }
    
        /**
         * 获得缓存的set
         *
         * @param key
         * @return
         */
        public <T> Set<T> getCacheSet(final String key)
        {
            return redisTemplate.opsForSet().members(key);
        }
    
        /**
         * 缓存Map
         *
         * @param key
         * @param dataMap
         */
        public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
        {
            if (dataMap != null) {
                redisTemplate.opsForHash().putAll(key, dataMap);
            }
        }
    
        /**
         * 获得缓存的Map
         *
         * @param key
         * @return
         */
        public <T> Map<String, T> getCacheMap(final String key)
        {
            return redisTemplate.opsForHash().entries(key);
        }
    
        /**
         * 往Hash中存入数据
         *
         * @param key Redis键
         * @param hKey Hash键
         * @param value 值
         */
        public <T> void setCacheMapValue(final String key, final String hKey, final T value)
        {
            redisTemplate.opsForHash().put(key, hKey, value);
        }
    
        /**
         * 获取Hash中的数据
         *
         * @param key Redis键
         * @param hKey Hash键
         * @return Hash中的对象
         */
        public <T> T getCacheMapValue(final String key, final String hKey)
        {
            HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
            return opsForHash.get(key, hKey);
        }
    
        /**
         * 删除Hash中的数据
         *
         * @param key
         * @param hkey
         */
        public void delCacheMapValue(final String key, final String hkey)
        {
            HashOperations hashOperations = redisTemplate.opsForHash();
            hashOperations.delete(key, hkey);
        }
    
        /**
         * 获取多个Hash中的数据
         *
         * @param key Redis键
         * @param hKeys Hash键集合
         * @return Hash对象集合
         */
        public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
        {
            return redisTemplate.opsForHash().multiGet(key, hKeys);
        }
    
        /**
         * 获得缓存的基本对象列表
         *
         * @param pattern 字符串前缀
         * @return 对象列表
         */
        public Collection<String> keys(final String pattern)
        {
            return redisTemplate.keys(pattern);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230

    📅 5. application.yml

    配置 redis 地址信息

    spring:
      redis:
        database: 0
        host: 127.0.0.1
        port: 6379
        timeout: 5000
        lettuce:
          pool:
            max-active: 32
            max-wait: -1
            max-idle: 16
            min-idle: 8
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    📅 6. utils.JwtUtil:

    配置 JWT 工具类

    package org.ymqx.utils;
    
    import ...
    
    /**
     * JWT工具类
     */
    public class JwtUtil {
    
        //有效期为
        public static final Long JWT_TTL = 60 * 60 * 1000L;// 60 * 60 *1000  一个小时
        //设置秘钥明文
        public static final String JWT_KEY = "zndl";
    
        public static String getUUID(){
            String token = UUID.randomUUID().toString().replaceAll("-", "");
            return token;
        }
    
        /**
         * 生成加密后的秘钥 secretKey
         * @return
         */
        public static SecretKey generalKey() {
            byte[] encodedKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
            SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
            return key;
        }
    
        /**
         * 生成jtw
         * @param subject token中要存放的数据(json格式)
         * @return
         */
        public static String createJWT(String subject) {
            JwtBuilder builder = getJwtBuilder(subject, null, getUUID());// 设置过期时间
            return builder.compact();
        }
    
        /**
         * 生成jtw
         * @param subject token中要存放的数据(json格式)
         * @param ttlMillis token超时时间
         * @return
         */
        public static String createJWT(String subject, Long ttlMillis) {
            JwtBuilder builder = getJwtBuilder(subject, ttlMillis, getUUID());  // 设置过期时间
            return builder.compact();
        }
    
        /**
         * 创建token
         * @param id 唯一的ID
         * @param subject token中要存放的数据(json格式)
         * @param ttlMillis token超时时间
         * @return
         */
        public static String createJWT(String id, String subject, Long ttlMillis) {
            JwtBuilder builder = getJwtBuilder(subject, ttlMillis, id);   // 设置过期时间
            return builder.compact();
        }
    
        private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {
            SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
            SecretKey secretKey = generalKey();
            long nowMillis = System.currentTimeMillis();
            Date now = new Date(nowMillis);
            if(ttlMillis==null){
                ttlMillis=JwtUtil.JWT_TTL;
            }
            long expMillis = nowMillis + ttlMillis;
            Date expDate = new Date(expMillis);
    
            /*System.out.println(uuid);
            System.out.println(subject);
            System.out.println(secretKey);
            System.out.println(expDate);*/
    
            return Jwts.builder()
                    .setId(uuid)             // 唯一的ID
                    .setSubject(subject)     // 主题  可以是JSON数据
                    .setIssuer("admin")      // 签发者
                    .setIssuedAt(now)        // 签发时间
                    .signWith(signatureAlgorithm, secretKey) //使用HS256对称加密算法签名, 第二个参数为秘钥
                    .setExpiration(expDate);
        }
    
        /**
         * 解析
         *
         * @param jwt
         * @return
         * @throws Exception
         */
        public static Claims parseJWT(String jwt) throws Exception {
            SecretKey secretKey = generalKey();
            return Jwts.parser()
                    .setSigningKey(secretKey)
                    .parseClaimsJws(jwt)
                    .getBody();
        }
    
        public static void main(String[] args) throws Exception {
            // 生成JWT
            /*String jwt = JwtUtil.createJWT("{user=shwen}");
            System.out.println(jwt);*/
    
            // 生成JWT,设置过期时间
            String jwt = JwtUtil.createJWT("{user=shwen}", 60*1000L);
            System.out.println(jwt);
    
            //过期JWT解析会抛出异常
            String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1ODljMGE0MDQ0Y2Y0YmJiYmFhYjFiOWFmOTMzYzc3ZiIsInN1YiI6Int1c2VyPXNod2VufSIsImlzcyI6ImFkbWluIiwiaWF0IjoxNjk5MjczMzE0LCJleHAiOjE2OTkyNzMzNzR9.xMOjtNp2AESTjuSq9G75DaZh9M1dDuLDr4s7yw-xw_Q";
            Claims claims = parseJWT(token);
            System.out.println(claims);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117

    📅 7. utils.WebUtils:

    package org.ymqx.utils;
    
    import ...
    
    public class WebUtils
    {
        /**
         * 将字符串渲染到客户端
         *
         * @param response 渲染对象
         * @param string 待渲染的字符串
         * @return null
         */
        public static String renderString(HttpServletResponse response, String string) {
            try
            {
                response.setStatus(200);
                response.setContentType("application/json");
                response.setCharacterEncoding("utf-8");
                response.getWriter().print(string);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    2.3 实现登陆接口

    1. 在 SecurityConfig 中配置把 AuthenticationManager 注入容器。
    2. 登陆接口中我们通过 AuthenticationManager 的authenticate()方法来进行用户认证。
    3. SpringSecurity对登录接口进行放行。

    📅 8. config.SecurityConfig:

    注入AuthenticationManager,对登录接口进行放行。

    package org.ymqx.config;
    
    import ...
    
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        // 指定加密类
        @Bean
        public PasswordEncoder passwordEncoder(){
            return new BCryptPasswordEncoder();
        }
        
        // 注入AuthenticationManager 到容器
        @Bean
        @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }
    
        // 对登录接口进行放行 antMatchers("/user/login").anonymous()
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    //关闭csrf
                    .csrf().disable()
                    //不通过Session获取SecurityContext
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    .authorizeRequests()
                    // 对于登录接口 允许匿名访问
                    .antMatchers("/user/login").anonymous()
                    // 除上面外的所有请求全部需要鉴权认证
                    .anyRequest().authenticated();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    📅 9. service:

    package org.ymqx.service;
    
    import org.ymqx.domain.ResponseResult;
    import org.ymqx.domain.User;
    
    public interface LoginServcie {
        public ResponseResult login(User user);
    
        public ResponseResult logout(User user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    📅 10. service.impl:

    登陆接口中我们通过 AuthenticationManager 的authenticate()方法来进行用户认证。

    package org.ymqx.service.impl;
    
    import ...
    
    @Service
    public class LoginServiceImpl implements LoginServcie {
        @Autowired
        private AuthenticationManager authenticationManager;
        @Autowired
        private RedisCache redisCache;
    
        @Override
        public ResponseResult login(User user) {
            // 1、自定义authenticationToken
            UsernamePasswordAuthenticationToken authenticationToken 
            		= new UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());
            		
            Authentication authenticate = authenticationManager.authenticate(authenticationToken);
            if(Objects.isNull(authenticate)){
                throw new RuntimeException("用户名或密码错误");
            }
    
            // 2、使用userid生成token
            LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
            String userId = loginUser.getUser().getId().toString();
            String jwt = JwtUtil.createJWT(userId);
    
            // 3、将 authenticate 存入 redis
            String key= "login:" + userId;
            System.out.println("存入redis的key=[" + key + "]");
            redisCache.setCacheObject(key, loginUser);
    
            // 4、把token响应给前端
            HashMap<String,String> map = new HashMap<>();
            map.put("token", jwt);
            return new ResponseResult(200,"登陆成功", map);
        }
    
        @Override
        public ResponseResult logout(User user) {
            // 1、通过全局 SecurityContextHolder 获取当前 Authentication
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    
            // 2、获取 LoginUser,并删除 redis 记录
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            Long userid = loginUser.getUser().getId();
            redisCache.deleteObject("login:" + userid);
    
            return new ResponseResult(200,"退出成功");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    IDEA查看接口实现类快捷键:Ctrl+Alt+B,可以查看接口 Authentication 的实现类。

    2.4 调用controller

    📅 11. domain.ResponseResult:

    配置响应类

    package org.ymqx.domain;
    
    import lombok.Data;
    
    @Data
    public class ResponseResult<T> {
        /**
         * 状态码
         */
        private Integer code;
        /**
         * 提示信息,如果有错误时,前端可以获取该字段进行提示
         */
        private String msg;
        /**
         * 查询到的结果数据,
         */
        private T data;
    
        public ResponseResult(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    
        public ResponseResult(Integer code, T data) {
            this.code = code;
            this.data = data;
        }
    
        public ResponseResult(Integer code, String msg, T data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    📅 12. controller.LoginController:

    配置请求地址 /user/login controller

    package org.ymqx.controller;
    
    import ...
    
    @RestController
    public class LoginController {
    
        @Autowired
        private LoginServcie loginServcie;
    
        @PostMapping("/user/login")
        public ResponseResult login(@RequestBody User user){
            return loginServcie.login(user);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.5 定义 Jwt 认证过滤器

    定义 Jwt 认证过滤器,获取token,解析 token 获取其中的 userid,通过 userid 从 redis 中获取用户信息,存入SecurityContextHolder

    📅 13. filter.JwtAuthenticationTokenFilter:

    package org.ymqx.filter;
    
    import ...
    
    @Component
    public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
    
        @Autowired
        private RedisCache redisCache;
    
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            // 1、获取token
            String token = request.getHeader("token");
            System.out.println("请求方token=[" + token + "]");
            if (!StringUtils.hasText(token)) {
                //放行
                filterChain.doFilter(request, response);
                return;
            }
    
            // 2、解析token
            String userid;
            try {
                Claims claims = JwtUtil.parseJWT(token);
                userid = claims.getSubject();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("token非法");
            }
    
            // 3、从redis中获取用户信息
            String redisKey = "login:" + userid;
            System.out.println("读取redis的key=[" + redisKey + "]");
            
            LoginUser loginUser = redisCache.getCacheObject(redisKey);
            if(Objects.isNull(loginUser)){
                throw new RuntimeException("用户未登录");
            }
    
            // 4、存入SecurityContextHolder
            //TODO 获取权限信息封装到Authentication中
            UsernamePasswordAuthenticationToken authenticationToken =
                    new UsernamePasswordAuthenticationToken(loginUser,null,null);
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
    
            // 5、放行
            filterChain.doFilter(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    📅 14. config.SecurityConfig:

    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    //关闭csrf
                    .csrf().disable()
                    //不通过Session获取SecurityContext
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    .authorizeRequests()
                    // 对于登录接口 允许匿名访问
                    .antMatchers("/user/login").anonymous()
                    // 除上面外的所有请求全部需要鉴权认证
                    .anyRequest().authenticated();
    
            //把token校验过滤器添加到过滤器链中
            http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        }
    
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); 将 token 校验过滤器添加到 SpringSecurity 过滤器链前面,保证Jwt认证过滤器提前执行。

    2.6 测试

    📌 请求地址 http://localhost:8086/user/login
    请添加图片描述

    获取JWT:eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIzN2E5MzY4MDNmNjI0MjhhOWVmZGU3Y2VhMzU2MzY0ZSIsInN1YiI6IjIiLCJpc3MiOiJhZG1pbiIsImlhdCI6MTY5OTQ5MzY0MSwiZXhwIjoxNjk5NDk3MjQxfQ.PEEq_2u7tEf4QaAPLavwU4PoDHckO86S95_8piq7oS8

    📌 请求地址 http://localhost:8086/hello ,请求头携带 JWT
    请添加图片描述

    访问成功

    3. 授权

    我们不能只依赖前端去判断用户的权限来选择显示哪些菜单哪些按钮。因为如果只是这样,如果有人知道了对应功能的接口地址就可以不通过前端,直接去发送请求来实现相关功能操作。

    所以我们还需要在后台进行用户权限的判断,判断当前用户是否有相应的权限,必须具有所需权限才能进行相应的操作。

    在SpringSecurity中,会使用默认的FilterSecurityInterceptor来进行权限校验。在FilterSecurityInterceptor中会从SecurityContextHolder获取其中的Authentication,然后获取其中的 权限信息,判断当前用户是否拥有访问当前资源所需的权限。

    • 请求地址设置访问资源所需要的权限。
    • 登录接口把当前登录用户的权限信息也存入Authentication。
    • Jwt过滤器获取权限信息后,存入SecurityContextHolder

    3.1 限制访问资源所需权限

    SpringSecurity为我们提供了基于注解的权限控制方案,这也是我们项目中主要采用的方式。我们可以使用注解去指定访问对应的资源所需的权限。

    但是要使用它我们需要先开启相关配置springSecurity里面加。

    @EnableGlobalMethodSecurity(prePostEnabled = true)
    
    • 1

    📅 1. config.SecurityConfig:

    @Configuration
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    📅 2. HelloController:

    访问资源使用注解@PreAuthorize

    @RestController
    public class HelloController {
    
        @RequestMapping("/hello")
        @PreAuthorize("hasAuthority('test')")
        public String hello(){
            return "hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.2 封装权限信息

    RBAC权限模型(Role-Based Access Control)即:基于角色的权限控制。这是目前最常被开发者使用也是相对易用、通用权限模型。

    用户表 sys_user

    CREATE TABLE `sys_user` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `user_name` varchar(64) NOT NULL DEFAULT 'NULL' COMMENT '用户名',
      `nick_name` varchar(64) NOT NULL DEFAULT 'NULL' COMMENT '昵称',
      `password` varchar(64) NOT NULL DEFAULT 'NULL' COMMENT '密码',
      `status` char(1) DEFAULT '0' COMMENT '账号状态(0正常 1停用)',
      `email` varchar(64) DEFAULT NULL COMMENT '邮箱',
      `phonenumber` varchar(32) DEFAULT NULL COMMENT '手机号',
      `sex` char(1) DEFAULT NULL COMMENT '用户性别(0男,1女,2未知)',
      `avatar` varchar(128) DEFAULT NULL COMMENT '头像',
      `user_type` char(1) NOT NULL DEFAULT '1' COMMENT '用户类型(0管理员,1普通用户)',
      `create_by` bigint(20) DEFAULT NULL COMMENT '创建人的用户id',
      `create_time` datetime DEFAULT NULL COMMENT '创建时间',
      `update_by` bigint(20) DEFAULT NULL COMMENT '更新人',
      `update_time` datetime DEFAULT NULL COMMENT '更新时间',
      `del_flag` int(11) DEFAULT '0' COMMENT '删除标志(0代表未删除,1代表已删除)',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    角色表 sys_role

    CREATE TABLE `sys_role` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `name` varchar(128) DEFAULT NULL,
      `role_key` varchar(100) DEFAULT NULL COMMENT '角色权限字符串',
      `status` char(1) DEFAULT '0' COMMENT '角色状态(0正常 1停用)',
      `del_flag` int(1) DEFAULT '0' COMMENT 'del_flag',
      `create_by` bigint(200) DEFAULT NULL,
      `create_time` datetime DEFAULT NULL,
      `update_by` bigint(200) DEFAULT NULL,
      `update_time` datetime DEFAULT NULL,
      `remark` varchar(500) DEFAULT NULL COMMENT '备注',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='角色表';
    
    insert into sys_role(id,name,role_key) values(1,'ADMIN','admin');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    用户_角色关联表 sys_user_role

    CREATE TABLE `sys_user_role` (
      `user_id` bigint(200) NOT NULL AUTO_INCREMENT COMMENT '用户id',
      `role_id` bigint(200) NOT NULL DEFAULT '0' COMMENT '角色id',
      PRIMARY KEY (`user_id`,`role_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    
    insert into sys_user_role(user_id, role_id) values(2, 1);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    菜单表 sys_menu

    CREATE TABLE `sys_menu` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `menu_name` varchar(64) NOT NULL DEFAULT 'NULL' COMMENT '菜单名',
      `path` varchar(200) DEFAULT NULL COMMENT '路由地址',
      `component` varchar(255) DEFAULT NULL COMMENT '组件路径',
      `visible` char(1) DEFAULT '0' COMMENT '菜单状态(0显示 1隐藏)',
      `status` char(1) DEFAULT '0' COMMENT '菜单状态(0正常 1停用)',
      `perms` varchar(100) DEFAULT NULL COMMENT '权限标识',
      `icon` varchar(100) DEFAULT '#' COMMENT '菜单图标',
      `create_by` bigint(20) DEFAULT NULL,
      `create_time` datetime DEFAULT NULL,
      `update_by` bigint(20) DEFAULT NULL,
      `update_time` datetime DEFAULT NULL,
      `del_flag` int(11) DEFAULT '0' COMMENT '是否删除(0未删除 1已删除)',
      `remark` varchar(500) DEFAULT NULL COMMENT '备注',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='菜单表';
    
    insert into sys_menu(id,menu_name,path,component,perms) values(1,'部门管理','dept','system/dept/index','system:dept:list');
    insert into sys_menu(id,menu_name,path,component,perms) values(2,'测试','test','system/test/index','system:test:list');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    角色_菜单关联表 sys_role_menu

    CREATE TABLE `sys_role_menu` (
      `role_id` bigint(200) NOT NULL AUTO_INCREMENT COMMENT '角色ID',
      `menu_id` bigint(200) NOT NULL DEFAULT '0' COMMENT '菜单id',
      PRIMARY KEY (`role_id`,`menu_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
    
    insert into sys_role_menu(role_id,menu_id) values(1,1);
    insert into sys_role_menu(role_id,menu_id) values(1,2);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    通过userid查询用户权限

    SELECT 
    	DISTINCT m.`perms`
    FROM
    	sys_user_role ur
    	LEFT JOIN `sys_role` r ON ur.`role_id` = r.`id`
    	LEFT JOIN `sys_role_menu` rm ON r.`id` = rm.`role_id`
    	LEFT JOIN `sys_menu` m ON rm.`menu_id` = m.`id`
    WHERE
    	user_id = 2
    	AND r.`status` = 0
    	AND m.`status` = 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    📅 3. domain.Menu :

    菜单表(Menu)实体类

    package org.ymqx.domain;
    
    import ...
    
    @TableName(value="sys_menu")
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class Menu implements Serializable {
        private static final long serialVersionUID = -54979041104113736L;
        
        @TableId
        private Long id;
        /**
        * 菜单名
        */
        private String menuName;
        /**
        * 路由地址
        */
        private String path;
        /**
        * 组件路径
        */
        private String component;
        /**
        * 菜单状态(0显示 1隐藏)
        */
        private String visible;
        /**
        * 菜单状态(0正常 1停用)
        */
        private String status;
        /**
        * 权限标识
        */
        private String perms;
        /**
        * 菜单图标
        */
        private String icon;
        
        private Long createBy;
        
        private Date createTime;
        
        private Long updateBy;
        
        private Date updateTime;
        /**
        * 是否删除(0未删除 1已删除)
        */
        private Integer delFlag;
        /**
        * 备注
        */
        private String remark;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    📅 4. mapper.MenuMapper:

    定义个mapper,其中提供一个方法可以根据userid查询权限信息。

    package org.ymqx.mapper;
    
    import ...
    
    @Mapper
    public interface MenuMapper extends BaseMapper<Menu> {
        List<String> selectPermsByUserId(Long id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    📅 5. mapper.MenuMapper.xml:

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="org.ymqx.mapper.MenuMapper">
        <select id="selectPermsByUserId" resultType="java.lang.String">
            SELECT
                DISTINCT m.`perms`
            FROM
                sys_user_role ur
                    LEFT JOIN `sys_role` r ON ur.`role_id` = r.`id`
                    LEFT JOIN `sys_role_menu` rm ON ur.`role_id` = rm.`role_id`
                    LEFT JOIN `sys_menu` m ON m.`id` = rm.`menu_id`
            WHERE
                user_id = #{userid}
              AND r.`status` = 0
              AND m.`status` = 0
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    📅 6. application.yml:

    配置xml路径

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/sg_security?characterEncoding=utf-8&serverTimezone=UTC
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
      redis:
        host: localhost
        port: 6379
    mybatis-plus:
      mapper-locations: classpath*:/mapper/**/*.xml 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.3 获取权限信息

    UserDetailsServiceImpl中去调用该 mapper 的方法查询权限信息封装到LoginUser对象中即可。

    📅 7. impl.UserDetailsServiceImpl:

    package org.ymqx.service.impl;
    
    import ...
    
    @Service
    public class UserDetailsServiceImpl implements UserDetailsService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Autowired
        private MenuMapper menuMapper;
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            // 1、根据用户名查询用户信息
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getUserName, username);
            User user = userMapper.selectOne(wrapper);
            // 1.1、如果查询不到数据就通过抛出异常来给出提示
            if(Objects.isNull(user)){
                throw new RuntimeException("用户名或密码错误");
            }
    
            // 2、根据用户查询权限信息 添加到LoginUser中
            List<String> permissionKeyList =  menuMapper.selectPermsByUserId(user.getId());
    
            // 3、封装成UserDetails对象返回
            UserDetails loginUser = new LoginUser(user, permissionKeyList);
    
            return loginUser;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    📅 8. domain.LoginUser:

    package org.ymqx.domain;
    
    import ...
    
    @Data
    @NoArgsConstructor
    public class LoginUser implements UserDetails {
        private User user;
    
        // 存储权限信息
        private List<String> permissions;
    
        // 存储SpringSecurity所需要的权限信息的集合
        @JSONField(serialize = false)
        private List<GrantedAuthority> authorities;
    
        public LoginUser(User user,List<String> permissions) {
            this.user = user;
            this.permissions = permissions;
        }
    
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            if(authorities!=null){
                return authorities;
            }
            // 把permissions中字符串类型的权限信息转换成GrantedAuthority对象存入authorities中
            authorities = permissions.stream().
                    map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());
            return authorities;
        }
    
        @Override
        public String getPassword() {
            // 返回用户密码
            return user.getPassword();
        }
    
        @Override
        public String getUsername() {
            // 返回用户名
            return user.getUserName();
        }
    
        @Override
        public boolean isAccountNonExpired() {
            // 返回true
            return true;
        }
    
        @Override
        public boolean isAccountNonLocked() {
            // 返回true
            return true;
        }
    
        @Override
        public boolean isCredentialsNonExpired() {
            // 返回true
            return true;
        }
    
        @Override
        public boolean isEnabled() {
            // 返回true
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    3.4 Jwt过滤器

    📅 9. filter.JwtAuthenticationTokenFilter:

    获取token,解析 token 获取其中的 userid,通过 userid 从 redis 中获取用户权限信息,存入SecurityContextHolder

    package org.ymqx.filter;
    
    import ...
    
    @Component
    public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
    
        @Autowired
        private RedisCache redisCache;
    
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            ...
    
            // 4、存入SecurityContextHolder
            // 获取权限信息封装到Authentication中
            UsernamePasswordAuthenticationToken authenticationToken =
                    new UsernamePasswordAuthenticationToken(loginUser,null, loginUser.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
    
            // 5、放行
            filterChain.doFilter(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    4. 自定义失败处理

    认证失败或者授权失败的情况下,我们希望接口返回相同格式的JSON数据,这样前端可以对响应统一处理,这里需要SpringSecurity的异常处理机制。

    在SpringSecurity中,如果我们在认证或者授权的过程中出现了异常会被ExceptionTranslationFilter捕获到。在ExceptionTranslationFilter中会去判断是认证失败还是授权失败出现的异常。

    • 认证异常:会被封装成AuthenticationException然后调用AuthenticationEntryPoint对象的方法去进行异常处理。
    • 授权异常:会被封装成AccessDeniedException然后调用AccessDeniedHandler对象的方法去进行异常处理。

    所以如果我们需要自定义异常处理,我们只需要自定义 AuthenticationEntryPointAccessDeniedHandler 然后配置给 SpringSecurity 即可。

    📅 1. handler.AuthenticationEntryPointImpl:

    package org.ymqx.handler;
    
    import ...
    
    /**
     * 认证失败处理类
     */
    @Component
    public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
            ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(), "认证失败请重新登录");
            String json = JSON.toJSONString(result);
            WebUtils.renderString(response,json);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    📅 2. handler.AccessDeniedHandlerImpl:

    package org.ymqx.handler;
    
    import ...
    
    /**
     * 权限失败处理类
     */
    @Component
    public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
            ResponseResult result = new ResponseResult(HttpStatus.FORBIDDEN.value(), "权限不足");
            String json = JSON.toJSONString(result);
            WebUtils.renderString(response,json);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    📅 3. config.SecurityConfig:

    package org.ymqx.config;
    
    import ...
    
    @Configuration
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        ...
    
        @Autowired
        private AuthenticationEntryPoint authenticationEntryPoint;
        @Autowired
        private AccessDeniedHandler accessDeniedHandler;
    
        ...
     
        // 对登录接口进行放行 antMatchers("/user/login").anonymous()
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    // 关闭csrf
                    .csrf().disable()
                    // 不通过Session获取SecurityContext
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    .authorizeRequests()
                    // 对于登录接口 允许匿名访问
                    .antMatchers("/user/login").anonymous()
                    // 除上面外的所有请求全部需要鉴权认证
                    .anyRequest().authenticated();
    
            // 把token校验过滤器添加到过滤器链中
            http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    
            // 配置异常处理器
            http.exceptionHandling()
                    // 配置认证失败处理类
                    .authenticationEntryPoint(authenticationEntryPoint).
                    // 配置权限认证处理类
                    accessDeniedHandler(accessDeniedHandler);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    先注入异常处理器,http.exceptionHandling()配置异常处理类。

    📌 访问 http://localhost:8086/user/login,密码错误

    在这里插入图片描述

    📌 权限修改 @PreAuthorize("hasAuthority('test111')"), 访问 http://localhost:8086/hello

    在这里插入图片描述

    5. 跨域

    浏览器出于安全的考虑,使用 XMLHttpRequest对象发起 HTTP请求时必须遵守同源策略,否则就是跨域的HTTP请求,默认情况下是被禁止的。 同源策略要求源相同才能正常进行通信,即协议、域名、端口号都完全一致

    前后端分离项目,前端项目和后端项目一般都不是同源的,所以肯定会存在跨域请求的问题。

    所以我们就要处理一下,让前端能进行跨域请求。

    📅 1. 先对SpringBoot配置,运行跨域请求

    @Configuration
    public class CorsConfig implements WebMvcConfigurer {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
          // 设置允许跨域的路径
            registry.addMapping("/**")
                    // 设置允许跨域请求的域名
                    .allowedOriginPatterns("*")
                    // 是否允许cookie
                    .allowCredentials(true)
                    // 设置允许的请求方式
                    .allowedMethods("GET", "POST", "DELETE", "PUT")
                    // 设置允许的header属性
                    .allowedHeaders("*")
                    // 跨域允许时间
                    .maxAge(3600);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    📅 2. 开启SpringSecurity的跨域访问

    由于我们的资源都会收到SpringSecurity的保护,所以想要跨域访问还要让SpringSecurity运行跨域访问。

    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        ...
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    // 关闭csrf
                    .csrf().disable()
                    // 不通过Session获取SecurityContext
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    .authorizeRequests()
                    // 对于登录接口 允许匿名访问
                    .antMatchers("/user/login").anonymous()
                    // 除上面外的所有请求全部需要鉴权认证
                    .anyRequest().authenticated();
    
            // 把token校验过滤器添加到过滤器链中
            http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    
            // 配置异常处理器
            http.exceptionHandling()
                    // 配置认证失败处理类
                    .authenticationEntryPoint(authenticationEntryPoint).
                    // 配置权限认证处理类
                    accessDeniedHandler(accessDeniedHandler);
    
            //允许跨域
            http.cors();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    6. 其他

    6.1 其它权限校验方法

    前面都是使用@PreAuthorize注解,然后在在其中使用的是hasAuthority方法进行校验。SpringSecurity还为我们提供了其它方法例如:hasAnyAuthorityhasRolehasAnyRole等。

    • hasAnyAuthority:可以传入多个权限,只有用户有其中任意一个权限都可以访问对应资源。
    @PreAuthorize("hasAnyAuthority('admin','test','system:dept:list')")
    public String hello(){
        return "hello";
    }
    
    • 1
    • 2
    • 3
    • 4
    • hasRole:要求有对应的角色才可以访问,但是它内部会把我们传入的参数拼接上 ROLE_ 后再去比较。所以这种情况下要用用户对应的权限也要有 ROLE_ 这个前缀才可以。
    @PreAuthorize("hasRole('system:dept:list')")
    public String hello(){
        return "hello";
    }
    
    • 1
    • 2
    • 3
    • 4
    • hasAnyRole:有任意的角色就可以访问。它内部也会把我们传入的参数拼接上 ROLE_ 后再去比较。所以这种情况下要用用户对应的权限也要有 ROLE_ 这个前缀才可以。
    @PreAuthorize("hasAnyRole('admin','system:dept:list')")
    public String hello(){
        return "hello";
    }
    
    • 1
    • 2
    • 3
    • 4

    6.2 自定义权限校验方法

    我们也可以定义自己的权限校验方法,在@PreAuthorize注解中使用我们的方法。

    📅 1. expression.PermissionService

    package org.ymqx.expression;
    
    import ...
    
    /**
     *  自定义权限实现,ss取自SpringSecurity首字母
     */
    @Component("ss")
    public class PermissionService {
        public boolean hasPermi(String authority){
            //获取当前用户的权限
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            List<String> permissions = loginUser.getPermissions();
            //判断用户权限集合中是否存在authority
            return permissions.contains(authority);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    📅 2. controller.HelloController

    @RestController
    public class HelloController {
    
        @RequestMapping("/hello")
        @PreAuthorize("@ss.hasPermi('test')")
        public String hello(){
            return "hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    SPEL表达式中使用 @ss 相当于获取容器中bean的名字为 ss 的对象。然后再调用这个对象的hasPermi()方法。

    6.3 CSRF

    CSRF是指跨站请求伪造(Cross-site request forgery),是web常见的攻击之一。​

    SpringSecurity 防止 CSRF 攻击的方式就是通过 csrf_token。后端会生成一个 csrf_token,前端发起请求的时候需要携带这个 csrf_token,后端会有过滤器进行校验,如果没有携带或者是伪造的就不允许访问。

    ​我们可以发现CSRF攻击依靠的是cookie中所携带的认证信息。而我们前后端分离的项目进行认证时,使用的是JSON Web Token(JWT),其实和 SpringSecurity 原理一样,所以 CSRF 攻击也就不用担心了。

    http
         // 关闭csrf
         .csrf().disable()
         // 不通过Session获取SecurityContext
         .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
         .and()
         .authorizeRequests()
         // 对于登录接口 允许匿名访问
         .antMatchers("/user/login").anonymous()
         // 除上面外的所有请求全部需要鉴权认证
         .anyRequest().authenticated();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    所以,SecurityConfig中可以配置csrf().disable()关闭 SpringSecurity 自带的 csrf,然后sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)设置不通过Session获取SecurityContext。

    7. 认证处理器

    7.1 认证成功处理器

    实际上在UsernamePasswordAuthenticationFilter进行登录认证的时候,如果登录成功了是会调用AuthenticationSuccessHandler的方法进行认证成功后的处理的。AuthenticationSuccessHandler就是登录成功处理器。

    我们也可以自己去自定义成功处理器进行成功后的相应处理。

    @Component
    public class SGSuccessHandler implements AuthenticationSuccessHandler {
    
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
            System.out.println("认证成功了");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private AuthenticationSuccessHandler successHandler;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.formLogin().successHandler(successHandler);
    
            http.authorizeRequests().anyRequest().authenticated();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    7.2 认证失败处理器

    实际上在UsernamePasswordAuthenticationFilter进行登录认证的时候,如果认证失败了是会调用AuthenticationFailureHandler的方法进行认证失败后的处理的。AuthenticationFailureHandler就是登录失败处理器。

    ​ 我们也可以自己去自定义失败处理器进行失败后的相应处理。

    @Component
    public class SGFailureHandler implements AuthenticationFailureHandler {
        @Override
        public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
            System.out.println("认证失败了");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private AuthenticationSuccessHandler successHandler;
    
        @Autowired
        private AuthenticationFailureHandler failureHandler;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.formLogin()
                    // 配置认证成功处理器
                    .successHandler(successHandler)
                    // 配置认证失败处理器
                    .failureHandler(failureHandler);
    
            http.authorizeRequests().anyRequest().authenticated();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    7.3 登出成功处理器

    @Component
    public class SGLogoutSuccessHandler implements LogoutSuccessHandler {
        @Override
        public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
            System.out.println("注销成功");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private AuthenticationSuccessHandler successHandler;
    
        @Autowired
        private AuthenticationFailureHandler failureHandler;
    
        @Autowired
        private LogoutSuccessHandler logoutSuccessHandler;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.formLogin()
                    // 配置认证成功处理器
                    .successHandler(successHandler)
                    // 配置认证失败处理器
                    .failureHandler(failureHandler);
    
            http.logout()
                    // 配置注销成功处理器
                    .logoutSuccessHandler(logoutSuccessHandler);
    
            http.authorizeRequests().anyRequest().authenticated();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    8. SecurityUtils工具类

    springsecurity会将认证的用户信息保存在全局的SecurityContextHolder中,其他服务可以通过SecurityContextHolder获取当前用户信息。

    /**
     * 安全服务工具类
     */
    public class SecurityUtils
    {
        /**
         * 获取用户账户
         **/
        public static String getUsername()
        {
            try
            {
                return getLoginUser().getUsername();
            }
            catch (Exception e)
            {
                throw new CustomException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
            }
        }
    
        /**
         * 获取用户
         **/
        public static LoginUser getLoginUser()
        {
            try
            {
                return (LoginUser) getAuthentication().getPrincipal();
            }
            catch (Exception e)
            {
                throw new CustomException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
            }
        }
    
        /**
         * 获取Authentication
         */
        public static Authentication getAuthentication()
        {
            return SecurityContextHolder.getContext().getAuthentication();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    静态通讯录
    wayland(xdg_wm_base) + egl + opengles 使用 Assimp 加载3D model 最简实例(十三)
    代码随想录算法训练营第29天 |第七章 回溯算法part05
    零基础学Linux内核之设备驱动篇(9)_平台设备驱动
    LeetCode-538. Convert BST to Greater Tree [C++][Java]
    loop_list单向循环列表
    R语言ggplot2可视化:使用ggpubr包的ggpie函数可视化饼图(pie chart)、为饼图不同区域添加标签、color参数指定饼图中线条的颜色
    机器学习11—原型聚类之学习向量量化(LVQ)
    难道企业真的需要私有区块链吗?
    【MySQL】数据库进阶之触发器内容详解
  • 原文地址:https://blog.csdn.net/weixin_40017062/article/details/134254431