mirror of
https://gitee.com/dromara/sa-token.git
synced 2026-02-27 16:50:24 +08:00
完善源码注释
This commit is contained in:
@@ -16,102 +16,108 @@ import cn.dev33.satoken.spring.SaTokenServletSpringImpl;
|
||||
import cn.dev33.satoken.stp.StpInterface;
|
||||
|
||||
/**
|
||||
* 利用spring的自动装配来加载开发者重写的Bean
|
||||
* 利用spring的自动装配来加载开发者重写的Bean
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class SaTokenSpringAutowired {
|
||||
|
||||
|
||||
/**
|
||||
* 获取配置Bean
|
||||
* @return 配置对象
|
||||
* 获取配置Bean
|
||||
*
|
||||
* @return 配置对象
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix="spring.sa-token")
|
||||
@ConfigurationProperties(prefix = "spring.sa-token")
|
||||
public SaTokenConfig getSaTokenConfig() {
|
||||
return new SaTokenConfig();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 注入配置Bean
|
||||
* 注入配置Bean
|
||||
*
|
||||
* @param saTokenConfig 配置对象
|
||||
*/
|
||||
@Autowired
|
||||
public void setConfig(SaTokenConfig saTokenConfig){
|
||||
public void setConfig(SaTokenConfig saTokenConfig) {
|
||||
SaTokenManager.setConfig(saTokenConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入持久化Bean
|
||||
* @param saTokenDao .
|
||||
* 注入持久化Bean
|
||||
*
|
||||
* @param saTokenDao SaTokenDao对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTokenDao(SaTokenDao saTokenDao){
|
||||
public void setSaTokenDao(SaTokenDao saTokenDao) {
|
||||
SaTokenManager.setSaTokenDao(saTokenDao);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入权限认证Bean
|
||||
* @param stpInterface .
|
||||
* 注入权限认证Bean
|
||||
*
|
||||
* @param stpInterface StpInterface对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setStpInterface(StpInterface stpInterface){
|
||||
public void setStpInterface(StpInterface stpInterface) {
|
||||
SaTokenManager.setStpInterface(stpInterface);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入Cookie操作Bean
|
||||
* @param saTokenCookie .
|
||||
* 注入Cookie操作Bean
|
||||
*
|
||||
* @param saTokenCookie SaTokenCookie对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTokenCookie(SaTokenCookie saTokenCookie){
|
||||
public void setSaTokenCookie(SaTokenCookie saTokenCookie) {
|
||||
SaTokenManager.setSaTokenCookie(saTokenCookie);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入框架行为Bean
|
||||
* @param saTokenAction .
|
||||
* 注入框架行为Bean
|
||||
*
|
||||
* @param saTokenAction SaTokenAction对象
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTokenAction(SaTokenAction saTokenAction){
|
||||
public void setSaTokenAction(SaTokenAction saTokenAction) {
|
||||
SaTokenManager.setSaTokenAction(saTokenAction);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Servlet操作Bean (Spring版)
|
||||
* @return Servlet操作Bean (Spring版)
|
||||
* 获取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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 路由匹配器
|
||||
* 注入Servlet操作Bean
|
||||
*
|
||||
* @param saTokenServlet SaTokenServlet对象
|
||||
*/
|
||||
@Autowired
|
||||
public void setSaTokenServlet(SaTokenServlet saTokenServlet) {
|
||||
SaTokenManager.setSaTokenServlet(saTokenServlet);
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由匹配器
|
||||
*/
|
||||
public static PathMatcher pathMatcher;
|
||||
|
||||
/**
|
||||
* 利用自动匹配特性,获取SpringMVC框架内部使用的路由匹配器
|
||||
* 利用自动匹配特性,获取SpringMVC框架内部使用的路由匹配器
|
||||
*
|
||||
* @param pathMatcher 要设置的 pathMatcher
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public static void setPathMatcher(PathMatcher pathMatcher) {
|
||||
SaTokenSpringAutowired.pathMatcher = pathMatcher;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -12,29 +12,29 @@ import cn.dev33.satoken.stp.StpLogic;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
/**
|
||||
* 注解式鉴权 - 拦截器
|
||||
* 注解式鉴权 - 拦截器
|
||||
*
|
||||
* @author kong
|
||||
*/
|
||||
public class SaAnnotationInterceptor implements HandlerInterceptor {
|
||||
|
||||
|
||||
/**
|
||||
* 底层的 StpLogic 对象
|
||||
* 在进行注解鉴权时使用的 StpLogic 对象
|
||||
*/
|
||||
public StpLogic stpLogic = null;
|
||||
|
||||
|
||||
/**
|
||||
* @return 底层的 StpLogic 对象
|
||||
* @return 在进行注解鉴权时使用的 StpLogic 对象
|
||||
*/
|
||||
public StpLogic getStpLogic() {
|
||||
if(stpLogic == null) {
|
||||
if (stpLogic == null) {
|
||||
stpLogic = StpUtil.stpLogic;
|
||||
}
|
||||
return stpLogic;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param stpLogic 底层的 StpLogic 对象
|
||||
* @param stpLogic 在进行注解鉴权时使用的 StpLogic 对象
|
||||
* @return 拦截器自身
|
||||
*/
|
||||
public SaAnnotationInterceptor setStpLogic(StpLogic stpLogic) {
|
||||
@@ -42,33 +42,30 @@ public class SaAnnotationInterceptor implements HandlerInterceptor {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建,并指定一个默认的 StpLogic
|
||||
* 构建: 注解式鉴权 - 拦截器
|
||||
*/
|
||||
public SaAnnotationInterceptor() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 每次请求之前触发的方法
|
||||
* 每次请求之前触发的方法
|
||||
*/
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws Exception {
|
||||
|
||||
|
||||
// 获取处理method
|
||||
if (handler instanceof HandlerMethod == false) {
|
||||
return true;
|
||||
}
|
||||
Method method = ((HandlerMethod) handler).getMethod();
|
||||
|
||||
// 进行验证
|
||||
|
||||
// 进行验证
|
||||
getStpLogic().checkMethodAnnotation(method);
|
||||
|
||||
// 通过验证
|
||||
|
||||
// 通过验证
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,16 +5,18 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 执行验证方法的辅助类
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public interface SaRouteFunction {
|
||||
|
||||
/**
|
||||
* 执行验证的方法
|
||||
* @param request request对象
|
||||
* @param response response对象
|
||||
* @param handler 处理对象
|
||||
* 执行验证的方法
|
||||
*
|
||||
* @param request request对象
|
||||
* @param response response对象
|
||||
* @param handler 处理对象
|
||||
*/
|
||||
public void run(HttpServletRequest request, HttpServletResponse response, Object handler);
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ import cn.dev33.satoken.fun.SaFunction;
|
||||
public class SaRouterUtil {
|
||||
|
||||
/**
|
||||
* 在进行路由匹配时所使用的的 PathMatcher 对象
|
||||
* 在进行路由匹配时所使用的 PathMatcher 对象
|
||||
*/
|
||||
private static PathMatcher pathMatcher;
|
||||
|
||||
|
||||
@@ -6,14 +6,15 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import cn.dev33.satoken.servlet.SaTokenServlet;
|
||||
|
||||
/**
|
||||
* sa-token 对cookie的相关操作 接口实现类
|
||||
* sa-token 对cookie的相关操作 接口实现类
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public class SaTokenServletSpringImpl implements SaTokenServlet {
|
||||
|
||||
/**
|
||||
* 获取当前请求的Request对象
|
||||
* 获取当前请求的Request对象
|
||||
*/
|
||||
@Override
|
||||
public HttpServletRequest getRequest() {
|
||||
@@ -21,11 +22,11 @@ public class SaTokenServletSpringImpl implements SaTokenServlet {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前请求的Response对象
|
||||
* 获取当前请求的Response对象
|
||||
*/
|
||||
@Override
|
||||
public HttpServletResponse getResponse() {
|
||||
return SpringMVCUtil.getResponse();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import cn.dev33.satoken.exception.SaTokenException;
|
||||
|
||||
/**
|
||||
* SpringMVC相关操作
|
||||
* @author kong
|
||||
@@ -18,9 +20,9 @@ public class SpringMVCUtil {
|
||||
* @return request
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();// 大善人SpringMVC提供的封装
|
||||
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
if(servletRequestAttributes == null) {
|
||||
throw new RuntimeException("当前环境非JavaWeb");
|
||||
throw new SaTokenException("非Web上下文无法获取Request");
|
||||
}
|
||||
return servletRequestAttributes.getRequest();
|
||||
}
|
||||
@@ -30,9 +32,9 @@ public class SpringMVCUtil {
|
||||
* @return response
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();// 大善人SpringMVC提供的封装
|
||||
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
if(servletRequestAttributes == null) {
|
||||
throw new RuntimeException("当前环境非JavaWeb");
|
||||
throw new SaTokenException("非Web上下文无法获取Request");
|
||||
}
|
||||
return servletRequestAttributes.getResponse();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user