自定义访问401返回值,解决页面401的错误提示
This commit is contained in:
parent
9a8feb9e58
commit
f830b702fc
|
|
@ -1,5 +1,6 @@
|
|||
package com.ruoyi.common.security.config;
|
||||
|
||||
import com.ruoyi.common.security.handler.AuthExceptionEntryPoint;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2ClientProperties;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
|
||||
|
|
@ -21,7 +22,7 @@ import org.springframework.web.client.RestTemplate;
|
|||
/**
|
||||
* oauth2 服务配置
|
||||
*
|
||||
* @author ruoyi
|
||||
* @author tanran
|
||||
*/
|
||||
@Configuration
|
||||
@EnableResourceServer
|
||||
|
|
@ -77,6 +78,6 @@ public class ResourceServerConfig extends ResourceServerConfigurerAdapter
|
|||
@Override
|
||||
public void configure(ResourceServerSecurityConfigurer resources)
|
||||
{
|
||||
resources.tokenServices(tokenServices());
|
||||
resources.tokenServices(tokenServices()).authenticationEntryPoint(new AuthExceptionEntryPoint());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package com.ruoyi.common.security.handler;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.common.core.constant.HttpStatus;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.utils.ServletUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 自定义访问401返回值
|
||||
*
|
||||
* @author tanran
|
||||
*/
|
||||
public class AuthExceptionEntryPoint implements AuthenticationEntryPoint {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(AuthExceptionEntryPoint.class);
|
||||
|
||||
@Override
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response,
|
||||
AuthenticationException authException){
|
||||
|
||||
logger.info("token已失效,跳转登录页面 {}", request.getRequestURI());
|
||||
|
||||
String msg = authException.getMessage();
|
||||
ServletUtils.renderString(response, JSON.toJSONString(R.fail(HttpStatus.UNAUTHORIZED, msg)));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue