spring security 登录根据用户角色跳转到不同的页面
By:Roy.LiuLast updated:2013-12-18
spring security 做的登录程序,不同角色的用户登录之后,可能会跳转到不同的页面,在默认情况下的配置,都是跳转到同一个页面,因为在 form-login 中设置的 default-target-url 就是登录后应该跳转到的页面。如何使得不同角色的用户登录后跳转到不同的页面呢? 至少有两种方法:
1. 方法一, 就在 form-login 的 default-target-url 上做处理,比如如果用 spring mvc 的controller 来做的话,就比较容易。
2. 方法二, 实现 spring 提供的 AuthenticationSuccessHandler 接口.
方法一, 就在 form-login 的 default-target-url 上做处理,比如如果用 spring mvc 的controller 来做的话,就比较容易。
在前面例子的基础上,增加如下代码:
同时 spring security 的配置如下:
这是一种比较简单的跳转方法, 在以前做系统的时候,基本都是采用这种方法,跳转到一个页面之后,再跳转到另外一个页面。
2. 方法二, 实现 spring 提供的 AuthenticationSuccessHandler 接口.
新增加一个类来处理页面的跳转:
同理,spring security 的配置文件也应该相应的修改:
当然这里用到 ref=mySuccessHandler, 所以需要配置这个 bean
其实这两种方式很类似,都实现了不同角色的用户登录后跳转到不同的页面。运行程序,用yihaomen/123456登录.

而用管理员admin/123456 登录后

这样很方便的实现了不同角色的用户登录后,跳转到不同的页面.
代码下载: spring security 不同用户登录后跳转不同页面
1. 方法一, 就在 form-login 的 default-target-url 上做处理,比如如果用 spring mvc 的controller 来做的话,就比较容易。
2. 方法二, 实现 spring 提供的 AuthenticationSuccessHandler 接口.
方法一, 就在 form-login 的 default-target-url 上做处理,比如如果用 spring mvc 的controller 来做的话,就比较容易。
在前面例子的基础上,增加如下代码:
@RequestMapping(value="/dispatch", method = RequestMethod.GET) public View dispatch(ModelMap model,HttpServletRequest request) { String path = request.getContextPath() ; String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; Setroles = AuthorityUtils.authorityListToSet(SecurityContextHolder.getContext() .getAuthentication().getAuthorities()); if (roles.contains("ROLE_ADMIN")) { return new RedirectView(basePath + "app/admin"); } return new RedirectView(basePath + "app/welcome"); }
同时 spring security 的配置如下:
这是一种比较简单的跳转方法, 在以前做系统的时候,基本都是采用这种方法,跳转到一个页面之后,再跳转到另外一个页面。
2. 方法二, 实现 spring 提供的 AuthenticationSuccessHandler 接口.
新增加一个类来处理页面的跳转:
package com.yihaomen.accesshandle; import java.io.IOException; import java.util.Set; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.security.core.Authentication; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; public class LoginSuccessHandle implements AuthenticationSuccessHandler{ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException,ServletException { Setroles = AuthorityUtils.authorityListToSet(authentication.getAuthorities()); String path = request.getContextPath() ; String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; if (roles.contains("ROLE_ADMIN")){ response.sendRedirect(basePath+"app/admin"); return; } response.sendRedirect(basePath+"app/welcome"); } }
同理,spring security 的配置文件也应该相应的修改:
当然这里用到 ref=mySuccessHandler, 所以需要配置这个 bean
其实这两种方式很类似,都实现了不同角色的用户登录后跳转到不同的页面。运行程序,用yihaomen/123456登录.

而用管理员admin/123456 登录后

这样很方便的实现了不同角色的用户登录后,跳转到不同的页面.
代码下载: spring security 不同用户登录后跳转不同页面
From:一号门
Previous:在 spring security 中使用自定义的错误消息
COMMENTS