一、实现需求

登录框架采用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登录实现自定义路径跳转的更多相关文章

  1. 使用Shiro登录成功后,跳转到之前访问的页面实现

    转:http://blog.csdn.net/lhacker/article/details/20450855 很多时候,我们需要做到,当用户登录成功后,跳转回登录前的页面.如果用户是点击" ...

  2. 简单实现Shiro单点登录(自定义Token令牌)

    1. MVC Controller 映射 sso 方法. /** * 单点登录(如已经登录,则直接跳转) * @param userCode 登录用户编码 * @param token 登录令牌,令牌 ...

  3. Shiro登录成功之后跳到指定URL

    通常我们使用shiro,登录之后就会跳到我们上一次访问的URL,如果我们是直接访问登录页面的话,shiro就会根据我们配置的successUrl去重定向,如果我们没有配置successUrl的话,那么 ...

  4. Vue.js Extension Pack 和 jsconfig.json 可以定位跳转到@开头的路径等自定义路径

    Vue.js Extension Pack | vsCode插件 可以定位跳转到@开头的路径等自定义路径 webpack自定义别名后,VScode路径提示问题 //tsconfig.json 或者 j ...

  5. spring boot(十四)shiro登录认证与权限管理

    这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security ...

  6. shiro登录验证简单理解

    这两天接手了下师兄的项目,要给系统加个日志管理模块,其中需要记录登录功能的日志,那么首先要知道系统的登录是在哪里实现验证的. 该系统把所有登录验证还有权限控制的工作都交给了shiro. 这篇文章就先简 ...

  7. Springboot项目搭建(3)-shiro登录

    shiro简述+实现简单登录:https://www.jianshu.com/p/7f724bec3dc3

  8. Shiro登录中遇到了问题

    Shiro登录中遇到了问题 记录二次开发中遇到的问题, 如果系统学习Shiro, 推荐跟我学Shrio. 问题 项目是要将验证从本地改为LDAP验证, 但是因为jeecms的验证和授权中, 用户和角色 ...

  9. Spring Boot Shiro权限管理--自定义 FormAuthenticationFilter验证码整合

    思路shiro使用FormAuthenticationFilter进行表单认证,验证校验的功能应该加在FormAuthenticationFilter中,在认证之前进行验证码校验. 需要写FormAu ...

随机推荐

  1. ZOJ 1005:Jugs(思维)

    Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Har ...

  2. HDU2044:一只小蜜蜂...

    题目贴不上了︿( ̄︶ ̄)︿http://acm.hdu.edu.cn/showproblem.php?pid=2044 注意数据类型,用int会超范围 #include<stdio.h> ...

  3. set_uid set_gid stick_bit 软硬链接

    1.set_uid,里面的s权限   即运行一个命令时,普通用户临时拥有root权限 ( 增加和移除s权限 chmod u+s  file_name chmod u-s file_name 大S  和 ...

  4. A Spy in the Metro(UVA 1025 ACM/ICPC World Finals2003)

    ---恢复内容开始--- 题意:有n(2<=n<=50)个车站,从左到右编号为1~n,有M1辆列车从第1站向右开,还有M2辆列车从第N站向左开.在时刻0,间谍从第1站出发,目的是在时刻T( ...

  5. 【spring源码分析】BeanDefinitionRegistryPostProcessor解析

    一.自定义BeanDefinitionRegistryPostProcessor BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProces ...

  6. CF643D Bearish Fanpages

    题意 英文版题面 Problems Submit Status Standings Custom test .input-output-copier { font-size: 1.2rem; floa ...

  7. C++中的抽象类

    一.抽象类学习笔记 1.virtual修饰函数(虚函数)后面加=0就称为一个纯虚函数,一个类中只要有纯虚函数那么它就是一个抽象类.抽象类不能用来实例化对象,是用来给他的派生类定义好这些框架的,给使用这 ...

  8. Singer 学习十三 发现模式

    发现模式 发现模式提供了一种描述tap 支持数据流的方式,使用了json schema 做为描述数据的结构以及每个数据流的 类型,发现模式的实现依赖tap 的数据源,有些taps 将硬编码每个流的模式 ...

  9. webpack 中,loader、plugin 的区别

    loader 和 plugin 的主要区别: loader 用于加载某些资源文件. 因为 webpack 只能理解 JavaScript 和 JSON 文件,对于其他资源例如 css,图片,或者其他的 ...

  10. Visual Studio Code 使用

    VS Code 快捷键: 快捷键 作用 Option+Up 或 Option+Down 上下移动一行 Shift+Option+Up 或 Shift+Option+Down 向上向下复制一行 VS C ...