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.
122 lines
3.5 KiB
122 lines
3.5 KiB
2 years ago
|
package com.eco.plugin.xx.zgrsauth.filter;
|
||
|
|
||
|
import com.eco.plugin.xx.zgrsauth.config.PluginSimpleConfig;
|
||
|
import com.eco.plugin.xx.zgrsauth.utils.*;
|
||
|
import com.fr.decision.fun.GlobalRequestFilterProvider;
|
||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
|
||
|
import com.fr.plugin.context.PluginContexts;
|
||
|
import com.fr.record.analyzer.EnableMetrics;
|
||
|
import com.fr.stable.fun.Authorize;
|
||
|
import org.jetbrains.annotations.NotNull;
|
||
|
|
||
|
import javax.servlet.FilterChain;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.UnsupportedEncodingException;
|
||
|
import java.net.URLDecoder;
|
||
|
|
||
|
@EnableMetrics
|
||
|
@Authorize(callSignKey = "com.eco.plugin.xx.zgrsauth")
|
||
|
public class SSOFilter extends AbstractGlobalRequestFilterProvider {
|
||
|
@Override
|
||
|
public String filterName() {
|
||
|
return "zgrsFilter";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String[] urlPatterns() {
|
||
|
return new String[]{"/*"};
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain ){
|
||
|
|
||
|
if(PluginContexts.currentContext().isAvailable()){
|
||
|
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
|
||
|
FRUtils.FRLogInfo(FRUtils.getAllUrl(req));
|
||
|
//是否放行
|
||
|
boolean release = isRelease(req,psc);
|
||
|
|
||
|
if(release){
|
||
|
FRUtils.FRLogInfo("无参数");
|
||
|
release(req,res,chain);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
//不是模板,放行
|
||
|
String viewlet = req.getParameter("viewlet");
|
||
|
|
||
|
if(Utils.isNullStr(viewlet)){
|
||
|
FRUtils.FRLogInfo("非模板");
|
||
|
release(req,res,chain);
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
viewlet = URLDecoder.decode(viewlet,"utf-8");
|
||
|
} catch (UnsupportedEncodingException e) {
|
||
|
|
||
|
}
|
||
|
FRUtils.FRLogInfo("viewlet:"+viewlet);
|
||
|
|
||
|
|
||
|
String username = "";
|
||
|
try {
|
||
|
username = FRUserUtils.getCurrentUser(req).getUserName();
|
||
|
} catch (Exception e) {
|
||
|
FRUtils.FRLogInfo("获取当前用户失败!");
|
||
|
ResponseUtils.responseText(res,"您无权限访问");
|
||
|
return ;
|
||
|
}
|
||
|
boolean hasAuth = AuthUtils.hasAuth(username,viewlet,psc);
|
||
|
|
||
|
if(!hasAuth){
|
||
|
ResponseUtils.responseText(res,"您无权限访问");
|
||
|
return ;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
release(req,res,chain);
|
||
|
}
|
||
|
|
||
|
private boolean isRelease(HttpServletRequest req,PluginSimpleConfig psc) {
|
||
|
String flag = psc.getFlag();
|
||
|
String flagValue = req.getParameter(flag);
|
||
|
|
||
|
//没有约定参数,放行
|
||
|
if(Utils.isNullStr(flagValue)){
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
//放行拦截器
|
||
|
private void release(HttpServletRequest req, HttpServletResponse res, FilterChain chain) {
|
||
|
try{
|
||
|
chain.doFilter(req,res);
|
||
|
}catch (Exception e){
|
||
|
FRUtils.FRLogInfo("拦截失败");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 可选实现的多个filter排序(执行顺序)的方法
|
||
|
* @param other
|
||
|
* @return 0 相等,大于0是自身优先 小于0 是other优先
|
||
|
*/
|
||
|
@Override
|
||
|
public int compareTo(@NotNull GlobalRequestFilterProvider other ){
|
||
|
|
||
|
//如果是单点拦截器,则单点拦截器优先
|
||
|
if(other.filterName().equals("global")){
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return super.compareTo(other);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|