44 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package middleware
 | ||
| 
 | ||
| import (
 | ||
| 	"net/http"
 | ||
| 
 | ||
| 	"tyc-server/app/main/api/internal/config"
 | ||
| )
 | ||
| 
 | ||
| type AuthInterceptorMiddleware struct {
 | ||
| 	Config config.Config
 | ||
| }
 | ||
| 
 | ||
| func NewAuthInterceptorMiddleware(c config.Config) *AuthInterceptorMiddleware {
 | ||
| 	return &AuthInterceptorMiddleware{
 | ||
| 		Config: c,
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| func (m *AuthInterceptorMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
 | ||
| 	return func(w http.ResponseWriter, r *http.Request) {
 | ||
| 		// 从请求头中获取Authorization字段
 | ||
| 		// authHeader := r.Header.Get("Authorization")
 | ||
| 
 | ||
| 		// if authHeader != "" && strings.HasPrefix(authHeader, "Bearer ") {
 | ||
| 		// 	// 提取JWT令牌(去除Bearer前缀)
 | ||
| 		// 	tokenString := strings.TrimPrefix(authHeader, "Bearer ")
 | ||
| 
 | ||
| 		// 	// 使用jwtx.ParseJwtTokenV2()解析令牌
 | ||
| 		// 	claims, err := jwtx.ParseJwtTokenV2(tokenString, m.Config.AdminConfig.AccessSecret)
 | ||
| 		// 	if err != nil {
 | ||
| 
 | ||
| 		// 		resultErr := errors.Wrapf(xerr.NewErrMsg("用户名或密码错误"), "用户登录, 用户名或密码错误, 用户名: %s")
 | ||
| 		// 		result.HttpResult(r, w, nil, resultErr)
 | ||
| 		// 		return
 | ||
| 		// 	}
 | ||
| 		// 	// 如果解析失败,可以添加处理代码,如返回401错误
 | ||
| 		// }
 | ||
| 
 | ||
| 		// 如果没有Authorization头或解析失败,继续传递到下一个处理器
 | ||
| 		// 根据业务需求可以决定是否继续处理请求
 | ||
| 		next(w, r)
 | ||
| 	}
 | ||
| }
 |