小编典典

如何根据用户登录隐藏某些功能?

jsp

我们想基于Tomcat中的用户登录隐藏一些代码功能。我们正在使用基本身份验证。有什么建议么?


阅读 387

收藏
2020-06-08

共1个答案

小编典典

如果 您的意思只是根据 用户是否登录 而隐藏一些资源,那么这只是限制对某些页面的访问(请参阅下面的参考资料)。

如果 要基于 登录 隐藏某些功能,则解决方案之一就是检查JSP内部的用户角色并相应地输出内容。

原始示例:
sample.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
    <title>Sample Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <c:choose>
        <c:when test="${pageContext.request.isUserInRole('admin')}">
            <p>Content for admin.<p>
        </c:when>
        <c:when test=${pageContext.request.isUserInRole('someRole')}">
            <p>Some content here</p>
        <c:when>
        <c:otherwise>
            <p>Another Content</p>
        </c:otherwise>
    </c:choose>
</body>
</html>

注意!
为了能够使用EL调用带有参数的方法,必须使用最低 Servlet版本3
从这里引用:https :
//stackoverflow.com/tags/el/info

由于EL 2.2是Servlet 3.0 / JSP 2.2(Tomcat 7,Glassfish 3,JBoss AS
6等)的一部分,因此可以调用非getter方法,如果需要的话可以使用参数。


根据用户角色来隐藏/限制对某些页面的访问的另一种方法 是在 web.xml中 进行安全配置,或者使用批注(最低Java EE
5),或者创建自己的 过滤器 来检查用户的角色。请求。

要创建自己的 Filter ,请创建一个实现
javax.servlet.Filter
接口的类,并在
doFilter()
方法中使用HttpServletRequest方法
isUserInRole()
检查发出请求的用户的角色。

这是实现自定义 Filter 的简单示例:
RoleCheckFilter.java

package com.example.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet Filter implementation class RoleCheckFilter.
 * Its purpose is to check logged-in user's role and
 * and accordingly allow or prevent access to the web resources.
 */
public class RoleCheckFilter implements Filter {

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig filterConfig) throws ServletException {}

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
                throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        if (request.isUserInRole("admin")) {
            // user have the appropriate rights, allow the request
            chain.doFilter(request, response);
        } else {
            // user does not have the appropriate rights, do something about it
            request.setAttribute("error", "You don't have enough rights to access this resource");
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            // or you could forward a user request somewhere
        }
    }


    /**
     * @see Filter#destroy()
     */
    public void destroy() {}

}

web.xml中 添加适当的过滤器配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    ...

    <filter>
        <filter-name>Role Check Filter</filter-name>
        <filter-class>com.example.filter.RoleCheckFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Role Check Filter</filter-name>
        <url-pattern>/admin/*</url-pattern>
    </filter-mapping>

    ...

</web-app>

当然, 在您的情况下 ,考虑到您使用 基本身份验证 的事实,在 web.xml声明性安全性 )中进行安全性配置或使用
编程安全性 要容易得多。

引用官方Java EE文档中的内容:

可以通过以下方式为Web应用程序实现Java EE安全服务:

  • 元数据注释 (或简称为注释)用于指定有关类文件内安全性的信息。部署应用程序时,此信息可以由应用程序部署描述符使用或覆盖。

  • 声明式安全性 表示应用程序的安全性结构,包括部署描述符中的安全性角色,访问控制和身份验证要求,该描述符在应用程序外部。
    在部署描述符中显式指定的任何值都将覆盖在注释中指定的任何值。

  • 程序安全性 嵌入在应用程序中,用于制定安全性决策。当仅声明性安全不足以表示应用程序的安全模型时,程序安全性将很有用。


查看与保护Java EE应用程序有关的官方Java EE文档(在这种情况下,请注意 指定“授权约束” 部分):
Java EE
6:保护Web应用程序

Java EE
5:保护Web应用程序

还可以从官方文档中查看示例:
Java
EE6。示例:保护Web应用程序

Java EE
5:示例:保护Web应用程序

2020-06-08