栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring MVC-检查用户是否已经通过Spring Security登录?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring MVC-检查用户是否已经通过Spring Security登录?

至少有4种不同的方式:

spring security XML configuration

这是最简单的方法

<security:http auto-config="true" use-expressions="true" ...>   ...  <security:intercept-url pattern="/forAll     public boolean myCustomAuthenticatedexpression() {        return super.request.getSession().getValue("myFlag") != null;     }}

2.)你需要对进行扩展

DefaultWebSecurityexpressionRootHandler
以拥有一个提供自定义表达式根的处理程序

 public class MyCustomWebSecurityexpressionHandler   extends DefaultWebSecurityexpressionHandler {      @Override   public evaluationContext createevaluationContext(Authentication a,     FilterInvocation f) {          StandardevaluationContext ctx =        (StandardevaluationContext) super.createevaluationContext(a, f);WebSecurityexpressionRoot myRoot =         new MyCustomWebSecurityexpressionRoot(a, f);ctx.setRootObject(myRoot);return ctx;      } }

3.)然后,你需要向选民注册你的经纪人

<security:http use-expressions="true" access-decision-manager-ref="httpAccessDecisionManager" ...>      ...    <security:intercept-url pattern="/restricted/**"   access="myCustomAuthenticatedexpression" />    ...</security:http><bean id="httpAccessDecisionManager"      >    <constructor-arg name="decisionVoters"> <list>     <ref bean="webexpressionVoter" /> </list>    </constructor-arg></bean><bean id="webexpressionVoter"      >    <property name="expressionHandler"   ref="myCustomWebSecurityexpressionHandler" /></bean><bean id="myCustomWebSecurityexpressionHandler"     />

Spring Security 3.1更新

从Spring Security 3.1开始,实现自定义表达式要容易一些。一个不再需要

sublcass WebSecurityexpressionHandler
override createevaluationContext
。取而代之的是一个
sublas AbstractSecurityexpressionHandler<FilterInvocation>
或其子类
DefaultWebSecurityexpressionHandler
并重写
SecurityexpressionOperations createSecurityexpressionRoot(final Authentication a, final FilterInvocation f)

 public class MyCustomWebSecurityexpressionHandler   extends DefaultWebSecurityexpressionHandler {      @Override   public SecurityexpressionOperations createSecurityexpressionRoot(     Authentication a,     FilterInvocation f) {WebSecurityexpressionRoot myRoot =         new MyCustomWebSecurityexpressionRoot(a, f);myRoot.setPermissionevaluator(getPermissionevaluator());myRoot.setTrustResolver(this.trustResolver);myRoot.setRoleHierarchy(getRoleHierarchy());return myRoot;      } }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/400509.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号