mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-10 21:19:00 +08:00
🐛 Fixing a bug. oauth2 异常被 全局拦截处理
This commit is contained in:
1 parent
cea0cafa70
commit
f8a9c21901
3 files changed
+18
-80
No files matched your search
-4
@@ -41,8 +41,6 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
|
||||
@Autowired
|
||||
protected ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;
|
||||
@Autowired
|
||||
protected PigAccessDeniedHandler pigAccessDeniedHandler;
|
||||
@Autowired
|
||||
protected RemoteTokenServices remoteTokenServices;
|
||||
@Autowired
|
||||
protected UserDetailsService userDetailsService;
|
||||
@@ -100,7 +98,6 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
|
||||
remoteTokenServices.setRestTemplate(lbRestTemplate);
|
||||
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
|
||||
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
|
||||
.accessDeniedHandler(pigAccessDeniedHandler)
|
||||
.tokenServices(remoteTokenServices);
|
||||
}
|
||||
|
||||
@@ -119,7 +116,6 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
|
||||
remoteTokenServices.setRestTemplate(lbRestTemplate);
|
||||
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
|
||||
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
|
||||
.accessDeniedHandler(pigAccessDeniedHandler)
|
||||
.tokenServices(remoteTokenServices);
|
||||
}
|
||||
|
||||
|
||||
+18
-2
@@ -15,11 +15,13 @@
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.core.exception;
|
||||
package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
@@ -36,7 +38,9 @@ import java.util.List;
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
@AllArgsConstructor
|
||||
public class GlobalExceptionHandlerResolver {
|
||||
|
||||
/**
|
||||
* 全局异常.
|
||||
*
|
||||
@@ -50,6 +54,18 @@ public class GlobalExceptionHandler {
|
||||
return new R<>(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* AccessDeniedException
|
||||
*
|
||||
* @param e the e
|
||||
* @return R
|
||||
*/
|
||||
@ExceptionHandler(AccessDeniedException.class)
|
||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||
public R exception(AccessDeniedException e) {
|
||||
return new R<>(e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* validation Exception
|
||||
*
|
||||
-74
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/6/22
|
||||
*/
|
||||
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
|
||||
import com.pig4cloud.pigx.common.core.exception.PigDeniedException;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* 授权拒绝处理器,覆盖默认的OAuth2AccessDeniedHandler
|
||||
* 包装失败信息到PigDeniedException
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class PigAccessDeniedHandler extends OAuth2AccessDeniedHandler {
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* 授权拒绝处理,使用R包装
|
||||
*
|
||||
* @param request request
|
||||
* @param response response
|
||||
* @param authException authException
|
||||
* @throws IOException IOException
|
||||
* @throws ServletException ServletException
|
||||
*/
|
||||
@Override
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException authException) throws IOException, ServletException {
|
||||
log.info("授权失败,禁止访问 {}", request.getRequestURI());
|
||||
response.setCharacterEncoding(CommonConstant.UTF8);
|
||||
response.setContentType(CommonConstant.CONTENT_TYPE);
|
||||
R<String> result = new R<>(new PigDeniedException("授权失败,禁止访问"));
|
||||
response.setStatus(HttpStatus.HTTP_FORBIDDEN);
|
||||
PrintWriter printWriter = response.getWriter();
|
||||
printWriter.append(objectMapper.writeValueAsString(result));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user