X Tutup
Skip to content

Commit 1f7be03

Browse files
committed
SESPRINGPYTHONPY-100: Fixed up noxml to work correctly. Also fixed logout function.
git-svn-id: https://src.springframework.org/svn/se-springpython-py/sandbox/gregturnquist/sespringpythonpy-100@708 ce8fead1-4192-4296-8608-a705134b927f
1 parent 7089b55 commit 1f7be03

File tree

3 files changed

+140
-12
lines changed

3 files changed

+140
-12
lines changed

samples/springwiki/noxml.py

Lines changed: 126 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,143 @@
1616

1717
import controller
1818
import view
19-
from springpython.config import PythonConfig
20-
from springpython.config import Object
21-
from springpython.security.web import FilterChainProxy
19+
20+
from springpython.config import *
21+
from springpython.security.web import *
22+
from springpython.security.providers import *
23+
from springpython.security.providers.dao import *
24+
from springpython.security.userdetails import *
25+
from springpython.security.vote import *
2226

2327
class SpringWikiClientAndServer(PythonConfig):
2428
def __init__(self):
2529
super(SpringWikiClientAndServer, self).__init__()
2630

31+
@Object
32+
def controller(self):
33+
return controller.SpringWikiController()
34+
2735
@Object
2836
def read(self):
2937
wikiView = view.Springwiki()
3038
wikiView.controller = self.controller()
3139
return wikiView
3240

3341
@Object
34-
def controller(self):
35-
return controller.SpringWikiController()
36-
42+
def userDetailsService2(self):
43+
service = InMemoryUserDetailsService()
44+
service.user_dict = {"writer": ("comein", ["VET_ANY"], True)}
45+
return service
46+
47+
@Object
48+
def plainEncoder(self):
49+
return PlaintextPasswordEncoder()
50+
51+
@Object
52+
def plainAuthenticationProvider(self):
53+
provider = DaoAuthenticationProvider()
54+
provider.user_details_service = self.userDetailsService2()
55+
provider.password_encoder = self.plainEncoder()
56+
return provider
57+
58+
59+
@Object
60+
def authenticationManager(self):
61+
manager = AuthenticationManager()
62+
manager.auth_providers = [self.plainAuthenticationProvider()]
63+
return manager
64+
65+
@Object
66+
def authenticationProcessingFilter(self):
67+
filter = AuthenticationProcessingFilter()
68+
filter.auth_manager = self.authenticationManager()
69+
filter.alwaysReauthenticate = False
70+
return filter
71+
72+
@Object
73+
def cherrypySessionStrategy(self):
74+
return CP3SessionStrategy()
75+
76+
@Object
77+
def redirectStrategy(self):
78+
return CP3RedirectStrategy()
79+
80+
@Object
81+
def httpContextFilter(self):
82+
filter = HttpSessionContextIntegrationFilter()
83+
filter.sessionStrategy = self.cherrypySessionStrategy()
84+
return filter
85+
86+
@Object
87+
def authenticationProcessingFilterEntryPoint(self):
88+
filter_point = AuthenticationProcessingFilterEntryPoint()
89+
filter_point.loginFormUrl = "/login"
90+
filter_point.redirectStrategy = self.redirectStrategy()
91+
return filter_point
92+
@Object
93+
def accessDeniedHandler(self):
94+
handler = SimpleAccessDeniedHandler()
95+
handler.errorPage = "/accessDenied"
96+
handler.redirectStrategy = self.redirectStrategy()
97+
return handler
98+
99+
@Object
100+
def exceptionTranslationFilter(self):
101+
filter = ExceptionTranslationFilter()
102+
filter.authenticationEntryPoint = self.authenticationProcessingFilterEntryPoint()
103+
filter.accessDeniedHandler = self.accessDeniedHandler()
104+
return filter
105+
106+
@Object
107+
def filterSecurityInterceptor(self):
108+
interceptor = FilterSecurityInterceptor()
109+
interceptor.validate_config_attributes = False
110+
interceptor.auth_manager = self.authenticationManager()
111+
interceptor.access_decision_mgr = self.accessDecisionManager()
112+
interceptor.sessionStrategy = self.cherrypySessionStrategy()
113+
interceptor.obj_def_source = [("/.*", ["VET_ANY", "CUSTOMER_ANY"])]
114+
return interceptor
115+
116+
@Object
117+
def vetRoleVoter(self):
118+
voter = RoleVoter()
119+
voter.role_prefix = "VET"
120+
return voter
121+
122+
@Object
123+
def customerRoleVoter(self):
124+
voter = RoleVoter()
125+
voter.role_prefix = "CUSTOMER"
126+
return voter
127+
128+
@Object
129+
def accessDecisionManager(self):
130+
policy = AffirmativeBased()
131+
policy.allow_if_all_abstain = False
132+
policy.access_decision_voters = [self.vetRoleVoter(), self.customerRoleVoter()]
133+
return policy
134+
37135
@Object
38136
def filterChainProxy(self):
39-
proxy = FilterChainProxy()
40-
proxy.filterInvocationDefinitionSource = [("/.*", [])]
41-
return proxy
137+
proxy = CP3FilterChainProxy()
138+
proxy.filterInvocationDefinitionSource = [
139+
("/login.*",
140+
["httpContextFilter"]),
141+
("/.*",
142+
["httpContextFilter",
143+
"exceptionTranslationFilter",
144+
"authenticationProcessingFilter",
145+
"filterSecurityInterceptor"]
146+
)]
147+
return proxy
148+
149+
@Object
150+
def loginForm(self):
151+
form = view.CherryPyAuthenticationForm()
152+
form.filter = self.authenticationProcessingFilter()
153+
form.controller = self.controller()
154+
form.authenticationManager = self.authenticationManager()
155+
form.redirectStrategy = self.redirectStrategy()
156+
form.httpContextFilter = self.httpContextFilter()
157+
return form
158+

