/connect-web/src/main/java/org/osforce/connect/web/security/interceptor/UrlSecurityInterceptor.java

http://focus-sns.googlecode.com/ · Java · 61 lines · 41 code · 11 blank · 9 comment · 5 complexity · 0621a3c96ac71ffcc00921fd5c5d62ca MD5 · raw file

  1. package org.osforce.connect.web.security.interceptor;
  2. import java.io.IOException;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.osforce.connect.entity.system.User;
  7. import org.osforce.connect.web.AttributeKeys;
  8. import org.osforce.connect.web.route.RouteController;
  9. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  10. /**
  11. * This interceptor should the first in interceptor stack!
  12. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  13. * @since 1.1.0
  14. * @create May 20, 2011 - 11:02:23 PM
  15. * <a href="http://www.opensourceforce.org">????</a>
  16. */
  17. public class UrlSecurityInterceptor extends HandlerInterceptorAdapter {
  18. private String[] administrators = new String[]{};
  19. public UrlSecurityInterceptor() {
  20. }
  21. public void setAdministrators(String administrators) {
  22. this.administrators = StringUtils.split(administrators, ",");
  23. }
  24. public void setAdministrators(String[] administrators) {
  25. this.administrators = administrators;
  26. }
  27. @Override
  28. public boolean preHandle(HttpServletRequest request,
  29. HttpServletResponse response, Object handler) throws Exception {
  30. //
  31. validateSystemSecurity(request, response, handler);
  32. return true;
  33. }
  34. protected void validateSystemSecurity(HttpServletRequest request,
  35. HttpServletResponse response, Object handler) throws IOException {
  36. if(handler instanceof RouteController) {
  37. String requestUri = request.getRequestURI();
  38. if(StringUtils.contains(requestUri, "system")) {
  39. User user = (User) request.getAttribute(AttributeKeys.USER_KEY);
  40. for(String administrator : administrators) {
  41. if(user!=null && StringUtils.equals(user.getUsername(), administrator)) {
  42. return ;
  43. }
  44. }
  45. //
  46. response.sendRedirect(request.getContextPath());
  47. }
  48. }
  49. }
  50. }