2009-07-16 6 views
9

मैं एक विरासत वातावरण में काम कर रहा हूं जहां एक एलडीएपी सर्वर केवल प्रमाणीकरण के लिए उपयोग किया जाता है और इसमें कोई भूमिका नहीं होती है, और डेटाबेस के खिलाफ प्रमाणीकरण किया जाता है जिसमें उपयोगकर्ता-भूमिका मैपिंग होती है, लेकिन कोई पासवर्ड नहीं।एलडीएपी प्रमाणीकरण और जेडीबीसी प्रमाणीकरण के साथ एक टोमकैट दायरे को कार्यान्वित करें

मेरी योजना जेएनडीआईआरएलएएम को विस्तारित करके एक नई टोमकैट दायरे को लागू करना है, और एक एन्सेप्लेटेड जेडीबीसीआरम कॉल करने के लिए भूमिका विधियों को ओवरराइड करना है।

मेरे दायरे server.xml में घोषित किया जाता है:

<Realm className="com.example.LdapJdbcRealm" 
    connectionURL="ldap://ldaphost:389" 
    resourceName="LDAP Auth" 
    userPattern="uid={0}, ou=Portal, dc=example, dc=com" 
    dbConnectionURL="jdbc:oracle:thin:@oracledb:1521:dbname" 
    userTable="db_user" userNameCol="user_id" 
    userRoleTable="db_user_role_xref" roleNameCol="role_id" /> 

यह JNDIRealm & JDBCRealm के लिए मानक संपत्ति के नाम का एक संयोजन है एक छोटे से बदलाव के साथ के रूप में वे दोनों connectionURL का उपयोग करें।

package com.example; 

import org.apache.catalina.Realm; 
import org.apache.catalina.Context; 
import org.apache.catalina.deploy.SecurityConstraint; 
import org.apache.catalina.connector.Request; 
import org.apache.catalina.connector.Response; 
import org.apache.catalina.realm.JNDIRealm; 
import org.apache.catalina.realm.JDBCRealm; 

import java.security.Principal; 
import java.io.IOException; 

public class LdapJdbcRealm extends JNDIRealm implements Realm 
{ 
    private JDBCRealm jdbcRealm = new JDBCRealm(); 

    protected static final String info = "com.example.LdapJdbcRealm/1.0"; 
    protected static final String name = "LdapJdbcRealm"; 

    public String getDbConnectionURL() { 
     return jdbcRealm.getConnectionURL(); 
    } 

    public void setDbConnectionURL(String dbConnectionURL) { 
     jdbcRealm.setConnectionURL(dbConnectionURL); 
    } 

    public String getUserTable() { 
     return jdbcRealm.getUserTable(); 
    } 

    public void setUserTable(String userTable) { 
     jdbcRealm.setUserTable(userTable); 
    } 

    public String getUserNameCol() { 
     return jdbcRealm.getUserNameCol(); 
    } 

    public void setUserNameCol(String userNameCol) { 
     jdbcRealm.setUserNameCol(userNameCol); 
    } 

    public String getUserRoleTable() { 
     return jdbcRealm.getUserRoleTable(); 
    } 

    public void setUserRoleTable(String userRoleTable) { 
     jdbcRealm.setUserRoleTable(userRoleTable); 
    } 

    public String getRoleNameCol() { 
     return jdbcRealm.getRoleNameCol(); 
    } 

    public void setRoleNameCol(String roleNameCol) { 
     jdbcRealm.setRoleNameCol(roleNameCol); 
    } 

    public boolean hasResourcePermission(Request request, 
             Response response, 
             SecurityConstraint[]constraints, 
             Context context) throws IOException 
    { 
     return jdbcRealm.hasResourcePermission(request, response, constraints, context); 
    } 

    public boolean hasRole(Principal principal, String role) { 
     return jdbcRealm.hasRole(principal, role); 
    } 
} 

