我正在尝试使用Spring3.1实现部分级别的安全性。使用 Thymeleaf2.0 作为我的 View 部分。这是我为此所做的配置,
使用的 jar ## - 所有spring3.1 jar和thymeleaf-extras-springsecurity3.jar(版本1.0.0.beta-1)
SpringWebFlow-Servlet.xml
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
...
<property name="additionalDialects">
<set>
<bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
</set>
</property>
...
</bean>
<bean id="webexpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" />
spring-security.xml
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/productSelection" access="hasRole('ROLE_ADMIN')"/>
.....
</http>
xxx.html
<div sec:authorize="hasRole('ROLE_ADMIN')">
This only be seen if authenticated user has role ROLE_ADMIN.
</div>
问题
Getting an Exception stating : No visible WebSecurityExpressionHandler instance could be found in the applicationContext
在Spring3.1中,DefaultWebSecurityExpressionHandler没有实现WebSecurityExpressionHandler,并且该接口(interface)已被弃用。请让我知道解决方法,因为 Thymeleaf 正在尝试搜索 ApplicationContext 中不可用的 WebSecurityExpressionHandler 实例。
最佳答案
您需要在应用程序(根)上下文中而不是在 servlet 上下文中定义 bean。
<bean id="webSecurityExpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/>
我遇到了同样的问题,将其从 servlet-context.xml 移动到 root-context.xml 并且 bean 被拾取。
根据您的 Spring 设置,它可能位于另一个 xml 文件中,因此请检查您的 web.xml 中的 Spring 上下文根。
在您的情况下,如果 spring-security.xml 导入到 Spring 根上下文中,您可以将其添加到那里。
关于spring-security - 在 applicationContext 中找不到可见的 WebSecurityExpressionHandler 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12786251/