A lightweight Java permission authentication framework – Sa-Token

A lightweight Java permission authentication framework – Sa-Token

2022-09-02 0 443
Resource Number 37493 Last Updated 2025-02-24
¥ 0USD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation
This issue recommends a lightweight Java permission authentication framework called Sa-Token.

Sa-Token is a lightweight Java permission authentication framework, mainly to solve: login authentication, permission authentication, Session session, single sign-on, OAuth2.0, microservice network authentication and a series of permissions related issues.

A lightweight Java permission authentication framework – Sa-Token插图

Overview of Sa-Token functions:
Login authentication – single-end login, multi-end login, mutually exclusive login, no login within seven days
Permission authentication: Permission authentication, role authentication, and session level-2 authentication
Session Session: All-end shared Session, single-end exclusive Session, and customized session
Kick a user offline: Kick a user offline based on the account id and Token value
Account blocking – designated days of blocking, permanent blocking, set unblocking time
Persistence layer extension – can integrate Redis, Memcached and other professional cache middleware, restart data without loss
Distributed session – Provides two distributed session solutions for jwt integration and shared data centers
Microservice authentication – Route blocking authentication for common gateways such as Gateway, ShenYu, and Zuul
Single sign-on – Three single sign-on modes are built in: cross-domain, shared Redis or not
OAuth2.0 authentication — Based on RFC-6749 standard, OAuth2.0 standard process of authorization authentication, support openid mode
Level 2 authentication: Re-authentication on the basis of login to ensure security
Basic Authentication – A line of code accesses Http Basic authentication
Standalone Redis – Separate the permission cache from the business cache
Temporary Token authentication – Solves the problem of short-term Token authorization
Simulate other accounts – manipulate any user status data in real time
Temporary Identity switching – Temporarily switching the session identity to another account
Separation of front and back – APP, small program and other terminals that do not support cookies
The same terminal is mutually exclusive login – like QQ mobile phone computer online at the same time, but the two mobile phones are mutually exclusive login
Multi-account authentication system – for example, the user table and admin table of a mall project are authenticated separately
Fancy token generation – built-in six Token styles, also can: custom Token generation strategy, custom Token prefix
Annotated authentication – elegantly separates authentication from business code
Route interception authentication – Based on route interception authentication, the restful mode can be adapted
Automatic renewal – Provides two Token expiration policies, which can be flexibly used together and can be automatically renewed
Session governance – Provides a convenient and flexible session query interface
Remember Me mode – Adapt [Remember me] mode, restart the browser without verification
Password encryption – Provides password encryption module, can fast MD5, SHA1, SHA256, AES, RSA encryption
Global listener – performs some AOP operations when a user logs in, logs out, gets kicked off, and so on
Out of the box – Provides SpringMVC, WebFlux and other common web framework starter integration package, true out of the box
Sa-Token function structure: A lightweight Java permission authentication framework – Sa-Token插图1

Introducing Dependencies (1 of 4):

  • SpringMVC Environment

Maven dependency

<dependency><groupId>cn.dev33</groupId><artifactId>sa-token-spring-boot-starter</artifactId><version>1.27.0</version></dependency>

Gradle dependency

implementation 'cn.dev33:sa-token-spring-boot-starter:1.27.0'
  • WebFlux Environment(Reactor)

Mavendependency

<dependency>    <groupId>cn.dev33</groupId>    <artifactId>sa-token-reactor-spring-boot-starter</artifactId>    <version>1.27.0</version></dependency>

Gradledependency

implementation 'cn.dev33:sa-token-reactor-spring-boot-starter:1.27.0'
  • ServletContainer environment

Mavendependency

<dependency>    <groupId>cn.dev33</groupId>    <artifactId>sa-token-servlet</artifactId>    <version>1.27.0</version></dependency>

Gradledependency

implementation 'cn.dev33:sa-token-servlet:1.27.0'
  • else

Mavendependency

<dependency>    <groupId>cn.dev33</groupId>    <artifactId>sa-token-core</artifactId>    <version>1.27.0</version></dependency>

Gradledependency

implementation 'cn.dev33:sa-token-core:1.27.0'

give a typical example:

The API design of Sa-Token is very simple, taking login authentication as an example, you only need to:

