Skip to content

Is there any way to modify the response content after JWT authentication fails? #5202

@bluven

Description

@bluven

目前go-zero自带的jwt校验失败后,返回的响应体是空的,我翻遍了官方文档,没看到定制响应体内容的方法,又看了一下源码,发现是可以调用callback的:

func (ng *engine) appendAuthHandler(fr featuredRoutes, chn chain.Chain,

func (ng *engine) appendAuthHandler(fr featuredRoutes, chn chain.Chain,
	verifier func(chain.Chain) chain.Chain) chain.Chain {
	if fr.jwt.enabled {
		if len(fr.jwt.prevSecret) == 0 {
			chn = chn.Append(handler.Authorize(fr.jwt.secret,
				handler.WithUnauthorizedCallback(ng.unauthorizedCallback)))
		} else {
			chn = chn.Append(handler.Authorize(fr.jwt.secret,
				handler.WithPrevSecret(fr.jwt.prevSecret),
				handler.WithUnauthorizedCallback(ng.unauthorizedCallback)))
		}
	}

	return verifier(chn)

// WithUnauthorizedCallback returns a RunOption that with given unauthorized callback set.

// WithUnauthorizedCallback returns a RunOption that with given unauthorized callback set.
func WithUnauthorizedCallback(callback handler.UnauthorizedCallback) RunOption {
	return func(svr *Server) {
		svr.ngin.setUnauthorizedCallback(callback)
	}
}

但是配置UnauthorizedCallback的位置应该是在生成的routes.go文件里,这个文件应该不能修改。

所以,我想问问,怎么做才能生成定制的响应体?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions