一、配置maven依赖
<!-- Sa-Token 权限认证, 在线文档:http://sa-token.dev33.cn/ -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.28.0</version>
</dependency>
二、配置文件
server:
port: 8888
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
max-lifetime: 30000
jpa:
show-sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: update
sa-token:
token-name: yxrtoken
timeout: 2592000
activity-timeout: -1
is-concurrent: true
is-share: false
token-style: uuid
is-log: true
三、创建启动类
package com.ctguyxr.satokendemo;
import cn.dev33.satoken.SaManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* created by IDEA
* @author Xinrui Yu
* @date 2021/11/27 18:21
**/
@SpringBootApplication
public class SatokenDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SatokenDemoApplication.class, args);
System.out.println("启动成功,Sa-Token的配置如下:" + SaManager.getConfig());
}
}
四、创建测试Controller
package com.ctguyxr.satokendemo.controller;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created By Intellij IDEA
*
* @author Xinrui Yu
* @date 2021/11/27 18:26 星期六
*/
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/login")
public String doLogin(String username,String password){
if("admin".equals(username) && "admin".equals(password)){
StpUtil.login(10001);
return "登陆成功";
}
return "登录失败";
}
@GetMapping("/check")
public String isLogin(){
return "当前会话是否已经登录:" + StpUtil.isLogin();
}
}
五、测试
访问本地接口