自定义的servlet怎么放到shiro中管理
各位大神:
最近项目中需要用到uflo工作流管理。搭建完环境之后想与项目本身的权限进行集成。项目用的是shiro管理的权限。可是启动了uflo要求的servlet之后,进入某个url总是没有权限。后台取不到token。哪位帮忙指点一下?
最近项目中需要用到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();
}
}
...全文
相关推荐
servlet-shiro:基于MVC分层的小项目
servlet-shiro 基于MVC分层的小项目
Spring配置shiro时自定义Realm中属性无法使用注解注入的解决办法
今天小编就为大家分享一篇关于Spring配置shiro时自定义Realm中属性无法使用注解注入的解决办法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
shiro web中自定义Realm
这里是shiro在web应用中自定义Realm的代码,以便做一个参考
shiro_tool.zip
https://blog.csdn.net/xixiyuguang/article/details/109744942
Apache Shiro教程
Apache Shiro 是一个框架,可用于身份验证和授权。Apache Shiro的教程(PDF),相关jar包,源码等。


Web 开发
