|
16 | 16 |
|
17 | 17 | import controller |
18 | 18 | 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 * |
22 | 26 |
|
23 | 27 | class SpringWikiClientAndServer(PythonConfig): |
24 | 28 | def __init__(self): |
25 | 29 | super(SpringWikiClientAndServer, self).__init__() |
26 | 30 |
|
| 31 | + @Object |
| 32 | + def controller(self): |
| 33 | + return controller.SpringWikiController() |
| 34 | + |
27 | 35 | @Object |
28 | 36 | def read(self): |
29 | 37 | wikiView = view.Springwiki() |
30 | 38 | wikiView.controller = self.controller() |
31 | 39 | return wikiView |
32 | 40 |
|
33 | 41 | @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 | + |
37 | 135 | @Object |
38 | 136 | 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 | + |
|
0 commit comments