JSD-8709 jwt单点
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.6 KiB

package com.fr.plugin.rcsso.filter;
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
import com.fr.plugin.context.PluginContexts;
import com.fr.plugin.rcsso.config.PluginSimpleConfig;
import com.fr.plugin.rcsso.utils.*;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.record.analyzer.EnableMetrics;
import com.fr.stable.fun.Authorize;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
@EnableMetrics
@FunctionRecorder
@Authorize(callSignKey = "com.fr.plugin.rcsso")
public class SSOFilter extends AbstractGlobalRequestFilterProvider {
@Override
public String filterName() {
return "rcssoFilter";
}
@Override
public String[] urlPatterns() {
return new String[]{"/decision/*"};
}
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain ){
if(PluginContexts.currentContext().isAvailable()) {
String tokenStr = PluginSimpleConfig.getInstance().getTokenStr();
String token = req.getParameter(tokenStr);
FRUtils.FRLogInfo("tokenStr:"+tokenStr + ";token:"+token);
if(Utils.isNullStr(token) || FRUtils.isLogin(req)){
release(req,res,chain);
return ;
}
String privateKey = PluginSimpleConfig.getInstance().getPkey();
String userName = "";
try {
if(Utils.isMobile(req)){
FRUtils.FRLogInfo("mobile");
userName = JwtUtil.getUsername(token);
}else{
FRUtils.FRLogInfo("pc");
userName = RSAUtil.decrypt(token,privateKey);
}
FRUtils.FRLogInfo("username "+userName);
} catch (Exception e) {
FRUtils.FRLogError("getUserName exception:"+e.getMessage());
ResponseUtils.failedResponse(res,"解密用户名异常");
}
String url = FRUtils.getAllUrl(req);
url = url.substring(0,url.indexOf(url.contains("?token") ? "?token" : "&token"));
url = url.replace("http","https");
//登录
FRUtils.login(req,res,userName,url);
}
release(req,res,chain);
}
//放行拦截器
private void release(HttpServletRequest req, HttpServletResponse res, FilterChain chain) {
try{
chain.doFilter(req,res);
}catch (Exception e){
FRUtils.FRLogInfo("拦截失败");
}
}
}