码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-04-21 00:02:42      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:text   否则   启动   logger   less   ldb   ipa   异常   resource   

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

重点在注释:

 <!-- 给web-app划分角色 -->
    <security-role>
        <role-name>spx.main</role-name>
    </security-role>
    <security-role>
        <role-name>spx.user</role-name>
    </security-role>

    <!--  只有配置security-constraint才会去调ServerAuthModule模块  -->
    <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

 

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()来获取到用户信息。

 

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

标签:text   否则   启动   logger   less   ldb   ipa   异常   resource   

原文地址:http://www.cnblogs.com/jay763190097/p/6741499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!