为什么我的自定义PermissionEvaluator不起作用?
我无法理解为什么我的安全性无法正常工作。 方法hasPermission()在Evaluator类中甚至没有被调用。 我认为我的安全配置有问题。
我的安全配置:
这是我的自定义评估者:
public class UserPermissionEvaluator implements PermissionEvaluator { private UserService service; @Autowired public UserPermissionEvaluator(UserService service) { this.service = service; } @Override public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) { UserDetails principal = (UserDetails) authentication.getPrincipal(); User authorizedUser = service.getUser(principal.getUsername()); Collection userPermissions = authorizedUser.getPermissions(); for (Permission p : userPermissions) { if (p.getName().equals(allowedPermission)) { return true; } } return false; } @Override public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) { throw new RuntimeException("Error"); } }
控制器中使用安全性的方法:
@RequestMapping(method = RequestMethod.GET) @PreAuthorize("hasPermission(null, 'create_user')") public String createUserForm(@ModelAttribute("user") User user) { return "user/internal/form/credentialForm"; }
任何想法如何解决它?
UPD:
log4jConfigLocation classpath:/log4j.properties org.springframework.web.util.Log4jConfigListener contextConfigLocation classpath:/spring-config/security.xml classpath:/spring-config/data.xml org.springframework.web.context.ContextLoaderListener springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* mvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:/spring-config/mvc.xml 1 mvc / 404 /WEB-INF/views/error/404.jsp
UPD 2
Spring MVC配置:
这可能是因为
标记需要与Spring MVC配置位于相同的上下文中,否则您的控制器将不会进行后期处理。 这在FAQ中讨论 。
例如,如果您的web.xml如下所示:
contextConfigLocation /WEB-INF/spring/*.xml springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* org.springframework.web.context.ContextLoaderListener spring org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/mvc/*.xml
要在控制器上支持方法安全性,请确保在/WEB-INF/mvc/*.xml中的某个位置定义
标记。 请注意,配置的其余部分应保持原样。 如果要在服务上支持方法安全性,则可能还需要父级中的
(即现在可能的位置)。
如果这没有帮助,请发布您的web.xml或WebApplicationInitializer,如果您没有使用web.xml