提交 71171a71 编写于 作者: tangyaolong's avatar tangyaolong

oauth2 Feign自动校验

上级 0dc3b762
<#ibiztemplate> <#ibiztemplate>
TARGET=PSSYSTEM TARGET=PSSYSTEM
</#ibiztemplate> </#ibiztemplate>
<#assign oauth2Enable=false>
<#if sysrun?? && sysrun.getPSDevSlnMSDepAPI()?? >
<#assign depSysApi=sysrun.getPSDevSlnMSDepAPI()>
<#if depSysApi.getPSDCMSPlatformNode()??>
<#assign depSysApiPlatformNode=depSysApi.getPSDCMSPlatformNode()>
<#assign depSysApiPlatform=depSysApi.getPSDCMSPlatform()>
<#if depSysApiPlatform.getUserParam("ibiz.oauth2.enable","")?? && depSysApiPlatform.getUserParam("ibiz.oauth2.enable","")!="">
<#assign oauth2Enable=true>
</#if>
</#if>
</#if>
package ${pub.getPKGCodeName()}.util.client; package ${pub.getPKGCodeName()}.util.client;
import ${pub.getPKGCodeName()}.util.security.AuthenticationUser; import ${pub.getPKGCodeName()}.util.security.AuthenticationUser;
...@@ -26,6 +37,12 @@ public class IBZUAAFallback implements IBZUAAFeignClient { ...@@ -26,6 +37,12 @@ public class IBZUAAFallback implements IBZUAAFeignClient {
return null; return null;
} }
<#if oauth2Enable>
@Override
public Object getToken(MultiValueMap<String, String> map){
return null;
}
</#if>
@Override @Override
public String getPublicKey() { public String getPublicKey() {
return null; return null;
......
<#ibiztemplate> <#ibiztemplate>
TARGET=PSSYSTEM TARGET=PSSYSTEM
</#ibiztemplate> </#ibiztemplate>
<#assign oauth2Enable=false>
<#if sysrun?? && sysrun.getPSDevSlnMSDepAPI()?? >
<#assign depSysApi=sysrun.getPSDevSlnMSDepAPI()>
<#if depSysApi.getPSDCMSPlatformNode()??>
<#assign depSysApiPlatformNode=depSysApi.getPSDCMSPlatformNode()>
<#assign depSysApiPlatform=depSysApi.getPSDCMSPlatform()>
<#if depSysApiPlatform.getUserParam("ibiz.oauth2.enable","")?? && depSysApiPlatform.getUserParam("ibiz.oauth2.enable","")!="">
<#assign oauth2Enable=true>
</#if>
</#if>
</#if>
package ${pub.getPKGCodeName()}.util.client; package ${pub.getPKGCodeName()}.util.client;
import ${pub.getPKGCodeName()}.util.security.AuthenticationUser; import ${pub.getPKGCodeName()}.util.security.AuthenticationUser;
...@@ -29,6 +40,15 @@ public interface IBZUAAFeignClient ...@@ -29,6 +40,15 @@ public interface IBZUAAFeignClient
@PostMapping(value = "/uaa/login") @PostMapping(value = "/uaa/login")
AuthenticationUser login(@RequestBody AuthorizationLogin authorizationLogin); AuthenticationUser login(@RequestBody AuthorizationLogin authorizationLogin);
<#if oauth2Enable>
/**
* oauth2认证
* @param 校验信息
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "/oauth/token")
Object getToken(@RequestBody MultiValueMap<String,String> map);
</#if>
@PostMapping(value = "/uaa/loginbyusername") @PostMapping(value = "/uaa/loginbyusername")
AuthenticationUser loginByUsername(@RequestBody String username); AuthenticationUser loginByUsername(@RequestBody String username);
......
<#ibiztemplate> <#ibiztemplate>
TARGET=PSSYSTEM TARGET=PSSYSTEM
</#ibiztemplate> </#ibiztemplate>
<#assign oauth2Enable=false>
<#if sysrun?? && sysrun.getPSDevSlnMSDepAPI()?? >
<#assign depSysApi=sysrun.getPSDevSlnMSDepAPI()>
<#if depSysApi.getPSDCMSPlatformNode()??>
<#assign depSysApiPlatformNode=depSysApi.getPSDCMSPlatformNode()>
<#assign depSysApiPlatform=depSysApi.getPSDCMSPlatform()>
<#if depSysApiPlatform.getUserParam("ibiz.oauth2.enable","")?? && depSysApiPlatform.getUserParam("ibiz.oauth2.enable","")!="">
<#assign oauth2Enable=true>
</#if>
</#if>
</#if>
package ${pub.getPKGCodeName()}.util.web; package ${pub.getPKGCodeName()}.util.web;
import feign.RequestInterceptor; import feign.RequestInterceptor;
import feign.RequestTemplate; import feign.RequestTemplate;
import net.ibizsys.sample.mng.util.client.IBZUAAFeignClient;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.LinkedHashMap;
/** /**
* feign请求拦截器 * feign请求拦截器
...@@ -22,23 +41,56 @@ public class FeignRequestInterceptor implements RequestInterceptor { ...@@ -22,23 +41,56 @@ public class FeignRequestInterceptor implements RequestInterceptor {
private final Logger logger = LoggerFactory.getLogger(getClass()); private final Logger logger = LoggerFactory.getLogger(getClass());
<#if oauth2Enable>
@Autowired
IBZUAAFeignClient ibzuaaFeignClient;
@Value("${r'${ibiz.oauth2.grantType:client_credentials}'}")
private String grantType;
@Value("${r'${ibiz.oauth2.clientId:test}'}")
private String clientId;
@Value("${r'${ibiz.oauth2.clientSecret:test}'}")
private String clientSecret;
@Value("${r'${ibiz.oauth2.header:accesstoken}'}")
private String oauth2Authorization;
</#if>
@Override @Override
public void apply(RequestTemplate requestTemplate) { public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(requestAttributes!=null){ <#if oauth2Enable>
if(requestTemplate.path().matches("/oauth/token")){
return;
}
</#if>
if (requestAttributes != null) {
HttpServletRequest request = requestAttributes.getRequest(); HttpServletRequest request = requestAttributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames(); Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) { if (headerNames != null) {
while (headerNames.hasMoreElements()) { while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement(); String name = headerNames.nextElement();
if(name.equalsIgnoreCase("transfer-encoding")){ if (name.equalsIgnoreCase("transfer-encoding")) {
continue; continue;
} }
String values = request.getHeader(name); String values = request.getHeader(name);
requestTemplate.header(name, values); requestTemplate.header(name, values);
} }
logger.info("feign interceptor header:{}",requestTemplate); <#if oauth2Enable>
if(ObjectUtils.isEmpty(request.getHeader(oauth2Authorization))){
MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<>();
postParameters.add("client_id", clientId);
postParameters.add("client_secret", clientSecret);
postParameters.add("grant_type", grantType);
LinkedHashMap<String,String> token = (LinkedHashMap<String, String>)ibzuaaFeignClient.getToken(postParameters);
if(!ObjectUtils.isEmpty(token.get("access_token"))){
requestTemplate.header(oauth2Authorization, "Bearer "+token.get("access_token"));
}
}
</#if>
logger.info("feign interceptor header:{}", requestTemplate);
} }
} }
} }
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册