Springsecurity之SwitchUserFilter切换用户

    使用的版本是Springsecurity的4.2.x版本。

    业务场景是这样的,系统中存在很多用户,超级管理员要有个功能,就是可以切换用户,比如超级管理员,可以切换为系统中的任何一个用户。Spingsecurity提供了一个SwitchUserFilter,我们就用这个来实现功能。

1、先来看怎么使用

    List-1.1

<bean id="switchUserFilter" class="org.springframework.security.web.authentication.switchuser.SwitchUserFilter">
    <property name="userDetailsService" ref="userService"/>
    <property name="usernameParameter" value="userNo"/>
    <property name="switchUserUrl" value="/switch/user"/>
</bean>
  1. 定义switchUserFilter这个bean,如下List-1所示。用过Springsecurity的,应该知道UserDetailsService,这里就不再细讲。
  2. usernameParameter的值是userNo,会从HttpServletRequest中获取key为userNo的值,之后用这个值从UserDetailsService中获得UserDetails。
  3. switchUserUrl,SwitchUserFilter是个Filter,会拦截请求,之后判断请求的url是否是switchUserUrl,如果是就执行SwitchUserFilter里面的逻辑,如果不是,就将请求转给后面的Filter。

    List-1.1之后,将SwitchUserFilter放在FILTER_SECURITY_INTERCEPTOR之后,为什么呢?因为accessDecisionManager在FILTER_SECURITY_INTERCEPTOR中,即判断是否有访问资源的权限是在FILTER_SECURITY_INTERCEPTOR中,判断过权限之后才到SwitchUserFilter。

    List-1.2

<security:http >
    ...
    <security:custom-filter ref="switchUserFilter" after="FILTER_SECURITY_INTERCEPTOR"/>
    ...
</security:http>

2、SwitchUserFilter的源码

             

                                                                                        图2.1

    如2.1所示,SwitchUserFilter继承了GenericFilterBean,它是Springframework中的类。SwitchUserFilter的doFilte方法如下List-2.1所示

    List-2.1

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
		throws IOException, ServletException {
	HttpServletRequest request = (HttpServletRequest) req;
	HttpServletResponse response = (HttpServletResponse) res;

	// check for switch or exit request
	if (requiresSwitchUser(request)) {
		// if set, attempt switch and store original
		try {
			Authentication targetUser = attemptSwitchUser(request);

			// update the current context to the new target user
			SecurityContextHolder.getContext().setAuthentication(targetUser);

			// redirect to target url
			this.successHandler.onAuthenticationSuccess(request, response,
					targetUser);
		}
		catch (AuthenticationException e) {
			this.logger.debug("Switch User failed", e);
			this.failureHandler.onAuthenticationFailure(request, response, e);
		}

		return;
	}
...
  1. List-2.1中,requiresSwitchUser会判断request是否是switchUserUrl对应的请求。
  2. 获取Authentication只会,会将其设置到SecurityContextHolder.getContext()。

 

转载于:https://my.oschina.net/u/2518341/blog/2992707

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值