如何使用Shiro实现不同用户登录成功后跳转到不同主页?
如何使用Shiro实现不同用户登录成功后跳转到不同主页?10
Shiro配置文件中successUrl指定的页面只有一个:Java代码

- <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

2
0 0 5
添加评论
关注(0)
4个答案按时间排序按投票排序
在success页面进行判断,不同的情况下重定向到不同页面
sendredirect

93
0 0 7
添加评论
登陆成功后获取 Subject 对象.
然后通过 Subject 对象来判断当前用户的角色/权限, 之后执行不同的跳转(直接在LoginAction中做).
我的登陆部分代码:

- 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代码

- <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

99
0 1 14
添加评论
1.力推 Filter 过滤器,
2.后台判断根据权限跳转页面

2207
1 3 134
添加评论
最简单的办法到success页面进行判断,然后不同的用户再重定向到不同的页面。
2012年9月11日 12:19
7280
2 3 591
如何使用Shiro实现不同用户登录成功后跳转到不同主页?10
Shiro配置文件中successUrl指定的页面只有一个:Java代码

- <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

2
0 0 5
-
添加评论
-
关注(0)
4个答案按时间排序按投票排序
在success页面进行判断,不同的情况下重定向到不同页面
sendredirect

93
0 0 7
-
添加评论
登陆成功后获取 Subject 对象.
然后通过 Subject 对象来判断当前用户的角色/权限, 之后执行不同的跳转(直接在LoginAction中做).
我的登陆部分代码:

- 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代码

- <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

99
0 1 14
-
添加评论
1.力推 Filter 过滤器,
2.后台判断根据权限跳转页面

2207
1 3 134
-
添加评论
最简单的办法到success页面进行判断,然后不同的用户再重定向到不同的页面。
2012年9月11日 12:19
7280
2 3 591
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: A标签使用javascript:伪协议
- 下一篇: C#获取SQLServer数据库表名和字段名