鍦ㄤ笂涓綃囦腑鎴戜滑鐮旂┒浜嗗浣曞疄鐜癝pringSecurity涓璊sp Tag鐨?lt;security:authorize ifAllGranted="ROLE_SUPERVISOR">鐨勫姛鑳姐傝繖涓嬈℃垜浠竴璧風爺絀朵竴涓嬪浣曞疄鐜板湪Tapestry5.1涓坊鍔犱竴涓狥ilter鏉ュ鎵鏈夌殑鎿嶄綔榪涜鏉冮檺鐨勮繃婊ゆ帶鍒躲?br />
鍦⊿pringSecurity涓紝鎴戜滑涓鑸槸鍦╝pplication-context.xml涓紝娣誨姞涓涓猄pringSecurity鐨凢ilter錛岀劧鍚庡湪鍙﹀涓涓獂ml涓緇嗛厤緗浣曟牴鎹甎rl鐨勮鍒欒繘琛屾潈闄愮殑鎺у埗銆傝孴apestry鐨勫摬瀛︽槸灝介噺鍑忓皯Xml涓殑閰嶇疆錛堝叾IOC瀹瑰櫒涔熷熀鏈笂鏄熼壌Guice鑰屼笉Spring鐨勶級錛屾墍浠ユ垜浠篃鏄湪浠g爜涓疄鐜版潈闄愯鍒欑殑鎺у埗銆?br />
鎬諱綋涓婃潵鐪嬶紝鍙互鐢ㄤ袱縐嶆柟寮忔潵瀹炵幇url瑙勫垯錛屼竴縐嶆槸Request綰у埆鐨凢ilter錛屼竴縐嶆槸欏甸潰緇勪歡綰у埆鐨凢ilter錛屽鏋滄槸Request綰у埆鐨勮瘽錛屽彲浠ヤ粠Request瀵硅薄涓幏鍙朥rl璺緞錛岃繖鏍峰氨涓嶴pringSecurity鍩烘湰涓鏍蜂簡銆傛湰鏂囦富瑕佷粙緇嶉〉闈㈢粍浠剁駭鍒殑Filter錛屼粠涓垜浠篃鍙互浣撲細鍒癟apestry5.1涓殑IOC瀹瑰櫒鐨勫己澶у拰渚垮埄銆?/p>
榪欏氨鏄疐ilter鐨勪唬鐮侊紝榪欎釜Filter蹇呴』瀹炵幇ComponentRequestFilter鎺ュ彛銆傚煎緱娉ㄦ剰鐨勬槸鍏舵瀯閫犲嚱鏁版墍闇瑕佺敤鍒扮殑4涓弬鏁幫紝榪?涓弬鏁伴兘鏄疶apestry5鏈韓鑷湁鐨勬湇鍔★紝鎵浠ユ垜浠粈涔堜篃涓嶇敤鍋氾紝Tapestry5鑷姩浼氬皢鏈嶅姟鐨勫疄渚嬫敞鍏ヨ繘鏉ワ紝榪欏氨鏄疶apestry-IOC鐨勫▉鍔涖?br />
ComponentRequestFilter鎺ュ彛涓鍏辨湁4涓柟娉曢渶瑕佸疄鐜幫紝鍏蜂綋浠g爜濡備笅錛?/p>
1 public class RequiresLoginFilter implements ComponentRequestFilter {
2
3 private final PageRenderLinkSource renderLinkSource;
4
5 private final ComponentSource componentSource;
6
7 private final Response response;
8
9 private final ApplicationStateManager appStateManager;
10
11 public RequiresLoginFilter(PageRenderLinkSource renderLinkSource,
12 ComponentSource componentSource, Response response,
13 ApplicationStateManager appStateManager
14 ) {
15 this.renderLinkSource = renderLinkSource;
16 this.componentSource = componentSource;
17 this.response = response;
18 this.appStateManager = appStateManager;
19 }
20
21 public void handleComponentEvent(
22 ComponentEventRequestParameters parameters,
23 ComponentRequestHandler handler) throws IOException {
24
25 if (dispatchedToLoginPage(parameters.getActivePageName())) {
26 return;
27 }
28
29 handler.handleComponentEvent(parameters);
30
31 }
32
33 public void handlePageRender(PageRenderRequestParameters parameters,
34 ComponentRequestHandler handler) throws IOException {
35 if (dispatchedToLoginPage(parameters.getLogicalPageName())) {
36 return;
37 }
38 handler.handlePageRender(parameters);
39
40 }
41
42 private boolean dispatchedToLoginPage(String pageName) {
43 Component page = componentSource.getPage(pageName);
44
45 if (page.getClass().isAnnotationPresent(RequiresLogin.class)) {
46 if ( ! appStateManager.exists(Authentication.class)) {
47 redirect();
48 return true;
49 }
50 Authentication auth = appStateManager.get(Authentication.class);
51 if ( auth == null ) {
52 redirect();
53 return true;
54 }
55
56 if ( ! auth.isLoggedIn()) {
57 redirect();
58 return true;
59 }
60
61 RequiresLogin requireLogin = page.getClass().getAnnotation(
62 RequiresLogin.class);
63 String ifNotGranted = requireLogin.ifNotGranted();
64 String ifAllGranted = requireLogin.ifAllGranted();
65 String ifAnyGranted = requireLogin.ifAnyGranted();
66 boolean permitted = auth.checkPermission(ifNotGranted, ifAllGranted, ifAnyGranted);
67 if ( ! permitted ) {
68 return true;
69 }
70 }
71
72 return false;
73 }
74
75 private void redirect() {
76 Link link = renderLinkSource.createPageRenderLink("Logout");
77
78 try {
79 response.sendRedirect(link);
80 } catch (Exception e) {
81 }
82 }
83
84 }
鍦–omponentRequestFilter涓紝鎴戜滑鏃犳硶浣跨敤@SessionState娉ㄨВ鏉ョ洿鎺ユ敞鍏ession涓殑鍙橀噺錛屼絾鏄垜浠彲浠ラ氳繃ApplicationStateManager鏉ュ彇寰椼?/p>
鐜板湪鎴戜滑闇瑕佹妸鍒氬畾涔夌殑Filter娉ㄥ唽鍒扮郴緇熶腑錛屽緢綆鍗曪紝鍙鍦ˋppModule涓坊鍔犱互涓嬪嚱鏁板氨琛屼簡錛?/p>
1 public static void contributeComponentRequestHandler(
2 OrderedConfiguration<ComponentRequestFilter> configuration) {
3 configuration.addInstance("RequiresLogin", RequiresLoginFilter.class);
4 }
5
浠庢湰渚嬪瓙涓垜浠彲浠ョ湅鍒癟apesty Ioc瀹瑰櫒浣跨敤鐨勪究鍒╂э紝涔熻璇嗗埌浜咺oc瀹瑰櫒鍦═apestry浣撶郴涓殑閲嶈鎬?img src ="http://m.tkk7.com/usherlight/aggbug/312016.html" width = "1" height = "1" />
]]>