mirror of
https://gitee.com/dromara/sa-token.git
synced 2026-02-27 16:50:24 +08:00
⚡ 重构为maven多模块架构
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package cn.dev33.satoken.autowired;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* 将此注解加到springboot启动类上,即可完成sa-token与springboot的集成
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
@Documented
|
||||
@Target({java.lang.annotation.ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Configuration
|
||||
@Import({SaTokenSpringAutowired.class})
|
||||
public @interface SaTokenSetup {
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package cn.dev33.satoken.interceptor;
|
||||
|
||||
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.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
/**
|
||||
* 注解式鉴权 - 拦截器
|
||||
* @author kong
|
||||
*/
|
||||
public class SaCheckInterceptor implements HandlerInterceptor {
|
||||
|
||||
|
||||
/**
|
||||
* 底层的 StpLogic 对象
|
||||
*/
|
||||
public StpLogic stpLogic = null;
|
||||
|
||||
/**
|
||||
* 创建,并指定一个默认的 StpLogic
|
||||
*/
|
||||
public SaCheckInterceptor() {
|
||||
this(StpUtil.stpLogic);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建,并指定一个的 StpLogic
|
||||
* @param stpLogic 指定的StpLogic
|
||||
*/
|
||||
public SaCheckInterceptor(StpLogic stpLogic) {
|
||||
this.stpLogic = stpLogic;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 每次请求之前触发
|
||||
*/
|
||||
@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)) {
|
||||
stpLogic.checkLogin();
|
||||
}
|
||||
|
||||
// 获取权限注解
|
||||
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()) {
|
||||
stpLogic.checkPermissionAnd(codeArray); // 必须全部都有
|
||||
} else {
|
||||
stpLogic.checkPermissionOr(codeArray); // 有一个就行了
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 合并三个数组
|
||||
* @param a .
|
||||
* @param b .
|
||||
* @param c .
|
||||
* @return .
|
||||
*/
|
||||
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,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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cn.dev33.satoken.spring;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
/**
|
||||
* SpringMVC相关操作
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public class SpringMVCUtil {
|
||||
|
||||
/**
|
||||
* 获取当前会话的 request
|
||||
* @return .
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();// 大善人SpringMVC提供的封装
|
||||
if(servletRequestAttributes == null) {
|
||||
throw new RuntimeException("当前环境非JavaWeb");
|
||||
}
|
||||
return servletRequestAttributes.getRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前会话的 response
|
||||
* @return .
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();// 大善人SpringMVC提供的封装
|
||||
if(servletRequestAttributes == null) {
|
||||
throw new RuntimeException("当前环境非JavaWeb");
|
||||
}
|
||||
return servletRequestAttributes.getResponse();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user