重构为maven多模块架构

This commit is contained in:
shengzhang 2020-12-22 01:11:28 +08:00
parent 4962742988
commit 4f22be308e
48 changed files with 591 additions and 435 deletions

21
LICENSE
View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2020 www.uviewui.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,92 +0,0 @@
<p align="center">
<img alt="logo" src="https://gitee.com/sz6/sa-token/raw/master/sa-token-doc/doc/logo.png" width="150" height="150" style="margin-bottom: 10px;">
</p>
<h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">sa-token v1.6.0</h1>
<h4 align="center">一个JavaWeb轻量级权限认证框架功能全面上手简单</h4>
<h4 align="center">
<a href="https://gitee.com/sz6/sa-token/stargazers"><img src="https://gitee.com/sz6/sa-token/badge/star.svg"></a>
<a href="https://github.com/click33/sa-token"><img src="https://img.shields.io/badge/sa--token-v1.6.0-2B9939"></a>
<a href="https://github.com/click33/sa-token/stargazers"><img src="https://img.shields.io/github/stars/click33/sa-token"></a>
<a href="https://github.com/click33/sa-token/watchers"><img src="https://img.shields.io/github/watchers/click33/sa-token"></a>
<a href="https://github.com/click33/sa-token/network/members"><img src="https://img.shields.io/github/forks/click33/sa-token"></a>
<a href="https://github.com/click33/sa-token/issues"><img src="https://img.shields.io/github/issues/click33/sa-token.svg"></a>
<a href="https://github.com/click33/sa-token/blob/master/LICENSE"><img src="https://img.shields.io/github/license/click33/sa-token.svg"></a>
</h4>
---
## 😘 在线资料
- ##### [官网首页http://sa-token.dev33.cn/](http://sa-token.dev33.cn/)
- ##### [在线文档http://sa-token.dev33.cn/doc/index.html](http://sa-token.dev33.cn/doc/index.html)
- ##### [需求提交:我们深知一个优秀的项目需要海纳百川,点我在线提交需求](http://sa-app.dev33.cn/wall.html?name=sa-token)
- ##### [开源不易求鼓励点个star吧](https://github.com/click33/sa-token)
## ⭐ sa-token是什么
- **sa-token是一个JavaWeb轻量级权限认证框架其API调用非常简单有多简单呢以登录验证为例你只需要**
``` java
// 在登录时写入当前会话的账号id
StpUtil.setLoginId(10001);
```
- **然后在任意需要验证登录权限的地方:**
``` java
// 检测是否登录 --- 如果当前会话未登录,下面这句代码会抛出 `NotLoginException`异常
StpUtil.checkLogin();
```
- **没有复杂的封装!不要任何的配置!先写入,后鉴权!只需这两行简单的调用,即可轻松完成系统登录鉴权!**
## 🔥 框架设计思想
与其它权限认证框架相比,`sa-token`尽力保证两点:
- 上手简单:能自动化的配置全部自动化,不让你费脑子
- 功能强大:能涵盖的功能全部涵盖,不让你用个框架还要自己给框架打各种补丁
## 💦️️ 涵盖功能
- ⚡ **登录验证** —— 轻松登录鉴权,并提供五种细分场景值
- ⚡ **权限验证** —— 拦截违规调用,不同角色不同授权
- ⚡ **自定义session会话** —— 专业的数据缓存中心
- ⚡ **踢人下线** —— 将违规用户立刻清退下线
- ⚡ **模拟他人账号** —— 实时操作任意用户状态数据
- ⚡ **持久层扩展** —— 可集成redis、MongoDB等专业缓存中间件
- ⚡ **多账号认证体系** —— 比如一个商城项目的user表和admin表分开鉴权
- ⚡ **无cookie模式** —— APP、小程序等前后台分离场景
- ⚡ **注解式鉴权** —— 优雅的将鉴权与业务代码分离
- ⚡ **花式token生成** —— 内置六种token风格还可自定义token生成策略
- ⚡ **组件自动注入** —— 零配置与Spring等框架集成
- ⚡ **更多功能正在集成中...** —— 如有您有好想法或者建议,欢迎加群交流
## 🔨 贡献代码
sa-token欢迎大家贡献代码为框架添砖加瓦
1. 在github上fork一份到自己的仓库
2. clone自己的仓库到本地电脑
3. 在本地电脑修改、commit、push
4. 提交pr点击New Pull Request
5. 等待合并
## 🌱 建议贡献的地方
- 修复源码现有bug或增加新的实用功能
- 完善在线文档,或者修复现有错误之处
- 更多demo示例比如SSM版搭建步骤
- 您可以参考项目issues与需求墙进行贡献
- 如果更新实用功能,可在文档友情链接处留下自己的推广链接
## 🚀 友情链接
[**[ okhttps ]** 一个轻量级http通信框架支持 WebSocket 以及 Stomp 协议](https://gitee.com/ejlchina-zhxu/okhttps)
## 😎 交流群
QQ交流群[1002350610 点击加入](https://jq.qq.com/?_wv=1027&k=45H977HM) ,欢迎你的加入
![扫码加群](https://color-test.oss-cn-qingdao.aliyuncs.com/sa-token/qq-group.png ':size=150')

13
license.txt Normal file
View File

@ -0,0 +1,13 @@
Copyright 1999-2018 Alibaba Group Holding Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

87
pom.xml Normal file
View File

@ -0,0 +1,87 @@
<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 基础信息 -->
<groupId>cn.dev33</groupId>
<artifactId>sa-token-parent</artifactId>
<packaging>pom</packaging>
<version>1.6.0</version>
<!-- 项目介绍 -->
<name>sa-token</name>
<description>A Java Web lightweight authority authentication framework, comprehensive function, easy to use</description>
<url>https://github.com/click33/sa-token</url>
<!-- 所有模块 -->
<modules>
<module>sp-token-core</module>
<module>sa-token-spring-boot-starter</module>
</modules>
<!-- 开源协议 apache 2.0 -->
<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<!-- 一些属性 -->
<properties>
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>
</properties>
<!-- 仓库信息 -->
<scm>
<tag>master</tag>
<url>https://github.com/click33/sa-token.git</url>
<connection>scm:git:https://github.com/click33/sa-token.git</connection>
<developerConnection>scm:git:https://github.com/click33/sa-token.git</developerConnection>
</scm>
<!-- 作者信息 -->
<developers>
<developer>
<name>shengzhang</name>
<email>2393584716@qq.com</email>
</developer>
</developers>
<!-- 父仓库 -->
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
<!-- 仓库依赖 -->
<dependencies>
</dependencies>
<!-- 项目构建 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -36,7 +36,8 @@
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/boot/spring-boot-starter-aop/2.0.0.RELEASE/spring-boot-starter-aop-2.0.0.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-aop/5.0.4.RELEASE/spring-aop-5.0.4.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/aspectj/aspectjweaver/1.8.13/aspectjweaver-1.8.13.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/cn/dev33/sa-token/1.6.0/sa-token-1.6.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/cn/dev33/sa-token/1.4.0/sa-token-1.4.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/boot/spring-boot-starter-redis/1.4.7.RELEASE/spring-boot-starter-redis-1.4.7.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/data/spring-data-redis/2.0.5.RELEASE/spring-data-redis-2.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/data/spring-data-keyvalue/2.0.5.RELEASE/spring-data-keyvalue-2.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>

View File

@ -32,10 +32,17 @@
<version>1.6.0</version>
</dependency> -->
<!-- sa-token -->
<!-- <dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token</artifactId>
<version>1.4.0</version>
</dependency> -->
<!-- sa-token 权限认证, 在线文档http://sa-token.dev33.cn/ -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token</artifactId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.6.0</version>
</dependency>

View File

@ -4,9 +4,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import cn.dev33.satoken.SaTokenManager;
import cn.dev33.satoken.spring.SaTokenSetup;
@SaTokenSetup // 必须有这个注解用来标注加载sa-token
@SpringBootApplication
public class SaTokenDemoApplication {

View File

@ -4,8 +4,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import cn.dev33.satoken.annotation.SaCheckInterceptor;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.interceptor.SaCheckInterceptor;
/**
* sa-token代码方式进行配置

View File

@ -22,7 +22,7 @@ spring:
# 是否在初始化配置时打印版本字符画
is-v: true
# redis配置
redis:
# Redis数据库索引默认为0

View File

@ -1,128 +0,0 @@
package cn.dev33.satoken;
import cn.dev33.satoken.action.SaTokenAction;
import cn.dev33.satoken.action.SaTokenActionDefaultImpl;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.config.SaTokenConfigFactory;
import cn.dev33.satoken.cookie.SaCookieOper;
import cn.dev33.satoken.cookie.SaCookieOperDefaultImpl;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.SaTokenDaoDefaultImpl;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpInterfaceDefaultImpl;
import cn.dev33.satoken.util.SaTokenInsideUtil;
/**
* 管理sa-token所有对象
* @author kong
*
*/
public class SaTokenManager {
/**
* 配置文件 Bean
*/
private static SaTokenConfig config;
public static SaTokenConfig getConfig() {
if (config == null) {
initConfig();
}
return config;
}
public static void setConfig(SaTokenConfig config) {
SaTokenManager.config = config;
if(config.getIsV()) {
SaTokenInsideUtil.printSaToken();
}
}
public synchronized static void initConfig() {
if (config == null) {
setConfig(SaTokenConfigFactory.createConfig());
}
}
/**
* 持久化 Bean
*/
public static SaTokenDao dao;
public static SaTokenDao getDao() {
if (dao == null) {
initDao();
}
return dao;
}
public static void setDao(SaTokenDao dao) {
SaTokenManager.dao = dao;
}
public synchronized static void initDao() {
if (dao == null) {
setDao(new SaTokenDaoDefaultImpl());
}
}
/**
* 权限认证 Bean
*/
public static StpInterface stp;
public static StpInterface getStp() {
if (stp == null) {
initStp();
}
return stp;
}
public static void setStp(StpInterface stp) {
SaTokenManager.stp = stp;
}
public synchronized static void initStp() {
if (stp == null) {
setStp(new StpInterfaceDefaultImpl());
}
}
/**
* sa-token行为 Bean
*/
public static SaTokenAction sta;
public static SaTokenAction getSta() {
if (sta == null) {
initSta();
}
return sta;
}
public static void setSta(SaTokenAction sta) {
SaTokenManager.sta = sta;
}
public synchronized static void initSta() {
if (sta == null) {
setSta(new SaTokenActionDefaultImpl());
}
}
/**
* sa-token cookie操作 Bean
*/
public static SaCookieOper saCookieOper;
public static SaCookieOper getSaCookieOper() {
if (saCookieOper == null) {
initSaCookieOper();
}
return saCookieOper;
}
public static void setSaCookieOper(SaCookieOper saCookieOper) {
SaTokenManager.saCookieOper = saCookieOper;
}
public synchronized static void initSaCookieOper() {
if (saCookieOper == null) {
setSaCookieOper(new SaCookieOperDefaultImpl());
}
}
}

View File

@ -1,80 +0,0 @@
package cn.dev33.satoken.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import cn.dev33.satoken.SaTokenManager;
import cn.dev33.satoken.action.SaTokenAction;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.cookie.SaCookieOper;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.stp.StpInterface;
/**
* 与SpringBoot集成, 保证此类被扫描即可完成sa-token与SpringBoot的集成
* @author kong
*
*/
@Component
public class SpringSaToken {
/**
* 获取配置Bean
* @return .
*/
@Bean
@ConfigurationProperties(prefix="spring.sa-token")
public SaTokenConfig getSaTokenConfig() {
return new SaTokenConfig();
}
/**
* 注入配置Bean
* @param saTokenConfig .
*/
@Autowired
public void setConfig(SaTokenConfig saTokenConfig){
SaTokenManager.setConfig(saTokenConfig);
}
/**
* 注入持久化Bean
* @param dao .
*/
@Autowired(required = false)
public void setDao(SaTokenDao dao){
SaTokenManager.setDao(dao);
}
/**
* 注入权限认证Bean
* @param stp .
*/
@Autowired(required = false)
public void setStp(StpInterface stp){
SaTokenManager.setStp(stp);
}
/**
* 注入Cookie操作Bean
* @param saCookieOper .
*/
@Autowired(required = false)
public void setSaCookieOper(SaCookieOper saCookieOper){
SaTokenManager.setSaCookieOper(saCookieOper);
}
/**
* 注入框架行为Bean
* @param sta .
*/
@Autowired(required = false)
public void setSta(SaTokenAction sta){
SaTokenManager.setSta(sta);
}
}

View File

@ -67,7 +67,7 @@ footer a:hover{text-decoration: underline;}
}
/* 闪光背景 */
.main-box{
.z-div{
background-size: 500%;
background-image: linear-gradient(125deg,#BFFEBE,#F6F8B5,#FCD0B3,#BFB6F8,#E8D8B3);
animation: bganimation 15s infinite;

View File

@ -14,7 +14,7 @@
</head>
<body>
<div style="height: 100vh; overflow: auto;">
<div class="z-div" style="height: 100vh; overflow: auto;">
<header>
<a href="/" style="text-decoration: none;">
<div class="logo-box">

12
sa-token-spring-boot-starter/.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
target/
node_modules/
bin/
.settings/
unpackage/
.classpath
.project
.factorypath
.idea/

View File

@ -1,41 +1,39 @@
<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 基础信息 -->
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dev</artifactId>
<packaging>jar</packaging>
<version>1.6.0</version>
<!-- SpringBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-parent</artifactId>
<version>1.6.0</version>
</parent>
<packaging>jar</packaging>
<name>sa-token-spring-boot-starter</name>
<artifactId>sa-token-spring-boot-starter</artifactId>
<description>springboot integrate sa-token</description>
<dependencies>
<!-- springboot依赖 -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- @ConfigurationProperties -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.0.0.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
</project>

View File

@ -1,4 +1,4 @@
package cn.dev33.satoken.spring;
package cn.dev33.satoken.autowired;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
@ -17,7 +17,7 @@ import org.springframework.context.annotation.Import;
@Target({java.lang.annotation.ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Configuration
@Import({SpringSaToken.class})
@Import({SaTokenSpringAutowired.class})
public @interface SaTokenSetup {
}

View File

@ -0,0 +1,100 @@
package cn.dev33.satoken.autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import cn.dev33.satoken.SaTokenManager;
import cn.dev33.satoken.action.SaTokenAction;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.cookie.SaTokenCookie;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.servlet.SaTokenServlet;
import cn.dev33.satoken.spring.SaTokenServletSpringImpl;
import cn.dev33.satoken.stp.StpInterface;
/**
* 利用spring的自动装配来加载开发者重写的Bean
* @author kong
*
*/
@Component
public class SaTokenSpringAutowired {
/**
* 获取配置Bean
* @return .
*/
@Bean
@ConfigurationProperties(prefix="spring.sa-token")
public SaTokenConfig getSaTokenConfig() {
return new SaTokenConfig();
}
/**
* 注入配置Bean
* @param saTokenConfig .
*/
@Autowired
public void setConfig(SaTokenConfig saTokenConfig){
SaTokenManager.setConfig(saTokenConfig);
}
/**
* 注入持久化Bean
* @param saTokenDao .
*/
@Autowired(required = false)
public void setSaTokenDao(SaTokenDao saTokenDao){
SaTokenManager.setSaTokenDao(saTokenDao);
}
/**
* 注入权限认证Bean
* @param stpInterface .
*/
@Autowired(required = false)
public void setStpInterface(StpInterface stpInterface){
SaTokenManager.setStpInterface(stpInterface);
}
/**
* 注入Cookie操作Bean
* @param saTokenCookie .
*/
@Autowired(required = false)
public void setSaTokenCookie(SaTokenCookie saTokenCookie){
SaTokenManager.setSaTokenCookie(saTokenCookie);
}
/**
* 注入框架行为Bean
* @param saTokenAction .
*/
@Autowired(required = false)
public void setSaTokenAction(SaTokenAction saTokenAction){
SaTokenManager.setSaTokenAction(saTokenAction);
}
/**
* 获取Servlet操作Bean (Spring版)
* @return Servlet操作Bean (Spring版)
*/
@Bean
public SaTokenServlet getSaTokenServlet() {
return new SaTokenServletSpringImpl();
}
/**
* 注入Servlet操作Bean
* @param saTokenServlet .
*/
@Autowired
public void setSaTokenServlet(SaTokenServlet saTokenServlet){
SaTokenManager.setSaTokenServlet(saTokenServlet);
}
}

View File

@ -1,4 +1,4 @@
package cn.dev33.satoken.annotation;
package cn.dev33.satoken.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -6,6 +6,8 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;

View File

@ -0,0 +1,32 @@
package cn.dev33.satoken.spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.dev33.satoken.servlet.SaTokenServlet;
/**
* sa-token 对cookie的相关操作 接口实现类
* @author kong
*
*/
public class SaTokenServletSpringImpl implements SaTokenServlet {
/**
* 获取当前请求的Request对象
*/
@Override
public HttpServletRequest getRequest() {
return SpringMVCUtil.getRequest();
}
/**
* 获取当前请求的Response对象
*/
@Override
public HttpServletResponse getResponse() {
return SpringMVCUtil.getResponse();
}
}

View File

@ -1,4 +1,4 @@
package cn.dev33.satoken.util;
package cn.dev33.satoken.spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -11,11 +11,11 @@ import org.springframework.web.context.request.ServletRequestAttributes;
* @author kong
*
*/
public class SpringMvcUtil {
public class SpringMVCUtil {
/**
* 获取当前会话的 request
* @return
* @return .
*/
public static HttpServletRequest getRequest() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();// 大善人SpringMVC提供的封装
@ -27,7 +27,7 @@ public class SpringMvcUtil {
/**
* 获取当前会话的 response
* @return
* @return .
*/
public static HttpServletResponse getResponse() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();// 大善人SpringMVC提供的封装

View File

@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.autowired.SaTokenSpringAutowired

12
sp-token-core/.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
target/
node_modules/
bin/
.settings/
unpackage/
.classpath
.project
.factorypath
.idea/

28
sp-token-core/pom.xml Normal file
View File

@ -0,0 +1,28 @@
<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-parent</artifactId>
<version>1.6.0</version>
</parent>
<packaging>jar</packaging>
<name>sa-token-core</name>
<artifactId>sa-token-core</artifactId>
<description>A Java Web lightweight authority authentication framework, comprehensive function, easy to use</description>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,149 @@
package cn.dev33.satoken;
import cn.dev33.satoken.action.SaTokenAction;
import cn.dev33.satoken.action.SaTokenActionDefaultImpl;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.config.SaTokenConfigFactory;
import cn.dev33.satoken.cookie.SaTokenCookie;
import cn.dev33.satoken.cookie.SaTokenCookieDefaultImpl;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.SaTokenDaoDefaultImpl;
import cn.dev33.satoken.servlet.SaTokenServlet;
import cn.dev33.satoken.servlet.SaTokenServletDefaultImpl;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpInterfaceDefaultImpl;
import cn.dev33.satoken.util.SaTokenInsideUtil;
/**
* 管理sa-token所有对象
* @author kong
*
*/
public class SaTokenManager {
/**
* 配置文件 Bean
*/
private static SaTokenConfig config;
public static SaTokenConfig getConfig() {
if (config == null) {
initConfig();
}
return config;
}
public static void setConfig(SaTokenConfig config) {
SaTokenManager.config = config;
if(config.getIsV()) {
SaTokenInsideUtil.printSaToken();
}
}
public synchronized static void initConfig() {
if (config == null) {
setConfig(SaTokenConfigFactory.createConfig());
}
}
/**
* 持久化 Bean
*/
public static SaTokenDao saTokenDao;
public static SaTokenDao getSaTokenDao() {
if (saTokenDao == null) {
initSaTokenDao();
}
return saTokenDao;
}
public static void setSaTokenDao(SaTokenDao saTokenDao) {
SaTokenManager.saTokenDao = saTokenDao;
}
public synchronized static void initSaTokenDao() {
if (saTokenDao == null) {
setSaTokenDao(new SaTokenDaoDefaultImpl());
}
}
/**
* 权限认证 Bean
*/
public static StpInterface stpInterface;
public static StpInterface getStpInterface() {
if (stpInterface == null) {
initStpInterface();
}
return stpInterface;
}
public static void setStpInterface(StpInterface stpInterface) {
SaTokenManager.stpInterface = stpInterface;
}
public synchronized static void initStpInterface() {
if (stpInterface == null) {
setStpInterface(new StpInterfaceDefaultImpl());
}
}
/**
* 框架行为 Bean
*/
public static SaTokenAction saTokenAction;
public static SaTokenAction getSaTokenAction() {
if (saTokenAction == null) {
initSaTokenAction();
}
return saTokenAction;
}
public static void setSaTokenAction(SaTokenAction saTokenAction) {
SaTokenManager.saTokenAction = saTokenAction;
}
public synchronized static void initSaTokenAction() {
if (saTokenAction == null) {
setSaTokenAction(new SaTokenActionDefaultImpl());
}
}
/**
* Cookie操作 Bean
*/
public static SaTokenCookie saTokenCookie;
public static SaTokenCookie getSaTokenCookie() {
if (saTokenCookie == null) {
initSaTokenCookie();
}
return saTokenCookie;
}
public static void setSaTokenCookie(SaTokenCookie saTokenCookie) {
SaTokenManager.saTokenCookie = saTokenCookie;
}
public synchronized static void initSaTokenCookie() {
if (saTokenCookie == null) {
setSaTokenCookie(new SaTokenCookieDefaultImpl());
}
}
/**
* Servlet操作 Bean
*/
public static SaTokenServlet saTokenServlet;
public static SaTokenServlet getSaTokenServlet() {
if (saTokenServlet == null) {
initSaTokenServlet();
}
return saTokenServlet;
}
public static void setSaTokenServlet(SaTokenServlet saTokenServlet) {
SaTokenManager.saTokenServlet = saTokenServlet;
}
public synchronized static void initSaTokenServlet() {
if (saTokenServlet == null) {
setSaTokenServlet(new SaTokenServletDefaultImpl());
}
}
}

View File

@ -0,0 +1,15 @@
package cn.dev33.satoken.action;
public interface SaTokenAction {
/**
* 生成一个token
* @param loginId 账号id
* @param loginKey 登录标识key
* @return 一个token
*/
public String createToken(Object loginId, String loginKey);
}

View File

@ -2,12 +2,8 @@ package cn.dev33.satoken.action;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.dev33.satoken.SaTokenManager;
import cn.dev33.satoken.util.SaTokenInsideUtil;
import cn.dev33.satoken.util.SpringMvcUtil;
/**
* SaTokenAction 接口的默认实现
@ -16,23 +12,6 @@ import cn.dev33.satoken.util.SpringMvcUtil;
*/
public class SaTokenActionDefaultImpl implements SaTokenAction {
/**
* 获取当前请求的Request对象
*/
@Override
public HttpServletRequest getCurrRequest() {
return SpringMvcUtil.getRequest();
}
/**
* 获取当前请求的Response对象
*/
@Override
public HttpServletResponse getResponse() {
return SpringMvcUtil.getResponse();
}
/**
* 生成一个token

View File

@ -9,7 +9,7 @@ import javax.servlet.http.HttpServletResponse;
* @author kong
*
*/
public interface SaCookieOper {
public interface SaTokenCookie {
/**
* 获取指定cookie .

View File

@ -4,41 +4,39 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.dev33.satoken.util.SaCookieUtil;
/**
* sa-token 对cookie的相关操作 接口实现类
* @author kong
*
*/
public class SaCookieOperDefaultImpl implements SaCookieOper {
public class SaTokenCookieDefaultImpl implements SaTokenCookie {
/**
* 获取指定cookie
*/
public Cookie getCookie(HttpServletRequest request, String cookieName) {
return SaCookieUtil.getCookie(request, cookieName);
return SaTokenCookieUtil.getCookie(request, cookieName);
}
/**
* 添加cookie
*/
public void addCookie(HttpServletResponse response, String name, String value, String path, int timeout) {
SaCookieUtil.addCookie(response, name, value, path, timeout);
SaTokenCookieUtil.addCookie(response, name, value, path, timeout);
}
/**
* 删除cookie
*/
public void delCookie(HttpServletRequest request, HttpServletResponse response, String name) {
SaCookieUtil.delCookie(request, response, name);
SaTokenCookieUtil.delCookie(request, response, name);
}
/**
* 修改cookie的value值
*/
public void updateCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) {
SaCookieUtil.updateCookie(request, response, name, value);
SaTokenCookieUtil.updateCookie(request, response, name, value);
}
}

View File

@ -1,4 +1,4 @@
package cn.dev33.satoken.util;
package cn.dev33.satoken.cookie;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
@ -10,7 +10,7 @@ import javax.servlet.http.HttpServletResponse;
* @author kong
*
*/
public class SaCookieUtil {
public class SaTokenCookieUtil {
/**
* 获取指定cookie .

View File

@ -0,0 +1,21 @@
package cn.dev33.satoken.exception;
/**
* sa-token框架内部逻辑发生错误抛出的异常
* @author kong
*
*/
public class SaTokenException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 6806129545290130132L;
public SaTokenException(String message) {
super(message);
}
}

View File

@ -1,16 +1,21 @@
package cn.dev33.satoken.action;
package cn.dev33.satoken.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface SaTokenAction {
/**
* Servlet相关操作
* @author kong
*
*/
public interface SaTokenServlet {
/**
* 获取当前请求的Request对象
* @return 当前请求的Request对象
*/
public HttpServletRequest getCurrRequest();
public HttpServletRequest getRequest();
/**
* 获取当前会话的 response
@ -18,13 +23,5 @@ public interface SaTokenAction {
*/
public HttpServletResponse getResponse();
/**
* 生成一个token
* @param loginId 账号id
* @param loginKey 登录标识key
* @return 一个token
*/
public String createToken(Object loginId, String loginKey);
}

View File

@ -0,0 +1,30 @@
package cn.dev33.satoken.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* sa-token 对Servlet的相关操作 接口默认实现类
* @author kong
*
*/
public class SaTokenServletDefaultImpl implements SaTokenServlet {
/**
* 获取当前请求的Request对象
*/
@Override
public HttpServletRequest getRequest() {
throw new RuntimeException("请实现SaTokenServlet接口后进行Servlet相关操作");
}
/**
* 获取当前请求的Response对象
*/
@Override
public HttpServletResponse getResponse() {
throw new RuntimeException("请实现SaTokenServlet接口后进行Servlet相关操作");
}
}

View File

@ -137,7 +137,7 @@ public class SaSession implements Serializable {
* 将这个session从持久库更新一下
*/
public void update() {
SaTokenManager.getDao().updateSaSession(this);
SaTokenManager.getSaTokenDao().updateSaSession(this);
}

View File

@ -23,7 +23,7 @@ public class SaSessionCustomUtil {
* @return 是否存在
*/
public boolean isExists(String sessionId) {
return SaTokenManager.getDao().getSaSession(getSessionKey(sessionId)) != null;
return SaTokenManager.getSaTokenDao().getSaSession(getSessionKey(sessionId)) != null;
}
/**
@ -33,10 +33,10 @@ public class SaSessionCustomUtil {
* @return SaSession
*/
public static SaSession getSessionById(String sessionId, boolean isCreate) {
SaSession session = SaTokenManager.getDao().getSaSession(getSessionKey(sessionId));
SaSession session = SaTokenManager.getSaTokenDao().getSaSession(getSessionKey(sessionId));
if(session == null && isCreate) {
session = new SaSession(getSessionKey(sessionId));
SaTokenManager.getDao().saveSaSession(session, SaTokenManager.getConfig().getTimeout());
SaTokenManager.getSaTokenDao().saveSaSession(session, SaTokenManager.getConfig().getTimeout());
}
return session;
}
@ -54,7 +54,7 @@ public class SaSessionCustomUtil {
* @param sessionId 删除指定key
*/
public static void deleteSessionById(String sessionId) {
SaTokenManager.getDao().deleteSaSession(getSessionKey(sessionId));
SaTokenManager.getSaTokenDao().deleteSaSession(getSessionKey(sessionId));
}

View File

@ -53,7 +53,7 @@ public class StpLogic {
* @return 生成的tokenValue
*/
public String randomTokenValue(Object loginId) {
return SaTokenManager.getSta().createToken(loginId, loginKey);
return SaTokenManager.getSaTokenAction().createToken(loginId, loginKey);
}
/**
@ -62,7 +62,7 @@ public class StpLogic {
*/
public String getTokenValue(){
// 0获取相应对象
HttpServletRequest request = SaTokenManager.getSta().getCurrRequest();
HttpServletRequest request = SaTokenManager.getSaTokenServlet().getRequest();
SaTokenConfig config = SaTokenManager.getConfig();
String keyTokenName = getTokenName();
@ -86,7 +86,7 @@ public class StpLogic {
}
// 4尝试从cookie里读取
if(config.getIsReadCookie() == true){
Cookie cookie = SaTokenManager.getSaCookieOper().getCookie(request, keyTokenName);
Cookie cookie = SaTokenManager.getSaTokenCookie().getCookie(request, keyTokenName);
if(cookie != null){
String tokenValue = cookie.getValue();
if(tokenValue != null) {
@ -104,7 +104,7 @@ public class StpLogic {
* @return .
*/
public String getTokenValueByLoginId(Object loginId) {
return SaTokenManager.getDao().getValue(getKeyLoginId(loginId));
return SaTokenManager.getSaTokenDao().getValue(getKeyLoginId(loginId));
}
/**
@ -128,9 +128,9 @@ public class StpLogic {
public void setLoginId(Object loginId) {
// 1获取相应对象
HttpServletRequest request = SaTokenManager.getSta().getCurrRequest();
HttpServletRequest request = SaTokenManager.getSaTokenServlet().getRequest();
SaTokenConfig config = SaTokenManager.getConfig();
SaTokenDao dao = SaTokenManager.getDao();
SaTokenDao dao = SaTokenManager.getSaTokenDao();
// 2获取tokenValue
String tokenValue = getTokenValueByLoginId(loginId); // 获取旧tokenValue
@ -150,7 +150,7 @@ public class StpLogic {
dao.setValue(getKeyLoginId(loginId), tokenValue, config.getTimeout()); // uid -> token
request.setAttribute(SaTokenInsideUtil.JUST_CREATED_SAVE_KEY, tokenValue); // 保存到本次request里
if(config.getIsReadCookie() == true){
SaTokenManager.getSaCookieOper().addCookie(SaTokenManager.getSta().getResponse(), getTokenName(), tokenValue, "/", (int)config.getTimeout()); // cookie注入
SaTokenManager.getSaTokenCookie().addCookie(SaTokenManager.getSaTokenServlet().getResponse(), getTokenName(), tokenValue, "/", (int)config.getTimeout()); // cookie注入
}
}
@ -165,10 +165,10 @@ public class StpLogic {
}
// 如果打开了cookie模式第一步先把cookie清除掉
if(SaTokenManager.getConfig().getIsReadCookie() == true){
SaTokenManager.getSaCookieOper().delCookie(SaTokenManager.getSta().getCurrRequest(), SaTokenManager.getSta().getResponse(), getTokenName());
SaTokenManager.getSaTokenCookie().delCookie(SaTokenManager.getSaTokenServlet().getRequest(), SaTokenManager.getSaTokenServlet().getResponse(), getTokenName());
}
// 尝试从db中获取loginId值
String loginId = SaTokenManager.getDao().getValue(getKeyTokenValue(tokenValue));
String loginId = SaTokenManager.getSaTokenDao().getValue(getKeyTokenValue(tokenValue));
// 如果根本查不到loginId那么也无需执行任何操作
if(loginId == null) {
return;
@ -194,9 +194,9 @@ public class StpLogic {
}
// 清除相关数据
SaTokenManager.getDao().delKey(getKeyTokenValue(tokenValue)); // 清除token-id键值对
SaTokenManager.getDao().delKey(getKeyLoginId(loginId)); // 清除id-token键值对
SaTokenManager.getDao().deleteSaSession(getKeySession(loginId)); // 清除其session
SaTokenManager.getSaTokenDao().delKey(getKeyTokenValue(tokenValue)); // 清除token-id键值对
SaTokenManager.getSaTokenDao().delKey(getKeyLoginId(loginId)); // 清除id-token键值对
SaTokenManager.getSaTokenDao().deleteSaSession(getKeySession(loginId)); // 清除其session
}
/**
@ -212,9 +212,9 @@ public class StpLogic {
}
// 清除相关数据
SaTokenManager.getDao().updateValue(getKeyTokenValue(tokenValue), NotLoginException.KICK_OUT); // 标记已被踢下线
SaTokenManager.getDao().delKey(getKeyLoginId(loginId)); // 清除id-token键值对
SaTokenManager.getDao().deleteSaSession(getKeySession(loginId)); // 清除其session
SaTokenManager.getSaTokenDao().updateValue(getKeyTokenValue(tokenValue), NotLoginException.KICK_OUT); // 标记已被踢下线
SaTokenManager.getSaTokenDao().delKey(getKeyLoginId(loginId)); // 清除id-token键值对
SaTokenManager.getSaTokenDao().deleteSaSession(getKeySession(loginId)); // 清除其session
}
// 查询相关
@ -246,7 +246,7 @@ public class StpLogic {
throw NotLoginException.newInstance(loginKey, NotLoginException.NOT_TOKEN);
}
// 查找此token对应loginId, 则抛出无效token
String loginId = SaTokenManager.getDao().getValue(getKeyTokenValue(tokenValue));
String loginId = SaTokenManager.getSaTokenDao().getValue(getKeyTokenValue(tokenValue));
if(loginId == null) {
throw NotLoginException.newInstance(loginKey, NotLoginException.INVALID_TOKEN);
}
@ -302,7 +302,7 @@ public class StpLogic {
return null;
}
// loginId为null或者在异常项里面均视为未登录
Object loginId = SaTokenManager.getDao().getValue(getKeyTokenValue(tokenValue));
Object loginId = SaTokenManager.getSaTokenDao().getValue(getKeyTokenValue(tokenValue));
if(loginId == null || NotLoginException.ABNORMAL_LIST.contains(loginId)) {
return null;
}
@ -348,7 +348,7 @@ public class StpLogic {
*/
public Object getLoginIdByToken(String tokenValue) {
if(tokenValue != null) {
Object loginId = SaTokenManager.getDao().getValue(getKeyTokenValue(tokenValue));
Object loginId = SaTokenManager.getSaTokenDao().getValue(getKeyTokenValue(tokenValue));
if(loginId != null) {
return loginId;
}
@ -366,10 +366,10 @@ public class StpLogic {
* @return .
*/
protected SaSession getSessionBySessionId(String sessionId, boolean isCreate) {
SaSession session = SaTokenManager.getDao().getSaSession(sessionId);
SaSession session = SaTokenManager.getSaTokenDao().getSaSession(sessionId);
if(session == null && isCreate) {
session = new SaSession(sessionId);
SaTokenManager.getDao().saveSaSession(session, SaTokenManager.getConfig().getTimeout());
SaTokenManager.getSaTokenDao().saveSaSession(session, SaTokenManager.getConfig().getTimeout());
}
return session;
}
@ -412,7 +412,7 @@ public class StpLogic {
* @return .
*/
public boolean hasPermission(Object loginId, Object pcode) {
List<Object> pcodeList = SaTokenManager.getStp().getPermissionCodeList(loginId, loginKey);
List<Object> pcodeList = SaTokenManager.getStpInterface().getPermissionCodeList(loginId, loginKey);
return !(pcodeList == null || pcodeList.contains(pcode) == false);
}
@ -441,7 +441,7 @@ public class StpLogic {
*/
public void checkPermissionAnd(Object... pcodeArray){
Object loginId = getLoginId();
List<Object> pcodeList = SaTokenManager.getStp().getPermissionCodeList(loginId, loginKey);
List<Object> pcodeList = SaTokenManager.getStpInterface().getPermissionCodeList(loginId, loginKey);
for (Object pcode : pcodeArray) {
if(pcodeList.contains(pcode) == false) {
throw new NotPermissionException(pcode, this.loginKey); // 没有权限抛出异常
@ -455,7 +455,7 @@ public class StpLogic {
*/
public void checkPermissionOr(Object... pcodeArray){
Object loginId = getLoginId();
List<Object> pcodeList = SaTokenManager.getStp().getPermissionCodeList(loginId, loginKey);
List<Object> pcodeList = SaTokenManager.getStpInterface().getPermissionCodeList(loginId, loginKey);
for (Object pcode : pcodeArray) {
if(pcodeList.contains(pcode) == true) {
return; // 有的话提前退出

View File

@ -2,15 +2,12 @@ package cn.dev33.satoken.stp;
import java.util.Map;
import org.springframework.stereotype.Service;
import cn.dev33.satoken.session.SaSession;
/**
* 一个默认的实现
* @author kong
*/
@Service
public class StpUtil {
/**

View File

@ -18,7 +18,7 @@ public class SaTokenInsideUtil {
/**
* sa-token 开源地址
*/
public static final String GITHUB_URL= "https://github.com/click33/sa-token";
public static final String GITHUB_URL = "https://github.com/click33/sa-token";
/**
* 打印 sa-token