यह ज्यादातर काम करने के लिए लगता है, प्राधिकरण एलडीएपी, अपेक्षा के अनुरूप जो कोई भूमिका नहीं है से एक प्रधान देता है। वही प्रिंसिपल hasResourcePermission() में प्रवेश करता है और विफल रहता है क्योंकि इसमें इसकी आवश्यकता नहीं होती है। स्पष्ट रूप से मुझे कुछ महत्वपूर्ण कोड याद आ रहा है।

मैं समाधान ढूंढ रहा हूं। मैं जेडीबीसीआरईएल को विस्तारित करने और एलडीएपी प्रमाणीकरण जोड़ने की कोशिश कर सकता हूं, लेकिन यह अधिक काम की तरह लगता है।

मुझे यह भी विश्वास है कि यह एलडीएपी प्रमाणीकरण/डीबी प्रमाणीकरण असामान्य पैटर्न नहीं है। क्या कोई वैकल्पिक समाधान पहले से उपलब्ध है?

यह पर नियंत्रण नहीं है ताकि एलडीएपी या डीबी में पासवर्ड जोड़ने के लिए मेरे नियंत्रण में, इसलिए वे मेरे लिए समाधान नहीं हैं।

+0

हाँ, मैं बिलाव 6.0.18 –

उत्तर

5

आपने टॉमकैट का संस्करण निर्दिष्ट नहीं किया है, इसलिए मैं यहां 6.x के साथ जा रहा हूं।

ऐसा लगता है कि आप hasResourcePermission को जेडीबीसी में findSecurityConstraints और hasUserDataPermission छोड़कर जेएनडीआई के हाथों में छोड़ रहे हैं। आपको उन सभी को या उनमें से कोई भी प्रतिनिधि नहीं देना चाहिए।

अद्यतन: JNDIRealm अपने authenticate() विधि के हिस्से के रूप protected getRoles(DirContext, User) कहता है। आपको इसे ओवरराइड करने और JDBCRealm के getRoles() पर अग्रेषित करने की आवश्यकता है।

+0

मैं findSecurityConstraints अधिरोहित है() और hasUserDataPermission() के साथ काम कर रहा हूँ, हालांकि JDBCRealm कॉल करने के लिए। हालांकि, जेडीबीसीआरम की getRoles() संरक्षित है। आप इसका प्रस्ताव कैसे देते हैं? –

+1

आपके पास 3 विकल्प हैं: प्रतिबिंब का उपयोग करें ('method.setAccessible()' 'संरक्षित 'के साथ मदद करेगा), अपने वास्तविक कार्यान्वयन को' org.apache.catalina.realm' पैकेज पर ले जाएं या पूरी तरह से जेडीबीसीआरएएमएल को छोड़ दें और अपना कोड लिखें डेटाबेस से भूमिकाएं पुनः प्राप्त करें)। – ChssPly76

+0

पैकेज में जा रहे org.apache.catalina.realm बहुत अच्छी तरह से काम किया। मैंने ड्राइवर को निर्दिष्ट करने के लिए भी उपेक्षित किया था, इसलिए मैंने इसके साथ-साथ डीबी उपयोगकर्ता और पासवर्ड के लिए गेटर्स और सेटर्स जोड़े। सब ठीक काम कर रहे हैं, धन्यवाद। –

13

मुझे अभी भी नियमित रूप से आवृत्ति के साथ इस प्रश्न के बारे में ईमेल प्राप्त हैं, इसलिए यहां उपयोग करने के लिए अंतिम उत्पाद है।


LdapJdbcRealm.java

package org.apache.catalina.realm; 

import org.apache.catalina.Realm; 
import org.apache.catalina.Context; 
import org.apache.catalina.connector.Request; 
import org.apache.catalina.connector.Response; 
import org.apache.catalina.deploy.SecurityConstraint; 

import javax.naming.directory.DirContext; 
import java.io.IOException; 
import java.security.Principal; 
import java.util.List; 

