我正在尝试将Spring Security集成到我的Spring应用程序中。基本上我需要根据用户权限隐藏一些菜单。这就是我所做的。
我在类路径中添加了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>
但它不起作用。任何建议将不胜感激。
答案 0 :(得分:2)
&#34; hasPermission(&#39; cadastra_categoria&#39;,#request;&#34;
实际上,有效调用必须交换参数,第一个必须是目标域对象,第二个 - 权限:
hasPermission(#request, 'cadastra_categoria')
我假设您还仔细检查了您已根据需要将sec
taglib导入您的JSP
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
最后,正如本answer的第二部分所阐明的那样,定义以下内容:
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class AnnotationConfigDispatcherServletInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {
SecurityConfig.class //your SecurityConfig
};
}
}
确保在您的Web应用程序启动期间调用configure(WebSecurity web)
答案 1 :(得分:2)
据我了解您的问题,您已创建了CustomPermissionEvaluator
课程,但未使用经过身份验证的用户权限进行检查。
我正在直接编写代码 CustomPermissionEvaluator
,以表明我的观点可能有任何错误:
public class CustomPermissionEvaluator implements PermissionEvaluator {
public boolean hasPermission(Authentication auth, Object targetDomainObject, Object permission) {
if ((auth == null) || (targetDomainObject == null) || !(permission instanceof String)){
return false;
}
Profile userProfile = (Profile) request.getSession().getAttribute("testprofile");
String targetType = userProfile.getPermissionMap().get(String.valueOf(permission));
return hasPrivilege(auth, targetType, permission.toString().toUpperCase());
}
private boolean hasPrivilege(Authentication auth, String targetType, String permission) {
for (GrantedAuthority grantedAuth : auth.getAuthorities()) {
if (grantedAuth.getAuthority().startsWith(targetType)) {
if (grantedAuth.getAuthority().contains(permission)) {
return true;
}
}
}
return false;
}
@Override
public boolean hasPermission(Authentication arg0, Serializable arg1, String arg2, Object arg3) {
// TODO Auto-generated method stub
return false;
}
}
答案 2 :(得分:0)
请尝试hasAnyRole并检查一次,即
<sec:authorize access="hasAnyRole('ROLE_NAME')"> TEST </sec:authorize>