【发布时间】:2018-02-03 07:40:51
【问题描述】:
我正在尝试将 Spring Security 集成到我的 Spring Web 应用程序中。基本上我需要根据用户权限隐藏一些菜单。这就是我所做的。
我在类路径中的 JARS 下面添加了。
spring-security-acl-4.0.2.RELEASE.jar
spring-security-config-4.0.2.RELEASE.jar
spring-security-core-4.0.2.RELEASE.jar
spring-security-taglibs-4.0.1.RELEASE.jar
spring-security-web-4.0.2.RELEASE.jar
以下是 web.xml 中的条目
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>/WEB-INF/web_log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-root.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
我写了一个类 CustomPermissionEvaluator,如下所示。
public class CustomPermissionEvaluator implements PermissionEvaluator{
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
HttpServletRequest request = (HttpServletRequest) targetDomainObject;
Profile userProfile = (Profile) request.getSession().getAttribute("testprofile");
if (userProfile.getPermissionMap().get(String.valueOf(permission)) != null) {
return true;
} else {
return false;
}
}
@Override
public boolean hasPermission(Authentication arg0, Serializable arg1,
String arg2, Object arg3) {
// TODO Auto-generated method stub
return false;
}
}
在此之后,我编写了 SecurityConfig 文件。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler();
handler.setPermissionEvaluator(new CustomPermissionEvaluator());
web.expressionHandler(handler);
}
}
我的 spring-root.xml 中有以下条目
<sec:global-method-security pre-post-annotations="enabled">
<sec:expression-handler ref="expressionHandler" />
</sec:global-method-security>
<bean id="expressionHandler"
class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<property name="permissionEvaluator" ref="permissionEvaluator" />
</bean>
<bean id="permissionEvaluator" class="main.java.com.config.CustomPermissionEvaluator" />
现在在我的 JSP 文件中,我在 taglib 下使用。
及以下代码
<sec:authorize access="hasPermission('cadastra_categoria', #request)">
<div id="TEST">
</div>
</sec:authorize>
但它不起作用。任何建议将不胜感激。
【问题讨论】:
-
是否有指标、错误之类的?放置断点时,hasPermission 调用是否到达了实现?我认为您缺少安全过滤器链docs.spring.io/spring-security/site/docs/4.2.3.RELEASE/…,但这是一个假设
-
不,我没有收到任何错误,也没有调用 CustomPermissionEvaluator 。
-
你确定你正确引用你的bean吗?
class="main.java.com.config.CustomPermissionEvaluator"- 这不应该没有main.java吗? -
@aturkovic 是的,我指的是我的 bean 是正确的。项目就是这样。
-
您是否尝试过添加一些日志来查看您的评估是否真的被调用?此外,一些日志记录用于调试并查看您是否遇到任何异常?
标签: java spring spring-mvc spring-security