您的位置:首页 > 文旅 > 美景 > 网业版qq_软件制作教程视频_五年级上册语文优化设计答案_长治seo

网业版qq_软件制作教程视频_五年级上册语文优化设计答案_长治seo

2025/1/9 2:05:46 来源:https://blog.csdn.net/weixin_52582672/article/details/144984749  浏览:    关键词:网业版qq_软件制作教程视频_五年级上册语文优化设计答案_长治seo
网业版qq_软件制作教程视频_五年级上册语文优化设计答案_长治seo
// 继承 AuthorizationServerConfigurerAdapter
// 重写下面的configure方法@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) {//配置端点endpoints.tokenStore(tokenStore).authenticationManager(authenticationManager).userDetailsService(userDetailsService).tokenGranter(tokenGranter).exceptionTranslator(new oAuth2ResponseExceptionTranslator());
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.DefaultThrowableAnalyzer;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.security.web.util.ThrowableAnalyzer;
import org.springframework.stereotype.Component;
import java.util.concurrent.ConcurrentHashMap;/*** 异常转换器* @author l*/
@Component
public class OAuth2ResponseExceptionTranslator implements WebResponseExceptionTranslator {private final ThrowableAnalyzer throwableAnalyzer = new DefaultThrowableAnalyzer();/*** 异常类型定义*/public static final String ERROR = "error";public static final String DESCRIPTION = "error_description";public static final String URI = "error_uri";public static final String INVALID_REQUEST = "invalid_request";public static final String INVALID_CLIENT = "invalid_client";public static final String INVALID_GRANT = "invalid_grant";public static final String UNAUTHORIZED_CLIENT = "unauthorized_client";public static final String UNSUPPORTED_GRANT_TYPE = "unsupported_grant_type";public static final String INVALID_SCOPE = "invalid_scope";public static final String INSUFFICIENT_SCOPE = "insufficient_scope";public static final String INVALID_TOKEN = "invalid_token";public static final String REDIRECT_URI_MISMATCH = "redirect_uri_mismatch";public static final String UNSUPPORTED_RESPONSE_TYPE = "unsupported_response_type";public static final String ACCESS_DENIED = "access_denied";/*** 其他异常*/public static final String METHOD_NOT_ALLOWED = "method_not_allowed";public static final String SERVER_ERROR = "server_error";public static final String UNAUTHORIZED = "unauthorized";private static final ConcurrentHashMap<String, String> OAUTH2_ERROR_MAP = new ConcurrentHashMap<>();/** 映射了部分*/static {OAUTH2_ERROR_MAP.put(INVALID_CLIENT, "无效的客户端");OAUTH2_ERROR_MAP.put(INVALID_GRANT, "登录密码错误");OAUTH2_ERROR_MAP.put(INVALID_SCOPE, "权限不足");OAUTH2_ERROR_MAP.put(UNSUPPORTED_GRANT_TYPE, "不支持的授权模式类型");OAUTH2_ERROR_MAP.put(ACCESS_DENIED, "验证码不正确");OAUTH2_ERROR_MAP.put(METHOD_NOT_ALLOWED, "方法不允许访问");OAUTH2_ERROR_MAP.put(SERVER_ERROR, "服务器内部异常");OAUTH2_ERROR_MAP.put(UNAUTHORIZED, "未授权");}@Overridepublic ResponseEntity<OAuth2Exception> translate(Exception e) {// 尝试从异常堆栈中提取 SpringSecurityException。Throwable[] causeChain = throwableAnalyzer.determineCauseChain(e);// 异常栈获取 OAuth2Exception 异常OAuth2Exception ase = (OAuth2Exception) throwableAnalyzer.getFirstThrowableOfType(OAuth2Exception.class, causeChain);if (ase != null) {return handleOAuth2Exception(ase);}System.out.println(e+"==========================");// 返回封装后的错误响应return new ResponseEntity<>(new OAuth2Exception(e.getMessage(),e),HttpStatus.BAD_REQUEST);}private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) {int status = e.getHttpErrorCode();HttpHeaders headers = new HttpHeaders();headers.set("Cache-Control", "no-store");headers.set("Pragma", "no-cache");// 如果是未授权或权限不足,添加 WWW-Authenticate 头部if (status == HttpStatus.UNAUTHORIZED.value() || (e instanceof InsufficientScopeException)) {headers.set("WWW-Authenticate", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, e.getSummary()));}String oAuth2ErrorMessage = OAUTH2_ERROR_MAP.getOrDefault(e.getOAuth2ErrorCode(),"未知异常:" + e.getOAuth2ErrorCode());// 构造响应体OAuth2Exception responseBody = new OAuth2Exception(oAuth2ErrorMessage,e);// 返回 ResponseEntityreturn new ResponseEntity<>(responseBody, headers, HttpStatus.valueOf(status));}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com