如何使用Shiro实现不同用户登录成功后跳转到不同主页?
2015-09-05 02:30
836 查看
0
Shiro配置文件中successUrl指定的页面只有一个:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login"/>
<property name="successUrl" value="/main"/>
<property name="unauthorizedUrl" value="/login"/>
...
</bean>
有的用户登录成功后要跳转到别的页面,怎么实现?
shiro
2012年9月11日 11:48
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/04/e10fe5334441baf3602f31a9c1ced508)
cwx714
2
0 0 5
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/a9136ab27e9bbdab7caeedc9a8de7869)
添加评论
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/0b757f00e92c5e784da856f348ef8f33.gif)
关注(0)
sendredirect2012年9月26日 14:11
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/04/a3a588d4abc94b19e9386787434dfea0)
hz020815
93
0 0 7
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/a9136ab27e9bbdab7caeedc9a8de7869)
添加评论
00登陆成功后获取 Subject 对象.
然后通过 Subject 对象来判断当前用户的角色/权限, 之后执行不同的跳转(直接在LoginAction中做).
我的登陆部分代码:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
UsernamePasswordToken token = new UsernamePasswordToken(name, password);
try {
SecurityUtils.getSubject().login(token);
Subject subject = SecurityUtils.getSubject();
// 这里可以调用subject 做判断
System.out.println("--------------------------------------------------------------");
Boolean isadmin = subject.hasRole("admin");
log.info("是否为管理员:"+isadmin);
System.out.println("--------------------------------------------------------------");
String userId = (String)subject.getPrincipal();
User user = userService.getById(userId);
ShiroUser shiroUser = shiroUserService.getByDyId(userId);
if(shiroUser == null){
this.addActionError(getText("login.failure"));
return ERROR;
}else{
int used = shiroUser.getUsed();
if(used == 1){
this.addActionError(getText("login.noused"));
return ERROR;
}
}
Session session = subject.getSession(true);
session.setAttribute(LoginAction.USER_KEY, user);
session.setAttribute(LoginAction.SHIRO_USER_KEY, shiroUser);
log.info("set workflow define to session");
session.setAttribute("ptDefine", WorkflowContext.getPtDefine());
} catch (AuthenticationException e) {
log.info(e.getMessage());
this.addActionError(getText("login.failure"));
}
if (this.hasErrors()) {
log.info("login erro ...");
return ERROR;
}
配置, 登陆跳转基本没用到, 注意 filterChainDefinitions,
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- override these for application-specific URLs if you like: -->
<property name="loginUrl" value="/index.jsp"/>
<property name="successUrl" value="/home.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean -->
<!-- defined will be automatically acquired and available via its beanName in chain -->
<!-- definitions, but you can perform instance overrides or name aliases here if you like: -->
<!-- -->
<property name="filters">
<map>
<entry key="authc">
<bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter"/>
</entry>
</map>
</property>
<property name="filterChainDefinitions">
<value>
# static file chains
/js/* = anon
/css/* = anon
/img/* = anon
/images/* = anon
/applets/* = anon
# login/logout chain
/login.action = anon
# some example chain definitions:
#/admin/** = authc, roles[ptAdmin]
/docs/** = authc, perms[document:read]
/** = user
# more URL-to-FilterChain definitions here
</value>
</property>
</bean>
2012年9月11日 15:19
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/04/513ccb57804ff32477c70b569e92cb88)
witcheryne
99
0 1 14
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/a9136ab27e9bbdab7caeedc9a8de7869)
添加评论
001.力推 Filter 过滤器,
2.后台判断根据权限跳转页面
2012年9月11日 12:38
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/04/44df42cf90dcb99df8265d030219615d)
demojava
2207
1 3 134
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/a9136ab27e9bbdab7caeedc9a8de7869)
添加评论
00最简单的办法到success页面进行判断,然后不同的用户再重定向到不同的页面。2012年9月11日 12:19
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/04/9349c101b73c417849e62de17b77a4cf)
jinnianshilongnian
7280
2 3 5910
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login"/>
<property name="successUrl" value="/main"/>
<property name="unauthorizedUrl" value="/login"/>
...
</bean>
有的用户登录成功后要跳转到别的页面,怎么实现?shiro 2012年9月11日 11:48
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/04/e10fe5334441baf3602f31a9c1ced508)
cwx714
2
0 0 5
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/a9136ab27e9bbdab7caeedc9a8de7869)
添加评论
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/0b757f00e92c5e784da856f348ef8f33.gif)
关注(0)
00
在success页面进行判断,不同的情况下重定向到不同页面
sendredirect
2012年9月26日 14:11
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/04/a3a588d4abc94b19e9386787434dfea0)
hz020815
93
0 0 7
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/a9136ab27e9bbdab7caeedc9a8de7869)
添加评论
00
登陆成功后获取 Subject 对象.
然后通过 Subject 对象来判断当前用户的角色/权限, 之后执行不同的跳转(直接在LoginAction中做).
我的登陆部分代码:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
UsernamePasswordToken token = new UsernamePasswordToken(name, password);
try {
SecurityUtils.getSubject().login(token);
Subject subject = SecurityUtils.getSubject();
// 这里可以调用subject 做判断
System.out.println("--------------------------------------------------------------");
Boolean isadmin = subject.hasRole("admin");
log.info("是否为管理员:"+isadmin);
System.out.println("--------------------------------------------------------------");
String userId = (String)subject.getPrincipal();
User user = userService.getById(userId);
ShiroUser shiroUser = shiroUserService.getByDyId(userId);
if(shiroUser == null){
this.addActionError(getText("login.failure"));
return ERROR;
}else{
int used = shiroUser.getUsed();
if(used == 1){
this.addActionError(getText("login.noused"));
return ERROR;
}
}
Session session = subject.getSession(true);
session.setAttribute(LoginAction.USER_KEY, user);
session.setAttribute(LoginAction.SHIRO_USER_KEY, shiroUser);
log.info("set workflow define to session");
session.setAttribute("ptDefine", WorkflowContext.getPtDefine());
} catch (AuthenticationException e) {
log.info(e.getMessage());
this.addActionError(getText("login.failure"));
}
if (this.hasErrors()) {
log.info("login erro ...");
return ERROR;
}
配置, 登陆跳转基本没用到, 注意 filterChainDefinitions,
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- override these for application-specific URLs if you like: -->
<property name="loginUrl" value="/index.jsp"/>
<property name="successUrl" value="/home.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean -->
<!-- defined will be automatically acquired and available via its beanName in chain -->
<!-- definitions, but you can perform instance overrides or name aliases here if you like: -->
<!-- -->
<property name="filters">
<map>
<entry key="authc">
<bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter"/>
</entry>
</map>
</property>
<property name="filterChainDefinitions">
<value>
# static file chains
/js/* = anon
/css/* = anon
/img/* = anon
/images/* = anon
/applets/* = anon
# login/logout chain
/login.action = anon
# some example chain definitions:
#/admin/** = authc, roles[ptAdmin]
/docs/** = authc, perms[document:read]
/** = user
# more URL-to-FilterChain definitions here
</value>
</property>
</bean>
2012年9月11日 15:19
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/04/513ccb57804ff32477c70b569e92cb88)
witcheryne
99
0 1 14
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/a9136ab27e9bbdab7caeedc9a8de7869)
添加评论
00
1.力推 Filter 过滤器,
2.后台判断根据权限跳转页面
2012年9月11日 12:38
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/04/44df42cf90dcb99df8265d030219615d)
demojava
2207
1 3 134
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/a9136ab27e9bbdab7caeedc9a8de7869)
添加评论
00
最简单的办法到success页面进行判断,然后不同的用户再重定向到不同的页面。
2012年9月11日 12:19
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/04/9349c101b73c417849e62de17b77a4cf)
jinnianshilongnian
7280
2 3 591
如何使用Shiro实现不同用户登录成功后跳转到不同主页?10
Shiro配置文件中successUrl指定的页面只有一个: Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login"/>
<property name="successUrl" value="/main"/>
<property name="unauthorizedUrl" value="/login"/>
...
</bean>
有的用户登录成功后要跳转到别的页面,怎么实现?
shiro
2012年9月11日 11:48
cwx714
2
0 0 5
添加评论
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/0b757f00e92c5e784da856f348ef8f33.gif)
关注(0)
4个答案按时间排序按投票排序
00在success页面进行判断,不同的情况下重定向到不同页面sendredirect2012年9月26日 14:11
hz020815
93
0 0 7
添加评论
00登陆成功后获取 Subject 对象.
然后通过 Subject 对象来判断当前用户的角色/权限, 之后执行不同的跳转(直接在LoginAction中做).
我的登陆部分代码:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
UsernamePasswordToken token = new UsernamePasswordToken(name, password);
try {
SecurityUtils.getSubject().login(token);
Subject subject = SecurityUtils.getSubject();
// 这里可以调用subject 做判断
System.out.println("--------------------------------------------------------------");
Boolean isadmin = subject.hasRole("admin");
log.info("是否为管理员:"+isadmin);
System.out.println("--------------------------------------------------------------");
String userId = (String)subject.getPrincipal();
User user = userService.getById(userId);
ShiroUser shiroUser = shiroUserService.getByDyId(userId);
if(shiroUser == null){
this.addActionError(getText("login.failure"));
return ERROR;
}else{
int used = shiroUser.getUsed();
if(used == 1){
this.addActionError(getText("login.noused"));
return ERROR;
}
}
Session session = subject.getSession(true);
session.setAttribute(LoginAction.USER_KEY, user);
session.setAttribute(LoginAction.SHIRO_USER_KEY, shiroUser);
log.info("set workflow define to session");
session.setAttribute("ptDefine", WorkflowContext.getPtDefine());
} catch (AuthenticationException e) {
log.info(e.getMessage());
this.addActionError(getText("login.failure"));
}
if (this.hasErrors()) {
log.info("login erro ...");
return ERROR;
}
配置, 登陆跳转基本没用到, 注意 filterChainDefinitions,
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- override these for application-specific URLs if you like: -->
<property name="loginUrl" value="/index.jsp"/>
<property name="successUrl" value="/home.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean -->
<!-- defined will be automatically acquired and available via its beanName in chain -->
<!-- definitions, but you can perform instance overrides or name aliases here if you like: -->
<!-- -->
<property name="filters">
<map>
<entry key="authc">
<bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter"/>
</entry>
</map>
</property>
<property name="filterChainDefinitions">
<value>
# static file chains
/js/* = anon
/css/* = anon
/img/* = anon
/images/* = anon
/applets/* = anon
# login/logout chain
/login.action = anon
# some example chain definitions:
#/admin/** = authc, roles[ptAdmin]
/docs/** = authc, perms[document:read]
/** = user
# more URL-to-FilterChain definitions here
</value>
</property>
</bean>
2012年9月11日 15:19
witcheryne
99
0 1 14
添加评论
001.力推 Filter 过滤器,
2.后台判断根据权限跳转页面
2012年9月11日 12:38
demojava
2207
1 3 134
添加评论
00最简单的办法到success页面进行判断,然后不同的用户再重定向到不同的页面。2012年9月11日 12:19
jinnianshilongnian
7280
2 3 5910
如何使用Shiro实现不同用户登录成功后跳转到不同主页?10
Shiro配置文件中successUrl指定的页面只有一个:Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login"/>
<property name="successUrl" value="/main"/>
<property name="unauthorizedUrl" value="/login"/>
...
</bean>
有的用户登录成功后要跳转到别的页面,怎么实现?shiro 2012年9月11日 11:48
cwx714
2
0 0 5
添加评论
![](https://oscdn.geek-share.com/Uploads/Images/Content/201612/0b757f00e92c5e784da856f348ef8f33.gif)
关注(0)
4个答案按时间排序按投票排序
00在success页面进行判断,不同的情况下重定向到不同页面
sendredirect
2012年9月26日 14:11
hz020815
93
0 0 7
添加评论
00
登陆成功后获取 Subject 对象.
然后通过 Subject 对象来判断当前用户的角色/权限, 之后执行不同的跳转(直接在LoginAction中做).
我的登陆部分代码:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
UsernamePasswordToken token = new UsernamePasswordToken(name, password);
try {
SecurityUtils.getSubject().login(token);
Subject subject = SecurityUtils.getSubject();
// 这里可以调用subject 做判断
System.out.println("--------------------------------------------------------------");
Boolean isadmin = subject.hasRole("admin");
log.info("是否为管理员:"+isadmin);
System.out.println("--------------------------------------------------------------");
String userId = (String)subject.getPrincipal();
User user = userService.getById(userId);
ShiroUser shiroUser = shiroUserService.getByDyId(userId);
if(shiroUser == null){
this.addActionError(getText("login.failure"));
return ERROR;
}else{
int used = shiroUser.getUsed();
if(used == 1){
this.addActionError(getText("login.noused"));
return ERROR;
}
}
Session session = subject.getSession(true);
session.setAttribute(LoginAction.USER_KEY, user);
session.setAttribute(LoginAction.SHIRO_USER_KEY, shiroUser);
log.info("set workflow define to session");
session.setAttribute("ptDefine", WorkflowContext.getPtDefine());
} catch (AuthenticationException e) {
log.info(e.getMessage());
this.addActionError(getText("login.failure"));
}
if (this.hasErrors()) {
log.info("login erro ...");
return ERROR;
}
配置, 登陆跳转基本没用到, 注意 filterChainDefinitions,
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201212/7a1e7afc0f2addbbdb746966b60e9e4a.png)
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- override these for application-specific URLs if you like: -->
<property name="loginUrl" value="/index.jsp"/>
<property name="successUrl" value="/home.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean -->
<!-- defined will be automatically acquired and available via its beanName in chain -->
<!-- definitions, but you can perform instance overrides or name aliases here if you like: -->
<!-- -->
<property name="filters">
<map>
<entry key="authc">
<bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter"/>
</entry>
</map>
</property>
<property name="filterChainDefinitions">
<value>
# static file chains
/js/* = anon
/css/* = anon
/img/* = anon
/images/* = anon
/applets/* = anon
# login/logout chain
/login.action = anon
# some example chain definitions:
#/admin/** = authc, roles[ptAdmin]
/docs/** = authc, perms[document:read]
/** = user
# more URL-to-FilterChain definitions here
</value>
</property>
</bean>
2012年9月11日 15:19
witcheryne
99
0 1 14
添加评论
00
1.力推 Filter 过滤器,
2.后台判断根据权限跳转页面
2012年9月11日 12:38
demojava
2207
1 3 134
添加评论
00
最简单的办法到success页面进行判断,然后不同的用户再重定向到不同的页面。
2012年9月11日 12:19
jinnianshilongnian
7280
2 3 591
相关文章推荐
- 求一元多项式
- LeetCode Invert Binary Tree
- LeetCode Invert Binary Tree
- python 函数的文档字符串 docstrings
- python 循环语句的else语句用法,当循环条件变为假,切不是通过breakbreak终止的时候,就会执行这个else语句。
- 下一跳:使用IP地址与接口的区别
- quick-cocos2dx3.5 mac模拟器改进
- Python爬虫教程——实战二三四五
- BZOJ 3781
- 1.xrange和range不要混了,2.range(len(xx))不如用enumerate
- dragonbones插件编译
- QGis2.9在windows下的编译以及二次开发包下载
- 手游系统逻辑档案之通信协议
- Python爬虫教程——实战一之爬取糗事百科段子
- 关于python 序列 深拷贝
- quick-cocos2dx 3.5集成protobuf
- linux 磁盘文件系统详解
- 随时随地笔记“好学”全程实践(三)- 数据库设计
- LeetCode Symmetric Tree
- LeetCode Symmetric Tree