合规方案:
    建议系统通过全局过滤器来检测用户是否登录、是否对资源具有访问权限。

    1. public class PrivilegeFilter implements Filter {
    2. private Properties properties = new Properties();
    3. @Override
    4. public void destroy() {
    5. properties = null;
    6. }
    7. @Override
    8. public void init(FilterConfig config) throws ServletException {
    9. //获取资源访问权限配置
    10. String fileName = config.getInitParameter("privilegeFile");
    11. String realPath = config.getServletContext().getRealPath(fileName);
    12. try {
    13. properties.load(new FileInputStream(realPath));
    14. } catch (Exception e) {
    15. config.getServletContext().log("读取权限控制文件失败", e);
    16. }
    17. }
    18. @Override
    19. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
    20. throws IOException, ServletException {
    21. HttpServletRequest request = (HttpServletRequest) req;
    22. HttpServletResponse response = (HttpServletResponse) res;
    23. String requestUri = request.getRequestURI().replace(request.getContextPath() + "/", "");
    24. String action = request.getParameter("action");
    25. action = action == null ? "" : action;
    26. String uri = requestUri + "?action=" + action;
    27. String role = (String) request.getSession().getAttribute("role");
    28. role = role == null ? "guest" : role;
    29. boolean authen = false;
    30. for (Object obj : properties.keySet()) {
    31. String key = (String) obj;
    32. if (uri.matches(key.replace("?", "\\?").replace(".", "\\.").replace("*", ".*"))) {
    33. if (role.equals(properties.get(key))) {
    34. authen = true;
    35. break;
    36. }
    37. }
    38. }
    39. if (!authen) {
    40. throw new RuntimeException("您无权访问该页面,请以合适的身份登录后查看。");
    41. }
    42. chain.doFilter(request, response);
    43. }
    44. }
    1. admin.do?action=* = administrator
    2. list.do?action=add = admin
    3. list.do?action=view = guest
    1. <filter>
    2. <filter-name>privilegeFilter</filter-name>
    3. <filter-class>com.filter.privilegeFilter</filter-class>
    4. <init-param>
    5. <param-name>privilegeFile</param-name>
    6. <param-value>/WEB-INF/privilege.properties</param-value>
    7. </init-param>
    8. </filter>