/connect-web/src/main/java/org/osforce/connect/web/security/interceptor/WidgetSecurityInterceptor.java

http://focus-sns.googlecode.com/ · Java · 125 lines · 101 code · 13 blank · 11 comment · 15 complexity · 5d63f745f8e77ecff4537f3394964288 MD5 · raw file

  1. package org.osforce.connect.web.security.interceptor;
  2. import java.lang.reflect.Method;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.Map;
  6. import javax.servlet.http.HttpServletRequest;
  7. import org.osforce.connect.entity.system.Project;
  8. import org.osforce.connect.entity.system.User;
  9. import org.osforce.connect.service.system.PermissionService;
  10. import org.osforce.connect.web.security.annotation.Permission;
  11. import org.osforce.spring4me.commons.collection.CollectionUtil;
  12. import org.osforce.spring4me.web.interceptor.WidgetInterceptorAdapter;
  13. import org.osforce.spring4me.web.widget.config.WidgetConfig;
  14. import org.osforce.spring4me.web.widget.core.HttpWidgetRequest;
  15. import org.osforce.spring4me.web.widget.core.HttpWidgetResponse;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.core.annotation.AnnotationUtils;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.support.HandlerMethodResolver;
  20. /**
  21. * This interceptor should the last one in interceptor stack
  22. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  23. * @since 1.1.0
  24. * @create May 20, 2011 - 3:49:11 AM
  25. * <a href="http://www.opensourceforce.org">????</a>
  26. */
  27. public class WidgetSecurityInterceptor extends WidgetInterceptorAdapter {
  28. private static final String PROJECT_KEY = "_" + Project.class.getName();
  29. private static final String USER_KEY = "_" + User.class.getName();
  30. private PermissionService permissionService;
  31. private Map<String, Permission> permissionMappings = CollectionUtil.newHashMap();
  32. public WidgetSecurityInterceptor() {
  33. }
  34. @Autowired
  35. public void setPermissionService(PermissionService permissionService) {
  36. this.permissionService = permissionService;
  37. }
  38. @Override
  39. protected boolean preHandleWidget(HttpWidgetRequest request,
  40. HttpWidgetResponse response, Object handler) throws Exception {
  41. resolveHandlerMethodPermission(request, handler);
  42. //
  43. return validatePermissions(request);
  44. }
  45. protected boolean validatePermissions(HttpServletRequest request) {
  46. WidgetConfig widgetConfig = (WidgetConfig) request.getAttribute(WidgetConfig.KEY) ;
  47. if(widgetConfig!=null) {
  48. Project project = (Project) request.getAttribute(PROJECT_KEY);
  49. User user = (User) request.getAttribute(USER_KEY);
  50. String requestPath = widgetConfig.getPath();
  51. Permission permission = permissionMappings.get(requestPath);
  52. if(permission!=null) {
  53. // validate user
  54. if(permission.userRequired() && user==null) {
  55. return false;
  56. }
  57. // validate project
  58. if(permission.projectRequired() && project==null) {
  59. return false;
  60. }
  61. // validate resource
  62. String[] resources = permission.value();
  63. return permissionService.hasPermission(project, user, resources);
  64. }
  65. }
  66. return true;
  67. }
  68. protected void resolveHandlerMethodPermission(HttpServletRequest request, Object handler) {
  69. WidgetConfig widgetConfig = (WidgetConfig) request.getAttribute(WidgetConfig.KEY) ;
  70. if(widgetConfig!=null) {
  71. String requestPath = widgetConfig.getPath();
  72. if(!permissionMappings.containsKey(requestPath)) {
  73. HandlerMethodResolver handlerMethodResolver = new HandlerMethodResolver();
  74. handlerMethodResolver.init(handler.getClass());
  75. RequestMapping classRequestMapping = AnnotationUtils.findAnnotation(handler.getClass(), RequestMapping.class);
  76. String[] namespaces = classRequestMapping.value();
  77. Iterator<Method> iter = handlerMethodResolver.getHandlerMethods().iterator();
  78. while(iter.hasNext()) {
  79. Method method = iter.next();
  80. RequestMapping methodRequestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
  81. String[] paths = methodRequestMapping.value();
  82. List<String> mappingPaths = buildMappingPaths(namespaces, paths);
  83. Permission permission = AnnotationUtils.findAnnotation(method, Permission.class);
  84. for(String mappingPath : mappingPaths) {
  85. permissionMappings.put(mappingPath, permission);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. protected List<String> buildMappingPaths(String[] namespaces, String[] paths) {
  92. List<String> mappingPaths = CollectionUtil.newArrayList();
  93. for(String namespace : namespaces) {
  94. for(String path : paths) {
  95. if(!namespace.startsWith("/")) {
  96. namespace = "/" + namespace;
  97. }
  98. if(namespace.endsWith("/")) {
  99. namespace = namespace.substring(0, namespace.length()-1);
  100. }
  101. if(!path.startsWith("/")) {
  102. path = "/" + path;
  103. }
  104. if(path.endsWith("/")) {
  105. path = path.substring(0, path.length()-1);
  106. }
  107. mappingPaths.add(namespace + path);
  108. }
  109. }
  110. return mappingPaths;
  111. }
  112. }