• 使用JavaEE的ServerAuthModule模块和web.xml进行相应配置,实现对用户的权限控制


    ServerAuthModule这里不细说,可以自行百度。

    重点在注释:

     <!-- 声明用于安全约束的角色 -->
        <security-role>
            <role-name>spx.main</role-name>
        </security-role>
        <security-role>
            <role-name>spx.user</role-name>
        </security-role>
    
        <security-constraint>
            <web-resource-collection>
                <!-- 这个名字是必须的,自定义,由工具使用,别的地方不使用 -->
                <web-resource-name>all</web-resource-name>
                <!-- 指定要受约束的资源,至少有一个。可以有多个. -->
                <url-pattern>/*</url-pattern>
                <!-- 指定受约束的方法
                如果没有<http-method>元素,所有http方法都受到约束。
                这里放置了POST方法,表示只有POST方法是受约束的。其他任何角色的人可以访问GET和其他的方法。
                但不能访问POST方法,除非通过下面<auth-constraint>的验证。
                -->
                <http-method>POST</http-method>
            </web-resource-collection>
            <!-- auth-constraint为可选 -->
            <!-- 如果没有<auth-constraint>表示所有角色都能访问POST方法,如果是<auth-constraint/>表示任何角色都不能访问POST方法 -->
            <auth-constraint>
                <!-- 可选。表示哪些角色能够在指定的资源上调用受约束的方法。这里表示只有拥有spx.user角色的人能够访问POST方法
                角色与上面<security-role>里的<role-name>对应 -->
                <role-name>spx.user</role-name>
            </auth-constraint>
        </security-constraint>

     在项目中导入sam模块的jar包,将项目打成war包,放入JavaEE容器中,启动服务,如此就实现了一次URL级别的权限控制。

    假设通过以上验证进入到了VerifyAuthModuleServlet的doPost方法中,

    可以在doPost中加上以下

    UserInfo userInfo = (UserInfo)req.getUserPrincipal();

    来获取到用户信息。

    还可以加上以下来判断用户是否拥有其他角色:

     if(req.isUserInRole("spx.admin")) {
                res.setStatus(200);
                res.getWriter().print(hello);
                return;
     }

    注意:"spx.admin"必须是web-app划分出来的角色中的一种,否则这里一直false。这里和@RolesAllowed("spx.main")有所区别,@RolesAllowed("spx.main")只需用户有该权限即可,

    而没必要一定要存在于<security-role>中。

    EJB中进行方法级别的权限判断

    如以下无状态会话bean

    @Stateless
    public class HelloWorldBean implements HelloWorldLocal {
    
        private Logger logger = Logger.getLogger(HelloWorldBean.class.getName());
        
        @EJB                   //采用注解方式
        private Other other;
    
        @Resource
        private SessionContext sessionContext;
    
    
        @RolesAllowed("spx.user")
        public String sayHello(String name) {
    
            Principal principal = sessionContext.getCallerPrincipal();
            logger.info("==========EJB============="+principal.getName());
    
            return name+"说:你好世界!"+other.sayMe();
        }
    
    }

    @RolesAllowed判断请求的用户是否拥有此权限,如果拥有权限则可以进入方法体,不拥有则抛出EJBAccessException异常

     try{
                 hello = helloWorldLocal.sayHello("jiangmeng");
     }catch (EJBAccessException e){
                res.setStatus(403);
                res.getWriter().write("{"err":"403 forbidden"}");
     }

    我们可以通过捕获该异常,给客户端相应的提示。

    而且我们注意到上面可以在EJB中通过SessionContext.getCallerPrincipal()来获取到用户信息。

  • 相关阅读:
    hdu5091(线段树+扫描线)
    hdu2874(tarjan)
    hdu4252
    poj2452(RMQ+二分)
    Dragon Balls HDU
    CF803
    poj1962(带权并查集)
    hdu2818(带权并查集)
    GitHub入门之一:使用github下载项目 .
    (亲测)设​置​m​y​e​c​l​i​p​s​e​打​开​默​认​工​作​空​间
  • 原文地址:https://www.cnblogs.com/jay763190097/p/6741499.html
Copyright © 2020-2023  润新知