egovframework.rte.fdl.security.securedobject.impl.SecuredObjectDAO.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.rte.fdl.security.securedobject.impl.SecuredObjectDAO.java

Source

/*
 * Copyright 2008-2009 MOPAS(Ministry of Public Administration and Security).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package egovframework.rte.fdl.security.securedobject.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.SecurityConfig;
import org.springframework.security.intercept.web.RequestKey;

import egovframework.rte.fdl.security.securedobject.EgovSecuredObjectService;

/**
 * DB? ? ??   DAO ?
 * <p>
 * <b>NOTE:</b> DB ? Secured Object    DAO 
 * default   ? DB ?  ? ? DB   .
 * namedParameterJdbcTemplate   DB  .
 * @author ByungHun Woo
 * @since 2009.06.01
 * @version 1.0
 * @see <pre>
 *  == ?(Modification Information) ==
 *   
 *   ?      ?           
 *  -------    --------    ---------------------------
 *   2009.06.01              ?
 * 
 * </pre>
 */
public class SecuredObjectDAO {

    /**
     * url ?? ?? - Role   default ?.
     */
    public static final String DEF_ROLES_AND_URL_QUERY = "SELECT a.resource_pattern url, b.authority authority "
            + "FROM SECURED_RESOURCES a, SECURED_RESOURCES_ROLE b " + "WHERE a.resource_id = b.resource_id "
            + "AND a.resource_type = 'url' ORDER BY a.sort_order ";

    /**
     * method ?? ?? - Role   default ?.
     */
    public static final String DEF_ROLES_AND_METHOD_QUERY = "SELECT a.resource_pattern method, b.authority authority "
            + "FROM SECURED_RESOURCES a, SECURED_RESOURCES_ROLE b " + "WHERE a.resource_id = b.resource_id "
            + "AND a.resource_type = 'method' ORDER BY a.sort_order ";

    /**
     * pointcut ?? ?? - Role   default
     * ?.
     */
    public static final String DEF_ROLES_AND_POINTCUT_QUERY = "SELECT a.resource_pattern pointcut, b.authority authority "
            + "FROM SECURED_RESOURCES a, SECURED_RESOURCES_ROLE b " + "WHERE a.resource_id = b.resource_id "
            + "AND a.resource_type = 'pointcut' ORDER BY a.sort_order ";

    /**
     *  request  best matching url ?? - Role 
     *  default ?. (Oracle 10g specific)
     */
    public static final String DEF_REGEX_MATCHED_REQUEST_MAPPING_QUERY_ORACLE10G = "SELECT a.resource_pattern uri, b.authority authority "
            + "FROM secured_resources a, secured_resources_role b " + "WHERE a.resource_id = b.resource_id "
            + "AND a.resource_id =  " + " ( SELECT resource_id FROM "
            + "    ( SELECT resource_id, ROW_NUMBER() OVER (ORDER BY sort_order) resource_order FROM secured_resources c "
            + "      WHERE REGEXP_LIKE ( :url, c.resource_pattern ) " + "      AND c.resource_type = 'url' "
            + "      ORDER BY c.sort_order ) " + "   WHERE resource_order = 1 ) ";

    /**
     * Role ? (Hierarchy)   default ?.
     */
    public static final String DEF_HIERARCHICAL_ROLES_QUERY = "SELECT a.child_role child, a.parent_role parent "
            + "FROM ROLES_HIERARCHY a LEFT JOIN ROLES_HIERARCHY b on (a.child_role = b.parent_role) ";

    private String sqlRolesAndUrl;

    private String sqlRolesAndMethod;

    private String sqlRolesAndPointcut;

    private String sqlRegexMatchedRequestMapping;

    private String sqlHierarchicalRoles;

