Announcement Announcement Module
Collapse
No announcement yet.
Spring Python trouble on running an example Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Python trouble on running an example

    Hello. I recently read 'Spring Python 1.1' by Greg L. Turnquist. to learn spring framework on python.
    While learning, I tried a few examples introduced in the book and I bumped into a trouble on chapter 8, "Case Study I - Intergrating Spring Python with your Web Application."

    And, I get this error below. I don't have a clue even on google..
    500 Internal Server Error

    The server encountered an unexpected condition which prevented it from fulfilling the request.

    Traceback (most recent call last): File "c:\Python27\lib\site-packages\cherrypy\_cprequest.py", line 633, in respond self.namespaces(self.config) File "c:\Python27\lib\site-packages\cherrypy\lib\reprconf.py", line 115, in __call__ exit(None, None, None) File "c:\Python27\lib\site-packages\cherrypy\_cptools.py", line 446, in __exit__ tool = getattr(self, name) AttributeError: 'Toolbox' object has no attribute 'filterChainProxy'
    Powered by CherryPy 3.2.4
    I figured out that CherryPy doesn't have the attribute 'filterChainProxy' in cherrypy.tools. However, the example code in main2.py below tries to use "tools.filterChainProxy.on" and that's the reason as far as i know.

    I cannot find any solution for this issue on google. Why in the first place the author made this code using tools.filterChainProxy? Was it supported back then, and now it's deprecated? Or, why else? I looked up in the documentation page of cherrypy and still no clue.

    Please help me with the code below (I put the related parts in red):
    <main2.py>
    import cherrypy
    import os
    from springpython.context import ApplicationContext
    from springpython.security.context import *

    from ctx2 import *

    if __name__ == '__main__':
    cherrypy.config.update({'server.socket_port': 8009})

    ctx = ApplicationContext(SpringBankAppContext())

    SecurityContextHolder.setStrategy(SecurityContextH older.MODE_GLOBAL)
    SecurityContextHolder.getContext()

    conf = {"/": {"tools.sessions.on":True,
    "tools.filterChainProxy.on":True}}

    cherrypy.tree.mount(
    ctx.get_object("view"),
    '/',
    config=conf)

    cherrypy.engine.start()
    cherrypy.engine.block()
    <ctx2.py>
    from springpython.config import PythonConfig, Object
    from springpython.security.providers import *
    from springpython.security.providers.dao import *
    from springpython.security.userdetails import *
    from springpython.security.vote import *
    from springpython.security.web import *
    from springpython.security.cherrypy3 import *

    from app2 import *

    class SpringBankAppContext(PythonConfig):
    def __init__(self):
    PythonConfig.__init__(self)

    @Object
    def view(self):
    view = SpringBankView()
    view.auth_provider = self.auth_provider()
    view.filter = self.auth_processing_filter()
    view.http_context_filter = self.httpSessionContextIntegrationFilter()
    return view

    @Object
    def filterChainProxy(self):
    return CP3FilterChainProxy(filterInvocationDefinitionSour ce =
    [
    ("/login.*", ["httpSessionContextIntegrationFilter"]),
    ("/.*", ["httpSessionContextIntegrationFilter",
    "exception_translation_filter",
    "auth_processing_filter",
    "filter_security_interceptor"])
    ])

    @Object
    def httpSessionContextIntegrationFilter(self):
    filter = HttpSessionContextIntegrationFilter()
    filter.sessionStrategy = self.session_strategy()
    return filter

    @Object
    def session_strategy(self):
    return CP3SessionStrategy()

    @Object
    def exception_translation_filter(self):
    filter = ExceptionTranslationFilter()
    filter.authenticationEntryPoint = self.auth_filter_entry_pt()
    filter.accessDeniedHandler = self.accessDeniedHandler()
    return filter

    @Object
    def auth_filter_entry_pt(self):
    filter = AuthenticationProcessingFilterEntryPoint()
    filter.loginFormUrl = "/login"
    filter.redirectStrategy = self.redirectStrategy()
    return filter

    @Object
    def accessDeniedHandler(self):
    handler = SimpleAccessDeniedHandler()
    handler.errorPage = "/accessDenied"
    handler.redirectStrategy = self.redirectStrategy()
    return handler

    @Object
    def redirectStrategy(self):
    return CP3RedirectStrategy()

    @Object
    def auth_processing_filter(self):
    filter = AuthenticationProcessingFilter()
    filter.auth_manager = self.auth_manager()
    filter.alwaysReauthenticate = False
    return filter

    @Object
    def auth_manager(self):
    auth_manager = AuthenticationManager()
    auth_manager.auth_providers = [self.auth_provider()]
    return auth_manager

    @Object
    def auth_provider(self):
    provider = DaoAuthenticationProvider()
    provider.user_details_service = self.user_details_service()
    provider.password_encoder = PlaintextPasswordEncoder()
    return provider

    @Object
    def user_details_service(self):
    user_details_service = InMemoryUserDetailsService()
    user_details_service.user_dict = {
    "alice": ("alicespassword",["ROLE_CUSTOMER"], True),
    "bob": ("bobspassword", ["ROLE_MGR"], True),
    "carol": ("carolspassword", ["ROLE_SUPERVISOR"], True)
    }
    return user_details_service


    @Object
    def filter_security_interceptor(self):
    filter = FilterSecurityInterceptor()
    filter.auth_manager = self.auth_manager()
    filter.access_decision_mgr = self.access_decision_mgr()
    filter.sessionStrategy = self.session_strategy()
    filter.obj_def_source = [
    ("/.*", ["ROLE_CUSTOMER", "ROLE_MGR", "ROLE_SUPERVISOR"])
    ]
    return filter

    @Object
    def access_decision_mgr(self):
    access_decision_mgr = AffirmativeBased()
    access_decision_mgr.allow_if_all_abstain = False
    access_decision_mgr.access_decision_voters = [RoleVoter()]
    return access_decision_mgr
    <app2.py>
    import cherrypy

    from springpython.security import *
    from springpython.security.providers import *
    from springpython.security.context import *

    class SpringBankView(object):

    def __init__(self):
    self.filter = None
    self.auth_provider = None
    self.http_context_filter = None

    @cherrypy.expose
    def index(self):
    return """
    Welcome to SpringBank!
    <p>
    <p>
    <a href="logout">Logout</a href>
    """

    @cherrypy.expose
    def login(self, from_page="/", login="", password="", error_msg=""):
    if login != "" and password != "":
    try:
    self.attempt_auth(login, password)
    raise cherrypy.HTTPRedirect(from_page)
    except AuthenticationException, e:
    raise cherrypy.HTTPRedirect(
    "?login=%s&error_msg=Username/password failure"
    % login)

    return """
    %s<p>
    <form method="POST" action="">
    <table>
    <tr>
    <td>Login:</td>
    <td><input type="text" name="login"
    value="%s"/></td>
    </tr>
    <tr>
    <td>Password:</td>
    <td><input type="password" name="password"/></td>
    </tr>
    </table>
    <input type="hidden" name="from_page" value="%s"/><br/>
    <input type="submit"/>
    </form>
    """ % (error_msg, login, from_page)

    def attempt_auth(self, username, password):
    token = UsernamePasswordAuthenticationToken(username, password)
    SecurityContextHolder.getContext().authentication = \
    self.auth_provider.authenticate(token)
    self.http_context_filter.saveContext()

    @cherrypy.expose
    def logout(self):
    self.filter.logout()
    self.http_context_filter.saveContext()
    raise cherrypy.HTTPRedirect("/")
    The full source code is found in the url:
    https://www.packtpub.com/sites/defau.../0660_Code.zip

    Thanks in advance!
    Last edited by Jin Choi; Dec 21st, 2013, 08:06 PM.

  • #2
    I resolved this problem myself outta several experiments and let me share it with you.
    Any issues while running the example demos are mostly from version conflicts.

    My platform is Ubuntu 13.10

    Try to use virtualenv if any possible.
    Python 2.7.x works fine with springpython 1.1.0.FINAL.
    CherryPy 3.1.2 is what the writer of the book uses, so try to install that specific version.
    Mysql server and client are fine for version 5.x
    Pyro seems fine with version of 3.x (I use 3.16)


    overall: (below is not for virualenv)
    $ pip install springpython==1.1.0.FINAL CherryPy==3.1.2 Pyro==3.16
    $ sudo apt-get -y install mysql-server mysql-client python-mysqldb
    Last edited by Jin Choi; Dec 27th, 2013, 10:15 AM.

    Comment

    Working...
    X