自定义的servlet怎么放到shiro中管理

zh_yi 2018-09-11 03:32:39
各位大神:
最近项目中需要用到uflo工作流管理。搭建完环境之后想与项目本身的权限进行集成。项目用的是shiro管理的权限。可是启动了uflo要求的servlet之后,进入某个url总是没有权限。后台取不到token。哪位帮忙指点一下?
@Bean("shiroFilter")
public ShiroFilterFactoryBean shirFilter(org.apache.shiro.mgt.SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
shiroFilter.setSecurityManager(securityManager);

//oauth过滤
Map<String, Filter> filters = new HashMap<>();
filters.put("oauth2", new OAuth2Filter());
shiroFilter.setFilters(filters);

Map<String, String> filterMap = new LinkedHashMap<>();
filterMap.put("/**", "oauth2");
shiroFilter.setFilterChainDefinitionMap(filterMap);

return shiroFilter;
}

public class UfloServlet extends HttpServlet {
private static final long serialVersionUID = 4869260811353088997L;
public static final String URL="/uflo";
private Map<String, ServletHandler> handlerMap=new HashMap<String, ServletHandler>();
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
WebApplicationContext applicationContext=getWebApplicationContext(config);
Collection<ServletHandler> handlers=applicationContext.getBeansOfType(ServletHandler.class).values();
for(ServletHandler handler:handlers){
String url=handler.url();
if(handlerMap.containsKey(url)){
throw new RuntimeException("Handler ["+url+"] already exist.");
}
handlerMap.put(url, handler);
}
}

protected WebApplicationContext getWebApplicationContext(ServletConfig config){
return WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
}


@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try{
String path=req.getContextPath()+URL;
String uri=req.getRequestURI();
String targetUrl=uri.substring(path.length());
if(targetUrl.length()<1){
resp.sendRedirect(req.getContextPath()+"/uflo/todo");
return;
}
int slashPos=targetUrl.indexOf("/",1);
if(slashPos>-1){
targetUrl=targetUrl.substring(0,slashPos);
}
ServletHandler targetHandler=handlerMap.get(targetUrl);
if(targetHandler==null){
outContent(resp,"Handler ["+targetUrl+"] not exist.");
return;
}
targetHandler.execute(req, resp);
}catch(Exception ex){
Throwable e=getCause(ex);
resp.setCharacterEncoding("UTF-8");
PrintWriter pw=resp.getWriter();
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
String errorMsg = e.getMessage();
if(StringUtils.isBlank(errorMsg)){
errorMsg=e.getClass().getName();
}
pw.write(errorMsg);
pw.close();
throw new ServletException(ex);
}
}

private Throwable getCause(Throwable e){
if(e.getCause()!=null){
return getCause(e.getCause());
}
return e;
}

private void outContent(HttpServletResponse resp, String msg) throws IOException {
resp.setContentType("text/html");
PrintWriter pw=resp.getWriter();
pw.write("<html>");
pw.write("<header><title>Uflo Console</title></header>");
pw.write("<body>");
pw.write(msg);
pw.write("</body>");
pw.write("</html>");
pw.flush();
pw.close();
}
}
...全文
165 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zh_yi 2018-09-12
  • 打赏
  • 举报
回复
引用 1 楼 walkonthesky 的回复:
对/**增加过滤后,header有无附加token过来?把浏览器的header抓出来

Header中一定没有token的。现在就是想怎么把token放到header中呢
walkonthesky 2018-09-11
  • 打赏
  • 举报
回复
对/**增加过滤后,header有无附加token过来?把浏览器的header抓出来

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