欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2022-11-10 09:22:06|栏目:JAVA代码|点击:

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

依赖

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

  • 关键依赖:
<dependency>
    <groupId>cn.dev33</groupId>
    <artifactId>sa-token-spring-boot-starter</artifactId>
    <version>1.28.0</version>
</dependency>
  • 其他依赖:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
        <groupId>com.fengwenyi</groupId>
        <artifactId>JavaLib</artifactId>
        <version>2.1.5</version>
    </dependency>

    <dependency>
        <groupId>com.fengwenyi</groupId>
        <artifactId>api-result</artifactId>
        <version>2.5.1</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
</dependencies>

登录

  • 构造用户数据
public class UserData {

    private static final Map<String, User> 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<String> roles;

        public User(String uid, String username, String password, List<String> 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<String> queryRolesByUid(String uid) {
        for (Map.Entry<String, User> 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<LoginResponseVo> 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<Void> logout() {
        StpUtil.logout();
        return ResponseTemplate.success();
    }

}

前后端分离

header 添加

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

上一篇:Java案例实现不重复的随机数

栏    目:JAVA代码

下一篇:java web实现自动登录

本文标题:SpringBoot整合Sa-Token实现登录认证的示例代码

本文地址:http://www.codeinn.net/misctech/218617.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有