    public SecuredObjectDAO() {
        this.sqlRolesAndUrl = DEF_ROLES_AND_URL_QUERY;
        this.sqlRolesAndMethod = DEF_ROLES_AND_METHOD_QUERY;
        this.sqlRolesAndPointcut = DEF_ROLES_AND_POINTCUT_QUERY;
        this.sqlRegexMatchedRequestMapping = DEF_REGEX_MATCHED_REQUEST_MAPPING_QUERY_ORACLE10G;
        this.sqlHierarchicalRoles = DEF_HIERARCHICAL_ROLES_QUERY;
    }

    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    }

    /**
     * ?  URL   SQL? .
     * @return
     */
    public String getSqlRolesAndUrl() {
        return sqlRolesAndUrl;
    }

    /**
     * ? URL   SQL? .
     * @param sqlRolesAndUrl
     */
    public void setSqlRolesAndUrl(String sqlRolesAndUrl) {
        this.sqlRolesAndUrl = sqlRolesAndUrl;
    }

    public String getSqlRolesAndMethod() {
        return sqlRolesAndMethod;
    }

    public void setSqlRolesAndMethod(String sqlRolesAndMethod) {
        this.sqlRolesAndMethod = sqlRolesAndMethod;
    }

    public String getSqlRolesAndPointcut() {
        return sqlRolesAndPointcut;
    }

    public void setSqlRolesAndPointcut(String sqlRolesAndPointcut) {
        this.sqlRolesAndPointcut = sqlRolesAndPointcut;
    }

    public String getSqlRegexMatchedRequestMapping() {
        return sqlRegexMatchedRequestMapping;
    }

    public void setSqlRegexMatchedRequestMapping(String sqlRegexMatchedRequestMapping) {
        this.sqlRegexMatchedRequestMapping = sqlRegexMatchedRequestMapping;
    }

    public String getSqlHierarchicalRoles() {
        return sqlHierarchicalRoles;
    }

    public void setSqlHierarchicalRoles(String sqlHierarchicalRoles) {
        this.sqlHierarchicalRoles = sqlHierarchicalRoles;
    }

    public LinkedHashMap getRolesAndResources(String resourceType) throws Exception {

        LinkedHashMap resourcesMap = new LinkedHashMap();

        String sqlRolesAndResources;
        boolean isResourcesUrl = true;
        if ("method".equals(resourceType)) {
            sqlRolesAndResources = getSqlRolesAndMethod();
            isResourcesUrl = false;
        } else if ("pointcut".equals(resourceType)) {
            sqlRolesAndResources = getSqlRolesAndPointcut();
            isResourcesUrl = false;
        } else {
            sqlRolesAndResources = getSqlRolesAndUrl();
        }

        List resultList = this.namedParameterJdbcTemplate.queryForList(sqlRolesAndResources, new HashMap());

        Iterator itr = resultList.iterator();
        Map tempMap;
        String preResource = null;
        String presentResourceStr;
        Object presentResource;
        while (itr.hasNext()) {
            tempMap = (Map) itr.next();

            presentResourceStr = (String) tempMap.get(resourceType);
            // url ?  RequestKey ?? key Map?  
            presentResource = isResourcesUrl ? new RequestKey(presentResourceStr) : (Object) presentResourceStr;
            List configList = new LinkedList();

            // ? requestMap ?  Resource ?  Role ?
            //  ?? ? ? , sort_order 
            // resource(Resource) ?   ?
            // Resource ?  Role ? ? ?.
            //   Role List (SecurityConfig) ? ??
            //   ?? 
            if (preResource != null && presentResourceStr.equals(preResource)) {
                List preAuthList = (List) ((ConfigAttributeDefinition) resourcesMap.get(presentResource))
                        .getConfigAttributes();
                Iterator preAuthItr = preAuthList.iterator();
                while (preAuthItr.hasNext()) {
                    SecurityConfig tempConfig = (SecurityConfig) preAuthItr.next();
                    configList.add(tempConfig);
                }
            }

            configList.add(new SecurityConfig((String) tempMap.get("authority")));
            ConfigAttributeDefinition cad = new ConfigAttributeDefinition(configList);

            //  ?? Resource ?   ??? Role  ?
            //  ? resourceKey ?   ? Role 
            //  ? ?.
            resourcesMap.put(presentResource, cad);

            // ? resource ? 
            preResource = presentResourceStr;
        }

        return resourcesMap;
    }

    public LinkedHashMap getRolesAndUrl() throws Exception {
        return getRolesAndResources("url");
    }

    public LinkedHashMap getRolesAndMethod() throws Exception {
        return getRolesAndResources("method");
    }

    public LinkedHashMap getRolesAndPointcut() throws Exception {
        return getRolesAndResources("pointcut");
    }

    public ConfigAttributeDefinition getRegexMatchedRequestMapping(String url) throws Exception {

        ConfigAttributeDefinition attributes = null;

        // best regex matching - best ? Uri ?  Role
        // List , DB ?? ? ??    (ex. hsqldb
        // custom function, Oracle 10g regexp_like )
        Map paramMap = new HashMap();
        paramMap.put("url", url);
        List resultList = this.namedParameterJdbcTemplate.queryForList(getSqlRegexMatchedRequestMapping(),
                paramMap);

        Iterator itr = resultList.iterator();
        Map tempMap;
        List configList = new LinkedList();
        // ? Uri ?  Role ?  configList ? add
        // 
        while (itr.hasNext()) {
            tempMap = (Map) itr.next();
            configList.add(new SecurityConfig((String) tempMap.get("authority")));
        }

        if (configList.size() > 0) {
            attributes = new ConfigAttributeDefinition(configList);
            EgovSecuredObjectService.LOGGER.debug("Request Uri : " + url + ", matched Uri : "
                    + ((Map) resultList.get(0)).get("uri") + ", mapping Roles : " + attributes);
        }

        return attributes;
    }

    public String getHierarchicalRoles() throws Exception {

        List resultList = this.namedParameterJdbcTemplate.queryForList(getSqlHierarchicalRoles(), new HashMap());

        Iterator itr = resultList.iterator();
        StringBuffer concatedRoles = new StringBuffer();
        Map tempMap;
        while (itr.hasNext()) {
            tempMap = (Map) itr.next();
            concatedRoles.append(tempMap.get("child"));
            concatedRoles.append(" > ");
            concatedRoles.append(tempMap.get("parent"));
            concatedRoles.append("\n");
        }

        return concatedRoles.toString();
    }

}