samples/springwiki/springwiki-noxml.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from springpython.config import PyContainerConfig
2222
from springpython.config import PythonConfig
2323
from springpython.context import ApplicationContext
24+
from springpython.security.context import SecurityContextHolder
2425

2526
port = 8003
2627

@@ -38,9 +39,15 @@
3839
logger.addHandler(ch)
3940

4041
applicationContext = ApplicationContext(noxml.SpringWikiClientAndServer())
42+
filterChainProxy = applicationContext.get_object("filterChainProxy")
43+
44+
SecurityContextHolder.setStrategy(SecurityContextHolder.MODE_GLOBAL)
45+
SecurityContextHolder.getContext()
4146

4247
# Use a configuration file for server-specific settings.
43-
conf = {"/": {"tools.staticdir.root": os.getcwd()},
48+
conf = {"/": {"tools.staticdir.root": os.getcwd(),
49+
"tools.sessions.on": True,
50+
"tools.filterChainProxy.on": True},
4451
"/images": {"tools.staticdir.on": True,
4552
"tools.staticdir.dir": "images"},
4653
"/html": {"tools.staticdir.on": True,
@@ -68,8 +75,11 @@
6875
}
6976

7077
cherrypy.config.update({'server.socket_port': port})
71-
72-
cherrypy.tree.mount(applicationContext.get_object(name = "read"), '/', config=conf)
78+
79+
app = applicationContext.get_object(name = "read")
80+
app.login = applicationContext.get_object(name = "loginForm")
81+
82+
cherrypy.tree.mount(app, '/', config=conf)
7383

7484
cherrypy.engine.start()
7585
cherrypy.engine.block()

samples/springwiki/view.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def index(self, fromPage="/", login="", password="", errorMsg=""):
131131
def logout(self):
132132
"""Replaces current authentication token, with an empty, non-authenticated one."""
133133
self.filter.logout()
134+
self.httpContextFilter.saveContext()
134135
raise cherrypy.HTTPRedirect("/")
135136

136137
def attemptAuthentication(self, username, password):

0 commit comments

Comments
 (0)
X Tutup