SpringBoot整合Sa-Token实现登录认证的示例代码

今天分享的是 Spring Boot 整合 Sa-Token 实现登录认证。

依赖

首先,我们需要添加依赖:

  • 关键依赖:

    cn.dev33
    sa-token-spring-boot-starter
    1.28.0
  • 其他依赖:

    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        com.fengwenyi
        JavaLib
        2.1.5
    

    
        com.fengwenyi
        api-result
        2.5.1
    

    
        org.projectlombok
        lombok
    

    
        org.springframework.boot
        spring-boot-starter-validation
    

登录

  • 构造用户数据
public class UserData {

    private static final Map users = Map.of(
            "u1", new User("01", "u1", "123456", List.of("ROLE_USER")),
            "a2", new User("02", "a2", "123456", List.of("ROLE_ADMIN", "ROLE_USER"))
    );


    public static class User {
        public String uid;
        public String username;
        public String password;
        public List roles;

        public User(String uid, String username, String password, List roles) {
            this.uid = uid;
            this.username = username;
            this.password = password;
            this.roles = roles;
        }
    }

    public static User queryByUsername(String username) {
        return users.get(username);
    }

    public static List queryRolesByUid(String uid) {
        for (Map.Entry entry : users.entrySet()) {
            if (uid.equals(entry.getValue().uid)) {
                return entry.getValue().roles;
            }
        }
        return null;
    }

}
  • 登录认证
@RestController
@RequestMapping("/auth")
public class AuthController {

    @PostMapping("/login")
    public ResponseTemplate login(@RequestBody @Validated LoginRequestVo requestVo) {
        String username = requestVo.getUsername();
        String password = requestVo.getPassword();

        UserData.User user = UserData.queryByUsername(username);

        if (Objects.isNull(user)) {
            return ResponseTemplate.fail("用户名不正确");
        }

        if (!user.password.equals(password)) {
            return ResponseTemplate.fail("密码不正确");
        }

        StpUtil.login(user.uid);
        LoginResponseVo responseVo = new LoginResponseVo()
                .setToken(StpUtil.getTokenValue())
                ;
        return ResponseTemplate.success(responseVo);
    }

}

关键代码

StpUtil.login(user.uid);
LoginResponseVo responseVo = new LoginResponseVo()
        .setToken(StpUtil.getTokenValue())
        ;
  • 测试

请求:

POST localhost:8080/auth/login

{
    "username": "u1",
    "password": "123456"
}

响应:

{
  "code": 0,
  "message": "Success",
  "success": true,
  "body": {
    "token": "f2f9a82f-bc6a-460a-877e-9d3e4c8867de"
  }
}

退出登录

@RestController
@RequestMapping("/auth")
public class AuthController {
    
    @PostMapping("/logout")
    public ResponseTemplate logout() {
        StpUtil.logout();
        return ResponseTemplate.success();
    }

}

前后端分离

header 添加

satoken = f2f9a82f-bc6a-460a-877e-9d3e4c8867de

到此这篇关于SpringBoot整合Sa-Token实现登录认证的示例代码的文章就介绍到这了,更多相关SpringBoot Sa-Token登录认证内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(SpringBoot整合Sa-Token实现登录认证的示例代码)