/** 
* LdapJdbcRealm is a minimal implementation of a <b>Realm</b> to connect to LDAP 
* for authentication and a database for authorization.<br> 
* <br> 
* Example server.xml configuration fragment:<br> 
* <pre> 
    &lt;Realm className="org.apache.catalina.realm.LdapJdbcRealm" 
     connectionURL="ldap://ldaphost:389" 
     resourceName="LDAP Auth" driverName="oracle.jdbc.driver.OracleDriver" 
     userPattern="uid={0}, ou=Portal, dc=example, dc=com" 
     dbConnectionName="dbuser" dbConnectionPassword="dbpassword" 
     dbConnectionURL="jdbc:oracle:thin:@oracledb:1521:dbname" 
     userTable="users" userNameCol="user_id" 
     userRoleTable="user_role_xref" roleNameCol="role_id" /&gt; 
* </pre> 
* 
* @author Greg Chabala 
* 
* Created by IntelliJ IDEA. 
* User: gchabala 
* Date: Jul 14, 2009 
* Time: 4:56:37 PM 
*/ 
public class LdapJdbcRealm extends JNDIRealm implements Realm 
{ 
    /** 
    * Encapsulated <b>JDBCRealm</b> to do role lookups 
    */ 
    private JDBCRealm jdbcRealm = new JDBCRealm(); 

    /** 
    * Descriptive information about this <b>Realm</b> implementation. 
    */ 
    protected static final String info = "org.apache.catalina.realm.LdapJdbcRealm/1.0"; 

    /** 
    * Descriptive information about this <b>Realm</b> implementation. 
    */ 
    protected static final String name = "LdapJdbcRealm"; 

    /** 
    * Set the all roles mode. 
    * 
    * @param allRolesMode authentication mode 
    */ 
    public void setAllRolesMode(String allRolesMode) { 
     super.setAllRolesMode(allRolesMode); 
     jdbcRealm.setAllRolesMode(allRolesMode); 
    } 

    /** 
    * Return the username to use to connect to the database. 
    * 
    * @return username 
    * @see JDBCRealm#getConnectionName() 
    */ 
    public String getDbConnectionName() { 
     return jdbcRealm.getConnectionName(); 
    } 

    /** 
    * Set the username to use to connect to the database. 
    * 
    * @param dbConnectionName username 
    * @see JDBCRealm#setConnectionName(String) 
    */ 
    public void setDbConnectionName(String dbConnectionName) { 
     jdbcRealm.setConnectionName(dbConnectionName); 
    } 

    /** 
    * Return the password to use to connect to the database. 
    * 
    * @return password 
    * @see JDBCRealm#getConnectionPassword() 
    */ 
    public String getDbConnectionPassword() { 
     return jdbcRealm.getConnectionPassword(); 
    } 

    /** 
    * Set the password to use to connect to the database. 
    * 
    * @param dbConnectionPassword password 
    * @see JDBCRealm#setConnectionPassword(String) 
    */ 
    public void setDbConnectionPassword(String dbConnectionPassword) { 
     jdbcRealm.setConnectionPassword(dbConnectionPassword); 
    } 

    /** 
    * Return the URL to use to connect to the database. 
    * 
    * @return database connection URL 
    * @see JDBCRealm#getConnectionURL() 
    */ 
    public String getDbConnectionURL() { 
     return jdbcRealm.getConnectionURL(); 
    } 

    /** 
    * Set the URL to use to connect to the database. 
    * 
    * @param dbConnectionURL The new connection URL 
    * @see JDBCRealm#setConnectionURL(String) 
    */ 
    public void setDbConnectionURL(String dbConnectionURL) { 
     jdbcRealm.setConnectionURL(dbConnectionURL); 
    } 

    /** 
    * Return the JDBC driver that will be used. 
    * 
    * @return driver classname 
    * @see JDBCRealm#getDriverName() 
    */ 
    public String getDriverName() { 
     return jdbcRealm.getDriverName(); 
    } 

