目录
[TOC]
官方文档
https://sa-token.cc
SpringBoot前后端分离
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <properties> <sa-token.version>1.37.0</sa-token.version> </properties>
<dependencies> <dependency> <groupId>cn.dev33</groupId> <artifactId>sa-token-spring-boot-starter</artifactId> <version>${sa-token.version}</version> </dependency>
<dependency> <groupId>cn.dev33</groupId> <artifactId>sa-token-redis-jackson</artifactId> <version>${sa-token.version}</version> </dependency> </dependencies>
|
配置文件
配置了sa-token.token-prefix
的值,前端在请求头必须添加如下信息
headerName:sa-token.token-name的值
headerValue:sa-token.token-prefix + 空格 + 登录成功后端返回的token值
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
| spring: redis: host: 127.0.0.1 port: 6379 database: 0 jedis: pool: max-active: 100 max-idle: 100 max-wait: 50 min-idle: 10
sa-token: token-prefix: Bearer token-name: sa-token-study timeout: 2592000 active-timeout: -1 is-concurrent: false is-share: true token-style: simple-uuid is-log: true
|
配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import cn.dev33.satoken.interceptor.SaInterceptor; import cn.dev33.satoken.stp.StpUtil; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration public class SaTokenConfigure implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin())) .addPathPatterns("/**") .excludePathPatterns("/login"); } }
|
认证
登录信息接收实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import lombok.Data;
@Data public class LoginInfoVO {
private String username;
private String password;
private String device; }
|
登录控制器
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
| import cn.dev33.satoken.stp.SaLoginModel; import cn.dev33.satoken.stp.SaTokenInfo; import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.util.SaResult; import com.xiaofei.login.entity.UserEntity; import com.xiaofei.login.service.UserService; import com.xiaofei.login.utils.ResponseUtils; import com.xiaofei.login.vo.LoginInfoVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@Api(tags = "登录") @RestController public class LoginController {
@Autowired private UserService userService;
@ApiOperation(value = "用户登录", httpMethod = "POST", response = ResponseUtils.class, produces = "application/json") @PostMapping("/login") public ResponseUtils login(@RequestBody LoginInfoVO loginInfoVO) throws IOException {
UserEntity userEntity = new UserEntity(); userEntity.setUsername(loginInfoVO.getUsername()); userEntity.setPassword(loginInfoVO.getPassword()); UserEntity respUser = userService.selectOne(userEntity);
if (respUser != null) { StpUtil.login(respUser.getId() , new SaLoginModel() .setDevice(loginInfoVO.getDevice()) .setTimeout(60 * 60 * 12) ); ; SaTokenInfo tokenInfo = StpUtil.getTokenInfo(); return ResponseUtils.success(SaResult.data(tokenInfo)); } throw new RuntimeException("用户名或密码错误"); }
@ApiOperation(value = "检查用户是否登录", httpMethod = "POST", response = ResponseUtils.class, produces = "application/json") @PostMapping("/login/check") public ResponseUtils loginCheck(@RequestBody LoginInfoVO loginInfoVO) throws IOException { return ResponseUtils.success("当前会话是否登录:" + StpUtil.isLogin()); } }
|
测试
登录
登录参数
1 2 3 4 5
| { "device": "PC", "password": "bMpiG70Fc0", "username": "Teresa Marshall" }
|
检查用户登录状态
当用户登录成功后,会给前端返回一个token,前端请求后端的时候,需要按照下面的格式将token的值放在请求头里面将值传给后端,具体查看配置文件中的配置
授权
当登录成功后,访问的请求设置了访问权限,才会触发该接口进行权限校验
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
|
@Component public class StpInterfaceImpl implements StpInterface {
@Override public List<String> getPermissionList(Object loginId, String loginType) { List<String> list = new ArrayList<String>(); return list; }
@Override public List<String> getRoleList(Object loginId, String loginType) { List<String> list = new ArrayList<String>(); return list; } }
|
SpringBoot集成Sa-Token实现认证授权