使用Sa-Token开源架构快速实现Http Basic 认证,如上图
1、springboot环境下直接添加starter即可
<!-- Sa-Token 权限认证,在线文档:https://sa-token.cc -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.39.0</version>
</dependency>
2、非sping环境下,需要自定义 SaTokenContext 实现
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-servlet</artifactId>
<version>1.39.0</version>
</dependency>
自定义 SaTokenContext 实现
package org.itdgz.satoken;
import cn.dev33.satoken.context.SaTokenContext;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.context.model.SaResponse;
import cn.dev33.satoken.context.model.SaStorage;
import cn.dev33.satoken.servlet.model.SaRequestForServlet;
import cn.dev33.satoken.servlet.model.SaResponseForServlet;
import cn.dev33.satoken.servlet.model.SaStorageForServlet;
public class SaTokenContextForNoSpring implements SaTokenContext {
@Override
public SaRequest getRequest() {
return new SaRequestForServlet(SpringMVCUtil.getRequest());
}
@Override
public SaResponse getResponse() {
return new SaResponseForServlet(SpringMVCUtil.getResponse());
}
@Override
public SaStorage getStorage() {
return new SaStorageForServlet(SpringMVCUtil.getRequest());
}
@Override
public boolean matchPath(String pattern, String path) {
return true;
}
}
SpringMVCUtil.java
package org.itdgz.satoken;
import cn.dev33.satoken.exception.NotWebContextException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SpringMVCUtil {
private SpringMVCUtil() {
}
/**
* 获取当前会话的 request 对象
* @return request
*/
public static HttpServletRequest getRequest() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(servletRequestAttributes == null) {
throw new NotWebContextException("非 web 上下文无法获取 HttpServletRequest").setCode(500);
}
return servletRequestAttributes.getRequest();
}
/**
* 获取当前会话的 response 对象
* @return response
*/
public static HttpServletResponse getResponse() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(servletRequestAttributes == null) {
throw new NotWebContextException("非 web 上下文无法获取 HttpServletResponse").setCode(500);
}
return servletRequestAttributes.getResponse();
}
/**
* 判断当前是否处于 Web 上下文中
* @return /
*/
public static boolean isWeb() {
return RequestContextHolder.getRequestAttributes() != null;
}
}
启动初始化时,将自定义的上下文注册
SaTokenContext saTokenContext = new SaTokenContextForNoSpring();
SaManager.setSaTokenContext(saTokenContext);
Controller接口
@RequestMapping(value = "/base", method = RequestMethod.GET)
@ResponseBody
public SaResult basic(){
try {
SaHttpBasicUtil.check("sa:123456");
}catch (Exception e){
e.printStackTrace();
return SaResult.error(e.getMessage());
}
return SaResult.ok("Http Basic 认证");
}
学习地址:https://gitee.com/dromara/sa-token