mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-04-05 17:37:53 +08:00
新增注解式鉴权功能
This commit is contained in:
parent
41b485983b
commit
defdd90336
@ -21,6 +21,7 @@
|
||||
- 持久层扩展(集成redis)
|
||||
- 多账号认证体系(比如一个商城项目的user表和admin表)
|
||||
- 无cookie模式(APP、小程序等前后台分离场景)
|
||||
- 注解式鉴权(优雅的将鉴权与业务代码分离)
|
||||
- 零配置与Spring等框架集成
|
||||
- ...
|
||||
|
||||
|
@ -25,11 +25,11 @@
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- sa-token权限验证 -->
|
||||
<!-- sa-token 权限认证, 在线文档:http://sa-token.dev33.cn/ -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot整合redis -->
|
||||
|
@ -1,18 +1,18 @@
|
||||
//package com.pj.satoken;
|
||||
//
|
||||
//import org.springframework.context.annotation.Bean;
|
||||
//import org.springframework.context.annotation.Configuration;
|
||||
//import org.springframework.context.annotation.Primary;
|
||||
//
|
||||
//import cn.dev33.satoken.config.SaTokenConfig;
|
||||
//
|
||||
///**
|
||||
// * sa-token代码方式进行配置
|
||||
// */
|
||||
//@Configuration
|
||||
//public class MySaTokenConfig {
|
||||
//
|
||||
// // 获取配置Bean
|
||||
package com.pj.satoken;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckInterceptor;
|
||||
|
||||
/**
|
||||
* sa-token代码方式进行配置
|
||||
*/
|
||||
@Configuration
|
||||
public class MySaTokenConfig extends WebMvcConfigurationSupport {
|
||||
|
||||
// 获取配置Bean (以代码的方式配置sa-token)
|
||||
// @Primary
|
||||
// @Bean(name="MySaTokenConfig")
|
||||
// public SaTokenConfig getSaTokenConfig() {
|
||||
@ -25,5 +25,11 @@
|
||||
// config.setIsV(true); // 是否在初始化配置时打印版本字符画
|
||||
// return config;
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
||||
// 注册sa-token的拦截器,打开注解式鉴权功能
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new SaCheckInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.session.SaSessionCustomUtil;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
@ -86,5 +88,21 @@ public class TestController {
|
||||
}
|
||||
|
||||
|
||||
// 测试注解式鉴权, 浏览器访问: http://localhost:8081/test/at_check
|
||||
@SaCheckLogin // 注解式鉴权:当前会话必须登录才能通过
|
||||
@SaCheckPermission("user-add") // 注解式鉴权:当前会话必须具有指定权限才能通过
|
||||
@RequestMapping("at_check")
|
||||
public AjaxJson at_check() {
|
||||
System.out.println("======================= 进入方法,测试注解鉴权接口 ========================= ");
|
||||
System.out.println("只有通过注解鉴权,才能进入此方法");
|
||||
return AjaxJson.getSuccess();
|
||||
}
|
||||
|
||||
|
||||
@SaCheckLogin // 注解式鉴权:当前会话必须登录才能通过
|
||||
@RequestMapping("getInfo")
|
||||
public String getInfo() {
|
||||
return "用户信息";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,9 +2,12 @@
|
||||
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<!-- 基础信息 -->
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-dev</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0.2</version>
|
||||
|
||||
<!-- SpringBoot -->
|
||||
<parent>
|
||||
|
@ -0,0 +1,72 @@
|
||||
package cn.dev33.satoken.annotation;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
/**
|
||||
* 注解式鉴权 - 拦截器
|
||||
*/
|
||||
public class SaCheckInterceptor implements HandlerInterceptor {
|
||||
|
||||
// 每次请求之前触发
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws Exception {
|
||||
|
||||
// 获取处理method
|
||||
if (handler instanceof HandlerMethod == false) {
|
||||
return true;
|
||||
}
|
||||
HandlerMethod method = (HandlerMethod ) handler;
|
||||
|
||||
// 验证登录
|
||||
if(method.hasMethodAnnotation(SaCheckLogin.class) || method.getBeanType().isAnnotationPresent(SaCheckLogin.class)) {
|
||||
StpUtil.getLoginId();
|
||||
}
|
||||
|
||||
// 获取权限注解
|
||||
SaCheckPermission scp = method.getMethodAnnotation(SaCheckPermission.class);
|
||||
if(scp == null) {
|
||||
scp = method.getBeanType().getAnnotation(SaCheckPermission.class);
|
||||
}
|
||||
if(scp == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 开始验证权限
|
||||
Object[] codeArray = concatABC(scp.value(), scp.valueInt(), scp.valueLong());
|
||||
if(scp.isAnd()) {
|
||||
StpUtil.checkPermissionAnd(codeArray); // 必须全部都有
|
||||
} else {
|
||||
StpUtil.checkPermissionOr(codeArray); // 有一个就行了
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// 合并三个数组
|
||||
private Object[] concatABC(String[] a, int[] b, long[] c) {
|
||||
// 循环赋值
|
||||
Object[] d = new Object[a.length + b.length + c.length];
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
d[i] = a[i];
|
||||
}
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
d[a.length + i] = b[i];
|
||||
}
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
d[a.length + b.length + i] = c[i];
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package cn.dev33.satoken.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 标注一个路由方法,当前会话必须已登录才能通过
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
public @interface SaCheckLogin {
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package cn.dev33.satoken.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 标注一个路由方法,当前会话必须具有指定权限才可以通过
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD,ElementType.TYPE})
|
||||
public @interface SaCheckPermission {
|
||||
|
||||
/**
|
||||
* 权限码数组 ,String类型
|
||||
* @return .
|
||||
*/
|
||||
String [] value() default {};
|
||||
|
||||
/**
|
||||
* 权限码数组 ,int类型
|
||||
* @return .
|
||||
*/
|
||||
int [] valueInt() default {};
|
||||
|
||||
/**
|
||||
* 权限码数组 ,long类型
|
||||
* @return .
|
||||
*/
|
||||
long [] valueLong() default {};
|
||||
|
||||
/**
|
||||
* 是否属于and型验证 ,true=必须全部具有,false=只要具有一个就可以通过
|
||||
* @return .
|
||||
*/
|
||||
boolean isAnd() default true;
|
||||
|
||||
}
|
@ -19,6 +19,7 @@
|
||||
- 持久层扩展(集成redis)
|
||||
- 多账号认证体系(比如一个商城项目的user表和admin表)
|
||||
- 无cookie模式(APP、小程序等前后台分离场景)
|
||||
- 注解式鉴权(优雅的将鉴权与业务代码分离)
|
||||
- 零配置与Spring等框架集成
|
||||
- ...
|
||||
|
||||
|
@ -14,6 +14,7 @@
|
||||
- [无cookie模式](/use/not-cookie)
|
||||
- [模拟他人](/use/mock-person)
|
||||
- [多账号验证](/use/many-account)
|
||||
- [注解式鉴权](/use/at-check)
|
||||
- [框架配置](/use/config)
|
||||
|
||||
- **其它**
|
||||
|
@ -7,7 +7,7 @@
|
||||
<meta name="description" content="Description">
|
||||
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||
<meta name="keywords" content="sa-token|sa-token框架|sa-token文档|sa-token在线文档|权限认证框架">
|
||||
<meta name="description" content="sa-token是一个的JavaWeb权限认证框架,强大、简单、好用,登录验证、权限验证、自定义session会话、踢人下线、持久层扩展、无cookie模式、模拟他人账号、多账号体系、Spring集成...,零配置开箱即用,覆盖所有应用场景,你所需要的功能,这里都有">
|
||||
<meta name="description" content="sa-token是一个的JavaWeb权限认证框架,强大、简单、好用,登录验证、权限验证、自定义session会话、踢人下线、持久层扩展、无cookie模式、模拟他人账号、多账号体系、注解式鉴权、Spring集成...,零配置开箱即用,覆盖所有应用场景,你所需要的功能,这里都有">
|
||||
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css">
|
||||
<link rel="shortcut icon" type="image/x-icon" href="logo.png">
|
||||
<style type="text/css">
|
||||
|
@ -1,5 +1,9 @@
|
||||
# 更新日志
|
||||
|
||||
### 2020-3-7 @v1.0.2
|
||||
- 新增:新增注解式验证,可在路由方法中使用注解进行权限验证,[注解式验证]()
|
||||
- 参考:[注解式验证](use/at-check)
|
||||
|
||||
### 2020-2-12 @v1.0.1
|
||||
- 修复:修复`StpUtil.getLoginId(T default_value)`取值转换错误的bug
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
|
||||
## jar包下载
|
||||
[点击下载:sa-token-1.0.1.jar](https://color-test.oss-cn-qingdao.aliyuncs.com/sa-token/sa-token-1.0.1.jar)
|
||||
[点击下载:sa-token-1.0.2.jar](https://color-test.oss-cn-qingdao.aliyuncs.com/sa-token/sa-token-1.0.2.jar)
|
||||
|
||||
|
||||
## maven依赖
|
||||
@ -24,7 +24,7 @@
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.2</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.2</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
49
sa-token-doc/doc/use/at-check.md
Normal file
49
sa-token-doc/doc/use/at-check.md
Normal file
@ -0,0 +1,49 @@
|
||||
# 注解式鉴权
|
||||
---
|
||||
|
||||
- 尽管我们可以方便的一句代码完成权限验证,但是有时候我们仍希望可以将鉴权代码与我们的业务代码分离开来
|
||||
- 怎么做?
|
||||
- sa-token内置两个注解,帮助你使用注解完成鉴权操作
|
||||
|
||||
|
||||
## 1、注册拦截器
|
||||
- 为了不为项目带来不必要的性能负担,`sa-token`默认没有强制为项目注册全局拦截器
|
||||
- 因此,为了使用注解式鉴权功能,你必须手动将`sa-token`的全局拦截器注册到你项目中
|
||||
- 以`springboot2.0`为例, 新建配置类`MySaTokenConfig.java`
|
||||
|
||||
``` java
|
||||
@Configuration
|
||||
public class MySaTokenConfig extends WebMvcConfigurationSupport {
|
||||
// 注册sa-token的拦截器,打开注解式鉴权功能
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new SaCheckInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
}
|
||||
```
|
||||
- 保证此类被springboot启动类扫描到
|
||||
|
||||
## 2、使用注解
|
||||
|
||||
#### 登录验证
|
||||
|
||||
``` java
|
||||
@SaCheckLogin // 注解式鉴权:当前会话必须登录才能通过
|
||||
@RequestMapping("info")
|
||||
public String info() {
|
||||
return "查询用户信息";
|
||||
}
|
||||
```
|
||||
|
||||
#### 权限验证
|
||||
|
||||
``` java
|
||||
@SaCheckPermission("user-add") // 注解式鉴权:当前会话必须具有指定权限才能通过
|
||||
@RequestMapping("add")
|
||||
public String add() {
|
||||
return "用户增加";
|
||||
}
|
||||
```
|
||||
|
||||
#### 注意事项
|
||||
以上两个注解都可以加在类上,代表为这个类所有方法进行鉴权
|
@ -7,7 +7,7 @@
|
||||
<meta name="description" content="Description">
|
||||
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||
<meta name="keywords" content="sa-token|sa-token框架|sa-token文档|sa-token在线文档|权限认证框架">
|
||||
<meta name="description" content="sa-token是一个的JavaWeb权限认证框架,强大、简单、好用,登录验证、权限验证、自定义session会话、踢人下线、持久层扩展、无cookie模式、模拟他人账号、多账号体系、Spring集成...,零配置开箱即用,覆盖所有应用场景,你所需要的功能,这里都有">
|
||||
<meta name="description" content="sa-token是一个的JavaWeb权限认证框架,强大、简单、好用,登录验证、权限验证、自定义session会话、踢人下线、持久层扩展、无cookie模式、模拟他人账号、多账号体系、注解式鉴权、Spring集成...,零配置开箱即用,覆盖所有应用场景,你所需要的功能,这里都有">
|
||||
<link rel="stylesheet" href="https://unpkg.com/docsify/lib/themes/vue.css">
|
||||
<link rel="shortcut icon" type="image/x-icon" href="doc/logo.png">
|
||||
<link rel="stylesheet" href="index.css">
|
||||
@ -44,7 +44,7 @@
|
||||
<h1>sa-token<small>v1.0.1</small></h1>
|
||||
<div class="sub-title">一个的JavaWeb权限认证框架,强大、简单、好用</div>
|
||||
<!-- <p>0配置开箱即用,低学习成本</p> -->
|
||||
<p>登录验证、权限验证、自定义session会话、踢人下线、持久层扩展、无cookie模式、模拟他人账号、多账号体系、Spring集成...</p>
|
||||
<p>登录验证、权限验证、自定义session会话、踢人下线、持久层扩展、无cookie模式、模拟他人账号、多账号体系、注解式鉴权、Spring集成...</p>
|
||||
<p>零配置开箱即用,覆盖所有应用场景,你所需要的功能,这里都有</p>
|
||||
<div class="btn-box">
|
||||
<a href="https://github.com/click33/sa-token" target="_blank">GitHub</a>
|
||||
|
Loading…
Reference in New Issue
Block a user