Search in sources :

Example 6 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project disconf by knightliao.

the class RoleResourceMgrImpl method getAllAsMap.

/**
     * @return
     */
@Override
@Cacheable(value = "${role_res_cache_name}")
public Map<String, Map<RequestMethod, List<Integer>>> getAllAsMap() {
    Map<String, Map<RequestMethod, List<Integer>>> infoMap = new HashMap<String, Map<RequestMethod, List<Integer>>>();
    LOG.info("Querying role_resource table to get all...");
    List<RoleResource> roleResList = roleResDao.findAll();
    // 遍历列表,把数据按<url, <method, List<roleId>>>的形式加到infoMap
    for (RoleResource roleRes : roleResList) {
        String urlPattern = roleRes.getUrlPattern();
        if (!urlPattern.endsWith(RoleResourceConstant.URL_SPLITOR)) {
            urlPattern += RoleResourceConstant.URL_SPLITOR;
        }
        // LOG.info(urlPattern);
        Map<RequestMethod, List<Integer>> value = infoMap.get(urlPattern);
        if (value == null) {
            value = new HashMap<RequestMethod, List<Integer>>();
            infoMap.put(urlPattern, value);
        }
        updateMethodMap(value, roleRes.getRoleId(), roleRes.getMethodMask());
    }
    return infoMap;
}
Also used : HashMap(java.util.HashMap) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) RoleResource(com.baidu.disconf.web.service.roleres.bo.RoleResource) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 7 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project disconf by knightliao.

the class RoleResourceMgrImpl method checkUserPermission.

/**
     * @param pattern
     * @param method
     *
     * @return
     */
@Override
public boolean checkUserPermission(String pattern, RequestMethod method) {
    // 获取用户角色
    Visitor visitor = ThreadContext.getSessionVisitor();
    if (visitor == null) {
        return false;
    }
    String urlPattarn = pattern;
    if (!urlPattarn.endsWith(RoleResourceConstant.URL_SPLITOR)) {
        urlPattarn += RoleResourceConstant.URL_SPLITOR;
    }
    Integer roleId = visitor.getRoleId();
    Map<String, Map<RequestMethod, List<Integer>>> roleResMap = getAllAsMap();
    Map<RequestMethod, List<Integer>> methodMap = roleResMap.get(urlPattarn);
    if (methodMap == null) {
        return false;
    }
    List<Integer> roleIdList = methodMap.get(method);
    if (roleIdList == null) {
        return false;
    }
    if (!roleIdList.contains(roleId)) {
        return false;
    }
    return true;
}
Also used : Visitor(com.baidu.disconf.web.service.user.dto.Visitor) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerMapping method initCorsConfiguration.

@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
    HandlerMethod handlerMethod = createHandlerMethod(handler, method);
    Class<?> beanType = handlerMethod.getBeanType();
    CrossOrigin typeAnnotation = AnnotatedElementUtils.findMergedAnnotation(beanType, CrossOrigin.class);
    CrossOrigin methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, CrossOrigin.class);
    if (typeAnnotation == null && methodAnnotation == null) {
        return null;
    }
    CorsConfiguration config = new CorsConfiguration();
    updateCorsConfig(config, typeAnnotation);
    updateCorsConfig(config, methodAnnotation);
    if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
        for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
            config.addAllowedMethod(allowedMethod.name());
        }
    }
    return config.applyPermitDefaultValues();
}
Also used : CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 9 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerMapping method initCorsConfiguration.

@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
    HandlerMethod handlerMethod = createHandlerMethod(handler, method);
    Class<?> beanType = handlerMethod.getBeanType();
    CrossOrigin typeAnnotation = AnnotatedElementUtils.findMergedAnnotation(beanType, CrossOrigin.class);
    CrossOrigin methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, CrossOrigin.class);
    if (typeAnnotation == null && methodAnnotation == null) {
        return null;
    }
    CorsConfiguration config = new CorsConfiguration();
    updateCorsConfig(config, typeAnnotation);
    updateCorsConfig(config, methodAnnotation);
    if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
        for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
            config.addAllowedMethod(allowedMethod.name());
        }
    }
    return config.applyPermitDefaultValues();
}
Also used : CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 10 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerMappingTests method assertComposedAnnotationMapping.

private RequestMappingInfo assertComposedAnnotationMapping(String methodName, String path, RequestMethod requestMethod) throws Exception {
    Class<?> clazz = ComposedAnnotationController.class;
    Method method = clazz.getMethod(methodName);
    RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, clazz);
    assertNotNull(info);
    Set<String> paths = info.getPatternsCondition().getPatterns();
    assertEquals(1, paths.size());
    assertEquals(path, paths.iterator().next());
    Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
    assertEquals(1, methods.size());
    assertEquals(requestMethod, methods.iterator().next());
    return info;
}
Also used : RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Method(java.lang.reflect.Method) RequestMethod(org.springframework.web.bind.annotation.RequestMethod)

Aggregations

RequestMethod (org.springframework.web.bind.annotation.RequestMethod)11 Map (java.util.Map)3 HandlerMethod (org.springframework.web.method.HandlerMethod)3 Visitor (com.baidu.disconf.web.service.user.dto.Visitor)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Test (org.junit.Test)2 CrossOrigin (org.springframework.web.bind.annotation.CrossOrigin)2 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)2 RequestMappingInfo (org.springframework.web.servlet.mvc.method.RequestMappingInfo)2 RoleResource (com.baidu.disconf.web.service.roleres.bo.RoleResource)1 AccessDeniedException (com.baidu.dsp.common.exception.AccessDeniedException)1 TreeSet (java.util.TreeSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Around (org.aspectj.lang.annotation.Around)1 Cacheable (org.springframework.cache.annotation.Cacheable)1 HttpMethod (org.springframework.http.HttpMethod)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1