一、配置maven依赖

  1. <!-- Sa-Token 权限认证, 在线文档:http://sa-token.dev33.cn/ -->
  2. <dependency>
  3. <groupId>cn.dev33</groupId>
  4. <artifactId>sa-token-spring-boot-starter</artifactId>
  5. <version>1.28.0</version>
  6. </dependency>

二、配置文件

  1. server:
  2. port: 8888
  3. spring:
  4. datasource:
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. hikari:
  7. max-lifetime: 30000
  8. jpa:
  9. show-sql: true
  10. database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  11. hibernate:
  12. ddl-auto: update
  13. sa-token:
  14. token-name: yxrtoken
  15. timeout: 2592000
  16. activity-timeout: -1
  17. is-concurrent: true
  18. is-share: false
  19. token-style: uuid
  20. is-log: true

三、创建启动类

  1. package com.ctguyxr.satokendemo;
  2. import cn.dev33.satoken.SaManager;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. /**
  6. * created by IDEA
  7. * @author Xinrui Yu
  8. * @date 2021/11/27 18:21
  9. **/
  10. @SpringBootApplication
  11. public class SatokenDemoApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(SatokenDemoApplication.class, args);
  14. System.out.println("启动成功,Sa-Token的配置如下:" + SaManager.getConfig());
  15. }
  16. }

四、创建测试Controller

  1. package com.ctguyxr.satokendemo.controller;
  2. import cn.dev33.satoken.annotation.SaCheckRole;
  3. import cn.dev33.satoken.stp.StpUtil;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. /**
  9. * Created By Intellij IDEA
  10. *
  11. * @author Xinrui Yu
  12. * @date 2021/11/27 18:26 星期六
  13. */
  14. @RestController
  15. @RequestMapping("/user")
  16. public class UserController {
  17. @GetMapping("/login")
  18. public String doLogin(String username,String password){
  19. if("admin".equals(username) && "admin".equals(password)){
  20. StpUtil.login(10001);
  21. return "登陆成功";
  22. }
  23. return "登录失败";
  24. }
  25. @GetMapping("/check")
  26. public String isLogin(){
  27. return "当前会话是否已经登录:" + StpUtil.isLogin();
  28. }
  29. }

五、测试

访问本地接口
image.png
image.png