1 28 29 package com.caucho.server.security; 30 31 import com.caucho.server.dispatch.ErrorFilterChain; 32 import com.caucho.server.dispatch.FilterChainBuilder; 33 import com.caucho.server.dispatch.ForwardFilterChain; 34 import com.caucho.server.dispatch.Invocation; 35 import com.caucho.server.webapp.WebApp; 36 import com.caucho.util.L10N; 37 38 import javax.servlet.FilterChain ; 39 import javax.servlet.RequestDispatcher ; 40 import javax.servlet.http.HttpServletResponse ; 41 import java.util.ArrayList ; 42 import java.util.HashMap ; 43 44 47 public class ConstraintManager extends FilterChainBuilder { 48 private static L10N L = new L10N(ConstraintManager.class); 49 50 private ArrayList <SecurityConstraint> _constraints 51 = new ArrayList <SecurityConstraint>(); 52 53 public void addConstraint(SecurityConstraint constraint) 54 { 55 _constraints.add(constraint); 56 } 57 58 public FilterChainBuilder getFilterBuilder() 59 { 60 return this; 61 67 } 68 69 75 public FilterChain build(FilterChain next, Invocation invocation) 76 { 77 String uri = invocation.getContextURI(); 78 79 WebApp app = invocation.getWebApp(); 80 if (app == null) 81 return next; 82 83 String lower = uri.toLowerCase(); 84 85 if (lower.startsWith("/web-inf") || 86 lower.startsWith("/meta-inf")) { 87 return new ErrorFilterChain(HttpServletResponse.SC_NOT_FOUND); 88 } 89 90 ArrayList <AbstractConstraint> constraints; 91 constraints = new ArrayList <AbstractConstraint>(); 92 93 HashMap <String ,AbstractConstraint[]> methodMap; 94 methodMap = new HashMap <String ,AbstractConstraint[]>(); 95 96 if (_constraints != null) { 97 for (int i = 0; i < _constraints.size(); i++) { 98 SecurityConstraint constraint = _constraints.get(i); 99 100 if (constraint.isMatch(uri)) { 101 AbstractConstraint absConstraint = constraint.getConstraint(); 102 103 if (absConstraint != null) { 104 ArrayList <String > methods = constraint.getMethods(uri); 105 106 for (int j = 0; methods != null && j < methods.size(); j++) { 107 String method = methods.get(j); 108 109 AbstractConstraint []methodList = methodMap.get(method); 110 111 if (methodList == null) 112 methodList = absConstraint.toArray(); 113 else { 114 } 116 117 methodMap.put(method, methodList); 118 } 119 120 if (methods == null || methods.size() == 0) { 121 AbstractConstraint []constArray = absConstraint.toArray(); 122 for (int k = 0; k < constArray.length; k++) 123 constraints.add(constArray[k]); 124 } 125 } 126 127 break; 128 } 129 } 130 } 131 132 if (uri.endsWith("/j_security_check") && 133 app.getLogin() instanceof FormLogin) { 134 RequestDispatcher disp = app.getNamedDispatcher("j_security_check"); 135 if (disp == null) 136 throw new IllegalStateException (L.l("j_security_check is an undefined servlet")); 137 138 next = new ForwardFilterChain(disp); 139 } 140 141 if (constraints.size() != 0 || methodMap.size() > 0) { 142 SecurityFilterChain filterChain = new SecurityFilterChain(next); 143 filterChain.setWebApp(invocation.getWebApp()); 144 if (methodMap.size() > 0) 145 filterChain.setMethodMap(methodMap); 146 filterChain.setConstraints(constraints); 147 148 return filterChain; 149 } 150 151 return next; 152 } 153 } 154 | Popular Tags |