前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Shiro整合JWT实现登录认证,原来这么简单!

Shiro整合JWT实现登录认证,原来这么简单!

作者头像
知否技术
发布2022-12-22 19:34:56
1.2K0
发布2022-12-22 19:34:56
举报
文章被收录于专栏:eclipse编程eclipse编程

题外话:今天是在家办公的第二周,疫情到底啥时候结束?好烦啊。

1.所用技术

  • SpringBoot
  • Mybatis-plus
  • Shiro
  • JWT
  • Redis

注:最后会给出完整代码

2.前置知识

Shiro:

Shiro 是一个基于 Java 的开源的安全框架。

在 Shiro 的核心架构里面,Subject 是访问系统的用户。SecurityManager 是安全管理器,负责用户的认证和授权,相当于 Shiro 的老大哥。

Realm 相当于数据源,用户的认证和授权都在 Realm 的方法中进行。

cryptography 用来管理用户的密码,对密码进行加密解密操作。

JWT:

JWT 全称 json web token,其实就是将用户的登录信息、过期时间以及加密算法经过"揉搓"之后生成的一串字符串,这个字符串又叫做令牌,当然你也可以叫做 token。

用户要想访问系统,请求头中必须携带使用 JWT 生成的 token。token 校验通过了,才能访问系统,否则抛出异常。

3.流程讲解

1.用户点击注册,系统将密码加密后存入数据库中。

2.用户登录

主要是校验账号密码并生成 token。

3.访问资源

其实在 Shiro 整合 JWT 的系统中,关键就是通过 JwtFilter 过滤器去校验请求头中是否包含 token,如果有 token,就交给自定义的 Realm。

然后在 Realm 的认证方法里面校验 token 是否正确、是否过期等。

4.初始化 SpringBoot 项目

1.新建数据库表

