Shiro控制没有登录时,返回指定JSON格式数据
Shiro框架在未登录时,需要返回JSON数据,需要新增ShiroUserFilter文件。
ShiroUserFilter过滤文件
import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.apache.shiro.web.filter.authc.UserFilter; import com.alibaba.fastjson.JSON; public class ShiroUserFilter extends UserFilter { @Override protected void redirectToLogin(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException { servletResponse.setContentType("application/json; charset=utf-8"); Map<String, Object> response = new HashMap<String, Object>(); response.put("code", 401); response.put("msg", "未登录或登录超时。请重新登录"); String json = JSON.toJSONString(response); servletResponse.getWriter().write(json); } }
ShiroConfig文件的ShiroFilterFactoryBean方法
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) { ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean(); shiroFilter.setSecurityManager(securityManager); Map<String, Filter> filters=new HashMap<>(); filters.put("authc", new ShiroUserFilter()); shiroFilter.setFilters(filters); shiroFilter.setUnauthorizedUrl("/"); Map<String, String> filterMap = new LinkedHashMap<>(); filterMap.put("/**", "authc"); shiroFilter.setFilterChainDefinitionMap(filterMap); return shiroFilter; }
正在加载评论...
相关文章
- 配置@ConfigurationProperties后读取yml文件的参数...
- Caused by: java.lang.NoClassDefFoundError: Coul...
- springboot加入SSL证书设置https访问
- Springboot用pagehelper分页插件查询Oracle的速度...
- shiro的登录账号密码校验过程,SimpleAuthenticati...
- Shiro控制没有登录时,返回指定JSON格式数据
- SpringBoot中mybatis返回的Date时间格式化
- ClassNotFoundException: org.aspectj.lang.annota...
- org.apache.shiro.UnavailableSecurityManagerExce...
栏目列表
推荐阅读
- java的StringEscapeUtils转义与反转义
- Mybatis的foreach事列,如何获取foreach的index值
- Java Ehcache缓存的timeToIdleSeconds和timeToLiveSeconds区别
- errcode: 40001, errmsg: invalid credential, access_token is invalid or not latest rid: 5f6981c2-6cc9cb20-6833977a
- 服务器侦听端口 netstat -na 命令详解
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- tomcat启动maven工程报错-Updating status for Tomcat v7.0 Server at localhost...
- Mybatis异常There is no getter for property named 'XXX' in 'class java.lang.String'
- Java后台获取当前访问客户端的真实IP地址(外网或内网地址)
- com.googlecode.aviator.AviatorEvaluator规则引擎用法
- java发送http的get、post请求
- StringSubstitutor替换占位符,处理SQL参数
- java拦截器HandlerInterceptor的preHandle、postHandle与afterCompletion三个方法
- java中Session缓存和Cache缓存的区别
- Java8对象集合转List<Integer>id集合,转List<String>name集合,List<Class>对象集合
- Java导出excel利用jxls合并单元格jx:mergeCells实现表格嵌套技术
- java图片文件base64流之间的互转帮助类源码下载
- 利用ObjectMapper,将json数组转List<Map<String,Object>>
- java的DES加密解密辅助类
- Java Ecache缓存工具类源码下载