    /** 
    * Set the JDBC driver that will be used. 
    * 
    * @param driverName The driver name 
    * @see JDBCRealm#setDriverName(String) 
    */ 
    public void setDriverName(String driverName) { 
     jdbcRealm.setDriverName(driverName); 
    } 

    /** 
    * Return the table that holds user data.. 
    * 
    * @return table name 
    * @see JDBCRealm#getUserTable() 
    */ 
    public String getUserTable() { 
     return jdbcRealm.getUserTable(); 
    } 

    /** 
    * Set the table that holds user data. 
    * 
    * @param userTable The table name 
    * @see JDBCRealm#setUserTable(String) 
    */ 
    public void setUserTable(String userTable) { 
     jdbcRealm.setUserTable(userTable); 
    } 

    /** 
    * Return the column in the user table that holds the user's name. 
    * 
    * @return username database column name 
    * @see JDBCRealm#getUserNameCol() 
    */ 
    public String getUserNameCol() { 
     return jdbcRealm.getUserNameCol(); 
    } 

    /** 
    * Set the column in the user table that holds the user's name. 
    * 
    * @param userNameCol The column name 
    * @see JDBCRealm#setUserNameCol(String) 
    */ 
    public void setUserNameCol(String userNameCol) { 
     jdbcRealm.setUserNameCol(userNameCol); 
    } 

    /** 
    * Return the table that holds the relation between user's and roles. 
    * 
    * @return user role database table name 
    * @see JDBCRealm#getUserRoleTable() 
    */ 
    public String getUserRoleTable() { 
     return jdbcRealm.getUserRoleTable(); 
    } 

    /** 
    * Set the table that holds the relation between user's and roles. 
    * 
    * @param userRoleTable The table name 
    * @see JDBCRealm#setUserRoleTable(String) 
    */ 
    public void setUserRoleTable(String userRoleTable) { 
     jdbcRealm.setUserRoleTable(userRoleTable); 
    } 

    /** 
    * Return the column in the user role table that names a role. 
    * 
    * @return role column name 
    * @see JDBCRealm#getRoleNameCol() 
    */ 
    public String getRoleNameCol() { 
     return jdbcRealm.getRoleNameCol(); 
    } 

    /** 
    * Set the column in the user role table that names a role. 
    * 
    * @param roleNameCol The column name 
    * @see JDBCRealm#setRoleNameCol(String) 
    */ 
    public void setRoleNameCol(String roleNameCol) { 
     jdbcRealm.setRoleNameCol(roleNameCol); 
    } 

    @Override 
    public SecurityConstraint[] findSecurityConstraints(Request request, Context context) 
    { 
     return jdbcRealm.findSecurityConstraints(request, context); 
    } 

    @Override 
    public boolean hasUserDataPermission(Request request, Response response, 
             SecurityConstraint []constraints) throws IOException 
    { 
     return jdbcRealm.hasUserDataPermission(request, response, constraints); 
    } 

    @Override 
    public boolean hasResourcePermission(Request request, Response response, 
             SecurityConstraint[]constraints, 
             Context context) throws IOException 
    { 
     return jdbcRealm.hasResourcePermission(request, response, constraints, context); 
    } 

    @Override 
    public boolean hasRole(Principal principal, String role) { 
     return jdbcRealm.hasRole(principal, role); 
    } 

    /** 
    * Return a List of roles associated with the given User. If no roles 
    * are associated with this user, a zero-length List is returned. 
    * 
    * @param context unused. JDBC does not need this field. 
    * @param user The User to be checked 
    * @return list of role names 
    * 
    * @see JNDIRealm#getRoles(DirContext, User) 
    * @see JDBCRealm#getRoles(String) 
    */ 
    @Override 
    protected List<String> getRoles(DirContext context, User user) 
    { 
     return jdbcRealm.getRoles(user.username); 
    } 
} 
+0

हाय, मैंने इसे github पर रखा है: https://github.com/rasenderhase/nest-tomcat-realms/ मैं पुल अनुरोधों के लिए खुश हूं। – andy

संबंधित मुद्दे