shiro登录实现自定义路径跳转
一、实现需求
登录框架采用shiro,需根据不同客户端类型实现相对应定义页面跳转。
二、登录页jsp表单
<div class="input-prepend" title="Username" data-rel="tooltip">
<span class="add-on"><i class="icon-user"></i></span> <input type="text" id="username" name="username" value="${username}" class="input-medium required"/>
</div>
<div class="clearfix"></div> <div class="input-prepend" title="Password" data-rel="tooltip">
<span class="add-on"><i class="icon-lock"></i></span><input type="password" id="password" name="password" class="input-medium required"/>
</div>
<div class="clearfix"></div> <div><input type="hidden" id="clienttype" name="clienttype" value="pc"/></div> <div class="input-prepend">
<label class="checkbox" for="rememberMe"><input type="checkbox" id="rememberMe" name="rememberMe"/> 记住我<a href="${ctx}/register">注册</a></label>
</div>
<div class="clearfix"></div> <p class="center span5">
<button type="submit" class="btn btn-primary">登录</button>
</p>
三、自定义过滤器类
创建自定义过滤器LoginFormAuthenticationFilter,继承FormAuthenticationFilter。然后通过重写onLoginSuccess方法与WebUtils工具类,实现自定义跳转。
public class LoginFormAuthenticationFilter extends FormAuthenticationFilter {
private static Logger logger = LoggerFactory.getLogger(LoginFormAuthenticationFilter.class);
@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request,
ServletResponse response) throws Exception {
ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
if(user != null) {
HttpServletRequest req = (HttpServletRequest) request;
String clientType = (String) req.getParameter("clienttype");
logger.debug("[LOGIN]login success(user:" + user.loginName + ", client:" + clientType + ")");
if("pc".equals(clientType)) { // 请求方为pc,执行原方法
return super.onLoginSuccess(token, subject, request, response);
} else { // 其他,根据类型跳转指定页面
WebUtils.getAndClearSavedRequest(request); // 清除登录前请求路径
String fallbackUrl = null;
fallbackUrl = "/static/" + clientType + ".jsp"; // 自定义跳转界面
WebUtils.redirectToSavedRequest(request, response, fallbackUrl);
}
}
return false;
}
}
四、修改shiro配置文件
在applicationContext-shiro.xml中配置自定义过滤器。
首先,修改beans元素属性;
xmlns:util=http://www.springframework.org/schema/util
xsi:schemaLocation:
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
其次,修改shiroFilter内容,添加自定义过滤器属性。
<!-- 请求分发过滤器 -->
<bean id="loginDispacher" class="cn.com.mcfly.web.filter.LoginFormAuthenticationFilter"/>
<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="filters">
<util:map>
<entry key="authc" value-ref="loginDispacher"/>
</util:map>
</property>
<!--<property name="successUrl" value="/" />-->
<property name="filterChainDefinitions">
<value>
/static/** = anon
/login = authc
/logout = logout
...
/** = user
</value>
</property>
</bean>
shiro登录实现自定义路径跳转的更多相关文章
- 使用Shiro登录成功后,跳转到之前访问的页面实现
转:http://blog.csdn.net/lhacker/article/details/20450855 很多时候,我们需要做到,当用户登录成功后,跳转回登录前的页面.如果用户是点击" ...
- 简单实现Shiro单点登录(自定义Token令牌)
1. MVC Controller 映射 sso 方法. /** * 单点登录(如已经登录,则直接跳转) * @param userCode 登录用户编码 * @param token 登录令牌,令牌 ...
- Shiro登录成功之后跳到指定URL
通常我们使用shiro,登录之后就会跳到我们上一次访问的URL,如果我们是直接访问登录页面的话,shiro就会根据我们配置的successUrl去重定向,如果我们没有配置successUrl的话,那么 ...
- Vue.js Extension Pack 和 jsconfig.json 可以定位跳转到@开头的路径等自定义路径
Vue.js Extension Pack | vsCode插件 可以定位跳转到@开头的路径等自定义路径 webpack自定义别名后,VScode路径提示问题 //tsconfig.json 或者 j ...
- spring boot(十四)shiro登录认证与权限管理
这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security ...
- shiro登录验证简单理解
这两天接手了下师兄的项目,要给系统加个日志管理模块,其中需要记录登录功能的日志,那么首先要知道系统的登录是在哪里实现验证的. 该系统把所有登录验证还有权限控制的工作都交给了shiro. 这篇文章就先简 ...
- Springboot项目搭建(3)-shiro登录
shiro简述+实现简单登录:https://www.jianshu.com/p/7f724bec3dc3
- Shiro登录中遇到了问题
Shiro登录中遇到了问题 记录二次开发中遇到的问题, 如果系统学习Shiro, 推荐跟我学Shrio. 问题 项目是要将验证从本地改为LDAP验证, 但是因为jeecms的验证和授权中, 用户和角色 ...
- Spring Boot Shiro权限管理--自定义 FormAuthenticationFilter验证码整合
思路shiro使用FormAuthenticationFilter进行表单认证,验证校验的功能应该加在FormAuthenticationFilter中,在认证之前进行验证码校验. 需要写FormAu ...
随机推荐
- CentOS下如何根据Dump文件分析线上问题
https://blog.csdn.net/lixin03080/article/details/79711296 一.保存现场 1.记录系统整体资源使用情况,进程信息.线程信息 top -b -n ...
- Easyui datagrid 去掉表头的checkbox复选框
$(".datagrid-header-check").html(""); 在onLoadSuccess中加入此行代码即可实现datagrid去除表头的chec ...
- 【liunx】Linux下的压缩和解压缩命令——jar
原文链接:http://blog.chinaunix.net/uid-692788-id-2681136.html JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包.当然也是 ...
- JavaEE开发的颠覆者 Spring Boot实战--笔记
1.Spring boot的三种启动模式 Spring 的问题 Spring boot的特点,没有特别的地方 1.Spring 基础 PS:关于spring配置 PS: 现在都已经使用 java配置, ...
- gcc的编译属性和选项
1.指定内存默认对其参数: __attribute__((packed)):按一字节对其__attribute__((aligned(n))):从此之后默认按n字节对其 例如: struct stu ...
- sqler sql 转rest api 授权处理
我们可以使用内置的authorizer 以及js 脚本,方便的进行api 接口的授权处理 说明: 这个是2.0 的功能,注意版本的使用 参考格式 addpost { authorizer = & ...
- Singer 学习十一 配置以及状态管理
配置和状态文件通过提供身份验证信息,开始时间和有关以前调用的信息,帮助为Taps和Targets提供上下文 配置文件 配置文件包含tap 运行需要的信息,通常包含API,以及数据源的凭据 特殊字段 s ...
- 记录:禁用你的元素 bootstrap
记录:禁用你的元素 bootstrap <script> $(document).ready(function() { $("#target1").css(" ...
- Bjarne Stroustrup announces C++ Core Guidelines
This morning in his opening keynote at CppCon, Bjarne Stroustrup announced the C++ Core Guidelines ( ...
- [转]HashMap的实现原理
1. HashMap概述: HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变 ...