使用JavaEE的ServerAuthModule模組和web.xml進行相應配置,實現對使用者的許可權控制

來源:互聯網
上載者:User

標籤: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

@Statelesspublic 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進行相應配置,實現對使用者的許可權控制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.