<security:authentication-manager alias="authenticationManager"> <security:authentication-provider ref="daoAuthenticationProvider" /> <security:authentication-provider ref="runAsImplAuthenticationProvider" /> </security:authentication-manager>
<bean id="methodSecurityInterceptor" class="org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor"> <property name="authenticationManager" ref="authenticationManager" /> <property name="accessDecisionManager" ref="accessDecisionManager" /> <property name="securityMetadataSource" ref="delegatingMethodSecurityMetadataSource" /> <property name="runAsManager" ref="runAsManager" /> <property name="objectDefinitionSource"> <value> zhangxin.security.service.RoleService.loadMenu=ROLE_USER, RUN_AS_TEMP zhangxin.security.RunAsDemo.RunAsDate.showDate=ROLE_ADMIN, ROLE_RUN_AS_TEMP </value> </property> </bean>
<!-- 替换验证身份 --> <bean id="runAsImplAuthenticationProvider" class="org.springframework.security.access.intercept.RunAsImplAuthenticationProvider"> <property name="key" value="javaee" /> </bean> <bean id="runAsManager" class="org.springframework.security.access.intercept.RunAsManagerImpl"> <property name="key" value="javaee" /> </bean>
objectDefinitionSource 与 delegatingMethodSecurityMetadataSource 一样,都为获取的资源。
以上配置
zhangxin.security.service.RoleService.loadMenu 简称方法A
zhangxin.security.RunAsDemo.RunAsDate.showDate 简称方法B
用户要访问方法B,而用户拥有的角色为ROLE_USER,
访问方法B,需要角色为ROLE_RUN_AS_TEMP,
那么此时就必须使用户拥有角色ROLE_RUN_AS_TEMP,才能访问方法B。
关键点再这里:
访问方法A的过程中会拥有以'RUN_AS'开头的RUN_AS_TEMP角色,实际过程中还会再加上角色前缀'ROLE_',也就是ROLE_RUN_AS_TEMP角色。
该用户在拥有角色ROLE_USER在执行方法A的过程中,拥有ROLE_RUN_AS_TEMP角色,在执行方法A过程中可以调用方法B。
注意: 用户执行方法A之前,没有ROLE_RUN_AS_TEMP角色,执行方法过程中才有,执行完毕后,自动移除ROLE_RUN_AS_TEMP角色。