已经获取到access_token,携带token访问资源时返回Cannot convert access token to JSON
这是什么问题。。。有没有人碰到过,后台也没提示报错之类的
重新生成RSA加密公钥私钥就好了~
参考解决方案:
1、在网关下面建一个类,用于拦截验证token是否过期或者非法
package com.immo.ymhis.gateway.exception;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
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.HashMap;
import java.util.Map;
/**
自定义Token异常信息,用于tokan校验失败返回信息,比如token过期/验证错误
@author Jonathan.WQ
@date 2021年09月01日
/
public class AuthExceptionEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws ServletException {
Map map = new HashMap();
map.put("code", "401");
map.put("message", "token非法");
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
try {
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(response.getOutputStream(), map);
} catch (Exception e) {
throw new ServletException();
}
}
}
2、然后在配置类上加上这个异常
package com.immo.ymhis.gateway.config;
import com.immo.ymhis.gateway.exception.AuthExceptionEntryPoint;
import com.immo.ymhis.oauth2.common.config.DefaultResourceServerConf;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends DefaultResourceServerConf {
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId("ymhis");
resources.authenticationEntryPoint(new AuthExceptionEntryPoint());
}
}
3、效果
是设置.sercet() 这里吗?