代码语言:javascript
复制
CREATE TABLE `t_user` (
  `id` bigint NOT NULL COMMENT 'id',
  `name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '姓名',
  `age` int DEFAULT NULL COMMENT '年龄',
  `sex` tinyint DEFAULT '0' COMMENT '性别:0-女 1-男',
  `username` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '账号',
  `password` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '密码',
  `created_date` datetime DEFAULT NULL COMMENT '创建时间',
  `updated_date` datetime DEFAULT NULL COMMENT '修改时间',
  `is_deleted` int DEFAULT '0' COMMENT '删除标识',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC;

2.新建 SpringBoot 项目

添加依赖:

代码语言:javascript
复制
<dependencies>
    <!--web-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--引入shiro整合Springboot依赖-->
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-spring-boot-starter</artifactId>
        <version>1.5.3</version>
    </dependency>
    <!--引入jwt-->
    <dependency>
        <groupId>com.auth0</groupId>
        <artifactId>java-jwt</artifactId>
        <version>3.10.3</version>
    </dependency>
    <!--redis-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!--myql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!--lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <!--mybatis plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.0</version>
    </dependency>
    <!--逆向工程-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.5.1</version>
    </dependency>
    <!--freemarker-->
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
    </dependency>
    <!--druid-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.6</version>
    </dependency>
    <!--hutool-->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.5.7</version>
    </dependency>
    <!--test-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

修改配置文件:

主要是设置数据源、mybatis-plus、redis 以及 jwt 秘钥。

代码语言:javascript
复制
server:
  port: 8081
  servlet:
    context-path: /shiro_jwt
spring:
  # 数据源
  datasource:
    url: jdbc:mysql://localhost:3306/shiro_jwt?allowPublicKeyRetrieval=true&useSSL=false
    username: root
    password: 12345678
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
  # Redis
  redis:
    host: 172.16.255.3
    port: 6379
    database: 0
    password: 123456
# MybatisPlus
mybatis-plus:
  global-config:
    db-config:
      field-strategy: IGNORED
      column-underline: true
      logic-delete-field: isDeleted # 全局逻辑删除的实体字段名
      logic-delete-value: 1 # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
      db-type: mysql
      id-type: assign_id
  mapper-locations: classpath*:/mapper/**Mapper.xml
  type-aliases-package: com.zhifou.entity
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#jwt
jwt:
  secret: zhifou_secret!

代码生成:

使用 Mybatis-plus 代码生成器生成entity、controller、service、dao、mapper 文件

配置 Redis(不是重点):

加解密工具类:

这里主要使用了 hutool 的加解密工具方法。

全局异常处理:

统一返回结果:

5.配置 JWT

1.JWT 工具类

主要用来生成 token、校验 token

2.JWTFilter

在 shiro 中,shiroFilter 用来拦截所有请求。

但是 shiro 要和 jwt 整合,所以要使用自定义的过滤器 JwtFilter。

JwtFilter 的主要作用就是拦截请求,判断请求头中书否携带 token。如果携带,就交给 Realm 处理。

代码语言:javascript
复制
@Component
@Slf4j
public class JwtFilter extends BasicHttpAuthenticationFilter {
    private String errorMsg;

    // 过滤器拦截请求的入口方法
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        // 判断请求头是否带上“Token”
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String token = httpServletRequest.getHeader("Authorization");
        // 游客访问电商平台首页可以不用携带 token
        if (StringUtils.isEmpty(token)) {
            return true;
        }
        try {
            // 交给 myRealm
            SecurityUtils.getSubject().login(new JwtToken(token));
            return true;
        } catch (Exception e) {
            errorMsg = e.getMessage();
            e.printStackTrace();
            return false;
        }
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setStatus(400);
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter out = httpServletResponse.getWriter();
        out.println(JSONUtil.toJsonStr(Result.fail(errorMsg)));
        out.flush();
        out.close();
        return false;
    }

    /**
     * 对跨域访问提供支持
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
        // 跨域发送一个option请求
        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
            httpServletResponse.setStatus(HttpStatus.OK.value());
            return false;
        }
        return super.preHandle(request, response);
    }

}

3.JwtToken

shiro 在没有和 jwt 整合之前,用户的账号密码被封装成了 UsernamePasswordToken 对象,UsernamePasswordToken 其实是 AuthenticationToken 的实现类。

这里既然要和 jwt 整合,JWTFilter 传递给 Realm 的 token 必须是 AuthenticationToken 的实现类。

6.配置 Shiro

1.ShiroConfig

ShiroConfig 主要包含 2 部分:过滤器、安全管理器

过滤器:

安全管理器:

2.自定义 Realm

自定义 Realm 的认证方法主要用来校验 token 的合法性:

代码语言:javascript
复制
@Component
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 限定这个realm只能处理JwtToken
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    /**
     * 授权(授权部分这里就省略了)
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // 获取到用户名,查询用户权限
        return null;
    }

    /**
     * 认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken){
        // 获取token信息
        String token = (String) authenticationToken.getCredentials();
        // 校验token:未校验通过或者已过期
        if (!jwtUtil.verifyToken(token) || jwtUtil.isExpire(token)) {
            throw new AuthenticationException("token已失效,请重新登录");
        }
        //用户信息
        User user = (User) redisUtil.get("token_" + token);
        if (null == user) {
            throw new UnknownAccountException("用户不存在");
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user, token, this.getName());
        return simpleAuthenticationInfo;
    }
}

7.测试

1.登录

代码语言:javascript
复制
@PostMapping("/login")
public Result login(@RequestParam String username, @RequestParam String password) {
    // 从数据库中查找用户的信息,信息正确生成token
    return userService.login(username, password);
}

2.访问资源

token 失效:

token 正常:

8.完整代码

代码语言:javascript
复制
链接: https://pan.baidu.com/s/1kbGI0nyfRMjgKKYd208f5w?pwd=1234 
提取码: 1234 
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-10-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 知否技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.所用技术
  • 2.前置知识
  • 3.流程讲解
  • 4.初始化 SpringBoot 项目
    • 1.新建数据库表
      • 2.新建 SpringBoot 项目
      • 5.配置 JWT
        • 1.JWT 工具类
          • 2.JWTFilter
            • 3.JwtToken
            • 6.配置 Shiro
              • 1.ShiroConfig
                • 2.自定义 Realm
                • 7.测试
                  • 1.登录
                    • 2.访问资源
                    • 8.完整代码
                    相关产品与服务
                    云数据库 Redis
                    腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档