// The account of the current session is written during login :idStpUtil.login(10001);// Then call the following method where you want to verify the login:// 如果当前会话未登录,这句代码会抛出 `NotLoginException` 异常StpUtil.checkLogin();

At this point, we have completed the login authentication with Sa-Token!

Permission authentication example (Only sessions with user:add permissions can enter the request)

@SaCheckPermission("user:add")@RequestMapping("/user/insert")public String insert(SysUser user) {// ... return "User increase";}

Kick an account offline (throws a NotLoginException when the other party accesses the system again))

// Example Kick session 10001 offline StpUtil.kickout(10001);

In Sa-Token, most functions can be done with a single line of code:

StpUtil.login(10001);                     // Mark the account for which the current session is logged in idStpUtil.getLoginId();                     // 获取当前会话登录的账号idStpUtil.isLogin();                        // 获取当前会话是否已经登录, 返回true或falseStpUtil.logout();                         // 当前会话注销登录StpUtil.kickout(10001);                   // 将账号为10001的会话踢下线StpUtil.hasRole("super-admin");           // 查询当前账号是否含有指定角色标识, 返回true或falseStpUtil.hasPermission("user:add");        // 查询当前账号是否含有指定权限, 返回true或falseStpUtil.getSession();                     // 获取当前账号id的SessionStpUtil.getSessionByLoginId(10001);       // 获取账号id为10001的SessionStpUtil.getTokenValueByLoginId(10001);    // 获取账号id为10001的token令牌值StpUtil.login(10001, "PC");               // 指定设备标识登录,常用于“同端互斥登录”StpUtil.kickout(10001, "PC");             // 指定账号指定设备标识踢下线 (不同端不受影响)StpUtil.openSafe(120);                    // 在当前会话开启二级认证,有效期为120秒 StpUtil.checkSafe();                      // 校验当前会话是否处于二级认证有效期内,校验失败会抛出异常 StpUtil.switchTo(10044);                  // 将当前会话身份临时切换为其它账号

谁在使用Sa-Token:

A lightweight Java permission authentication framework – Sa-Token插图2

A lightweight Java permission authentication framework – Sa-Token插图3

You can read more on your own。


If you need project recommendations and resources, please send private letters to the author

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free source code A lightweight Java permission authentication framework – Sa-Token https://ictcoder.com/kyym/a-lightweight-java-permission-authentication-framework-sa-token.html

Share free open-source source code

Q&A
  • 1, automatic: after taking the photo, click the (download) link to download; 2. Manual: After taking the photo, contact the seller to issue it or contact the official to find the developer to ship.
View details
  • 1, the default transaction cycle of the source code: manual delivery of goods for 1-3 days, and the user payment amount will enter the platform guarantee until the completion of the transaction or 3-7 days can be issued, in case of disputes indefinitely extend the collection amount until the dispute is resolved or refunded!
View details
  • 1. Heptalon will permanently archive the process of trading between the two parties and the snapshots of the traded goods to ensure that the transaction is true, effective and safe! 2, Seven PAWS can not guarantee such as "permanent package update", "permanent technical support" and other similar transactions after the merchant commitment, please identify the buyer; 3, in the source code at the same time there is a website demonstration and picture demonstration, and the site is inconsistent with the diagram, the default according to the diagram as the dispute evaluation basis (except for special statements or agreement); 4, in the absence of "no legitimate basis for refund", the commodity written "once sold, no support for refund" and other similar statements, shall be deemed invalid; 5, before the shooting, the transaction content agreed by the two parties on QQ can also be the basis for dispute judgment (agreement and description of the conflict, the agreement shall prevail); 6, because the chat record can be used as the basis for dispute judgment, so when the two sides contact, only communicate with the other party on the QQ and mobile phone number left on the systemhere, in case the other party does not recognize self-commitment. 7, although the probability of disputes is very small, but be sure to retain such important information as chat records, mobile phone messages, etc., in case of disputes, it is convenient for seven PAWS to intervene in rapid processing.
View details
  • 1. As a third-party intermediary platform, Qichou protects the security of the transaction and the rights and interests of both buyers and sellers according to the transaction contract (commodity description, content agreed before the transaction); 2, non-platform online trading projects, any consequences have nothing to do with mutual site; No matter the seller for any reason to require offline transactions, please contact the management report.
View details

Related Article

make a comment
No comments available at the moment
Official customer service team

To solve your worries - 24 hours online professional service